Added documentation

This commit is contained in:
Sagi Dayan 2016-01-23 18:20:50 +02:00
parent 18e9a26ef3
commit 57575947f4
7 changed files with 45 additions and 40 deletions

View file

@ -19,17 +19,17 @@ import java.util.Vector;
public class GameEngine extends MouseAdapter {
private final int PIPE_ACC = 10, PIPE_WIDTH = 90;
private final int PIPE_ACC = 10, PIPE_WIDTH = 90, PIPE_INTERVAL_TIME = 2000;
public boolean gameOn , gameOver, isFirstGame, canScore;
private Bird bird;
private Vector<Sprite> pipes; //will save all laser shots and asteroids which are currently on the screen.
private int pWidth, pHeight; //panel dimensions
private Timer pipeTimer;
private Timer pipeTimer; //interval to create new pipe
private Random r;
private int score;
private BufferedImage sceneImage;
private Vector<SideScollerBackground> backgrounds;
private Vector<SideScollerBackground> backgrounds; //Background elements
private Vector<AudioClip> successSounds;
private AudioClip themeAudioClip;
@ -50,8 +50,10 @@ public class GameEngine extends MouseAdapter {
themeAudioClip = null;
}
if(themeAudioClip != null)
if(themeAudioClip != null) {
themeAudioClip.loop();
}
r = new Random();
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
loadSuccessSounds();
@ -67,7 +69,7 @@ public class GameEngine extends MouseAdapter {
*/
private void startNewGame(){
this.gameOn = true;
pipeTimer = new Timer(2000, new PipeTimerListener());
pipeTimer = new Timer(PIPE_INTERVAL_TIME, new PipeTimerListener());
backgrounds = new Vector<>();
initBackgrounds();
initGame();
@ -87,13 +89,14 @@ public class GameEngine extends MouseAdapter {
}
// Loads soundFX Vector
private void loadSuccessSounds(){
successSounds = new Vector<>();
for (int i = 1 ; i < 5 ; i++){
successSounds.add(Applet.newAudioClip(getClass().getResource("/Sounds/pass"+i+".wav")));
}
}
//initialize background Sprites
private void initBackgrounds(){
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 2, "skyLine.png", pWidth, pHeight));
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 5, "trees.png", pWidth + 50, pHeight));
@ -175,6 +178,7 @@ public class GameEngine extends MouseAdapter {
pipeTimer.stop();
}
//passed the pipe?
if(canScore && !pipes.isEmpty() && bird.getLocX() >= pipes.elementAt(0).getLocX() + pipes.elementAt(0).getSWidth()) {
score++;
canScore = false;
@ -223,9 +227,9 @@ public class GameEngine extends MouseAdapter {
}
}
public void mouseClicked(MouseEvent e){
//mouse clicked starts game Or jumps
public void mousePressed(MouseEvent e){
if(gameOver){
// gameOn = true;
this.isFirstGame = false;
gameOver = false;
initGame();

View file

@ -16,7 +16,6 @@ public class GamePanel extends JPanel implements Runnable{
private final URL startURL= getClass().getResource("/Images/start.png");
// private int width, height;
private GameEngine engine;
private JLabel lbl_score, lbl_gameOver, lbl_bg, lbl_start;
@ -51,7 +50,7 @@ public class GamePanel extends JPanel implements Runnable{
lbl_bg = new JLabel(img_bg);
lbl_bg.setBounds(0,0,width,height);
//create "start game" & "game over" labels
img_start = new ImageIcon(startURL);
lbl_start = new JLabel(img_start);
lbl_start.setVisible(true);
@ -119,7 +118,7 @@ public class GamePanel extends JPanel implements Runnable{
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
//Some labels - Score & countDown
//Update Score
lbl_score.setText("SCORE : " + engine.getScore());
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 31 KiB

View file

@ -8,24 +8,28 @@ package Sprites;
public class Bird extends Sprite {
private final int ACC = 10, MAX_ANGLE=45;
public Bird(int xPosition, int pWidth, int pHeight, int size){
super(xPosition, pHeight/2, pWidth, pHeight, 0, "birdSprite.png", 0 ,size, size);
}
//"pull" sprite down up to ACC speed
@Override
public void update() {
if(acceleration > -10 ) {
if(acceleration > -ACC ) {
acceleration--;
}
locY -= acceleration;
if(angle <= 45)
if(angle <= MAX_ANGLE)
angle += 5;
}
//bird "jumps" ACC pixels up, and img angle is set to MAX_ANGLE
public void jump() {
this.acceleration = 10;
angle = -45;
angle = -1*MAX_ANGLE;
}
}

View file

@ -11,6 +11,7 @@ public class Pipe extends Sprite {
}
//move pipe left
@Override
public void update() {
locX -= acceleration;

View file

@ -25,22 +25,8 @@ public class SideScollerBackground extends Sprite {
}
/*
* resizes image to a set size
*/
@Override
protected void setImageDimensions()
{
Image tmp = bImage.getScaledInstance(sWidth, sHeight, Image.SCALE_SMOOTH);
BufferedImage bi = new BufferedImage(sWidth, sHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(tmp,0,0,null);
g2d.dispose();
bImage = bi;
}
//if a bg element moves pass the screen, it returns to the begining
@Override
public void update() {
locX -= acceleration;
@ -51,6 +37,7 @@ public class SideScollerBackground extends Sprite {
locXCopy = pWidth;
}
//draws 2 bg instances that follow each other
@Override
public void drawSprite(Graphics g, JPanel p){
super.drawSprite(g,p);

View file

@ -77,8 +77,18 @@ public abstract class Sprite {
*/
public double getLocY() {return locY;}
/**
* returns sprite image width
* @return
* int
*/
public int getSWidth() {return sWidth;}
/**
* returns sprite image height
* @return
* int
*/
public int getsHeight() {return sHeight;}
/**
@ -131,17 +141,17 @@ public abstract class Sprite {
/**
* its not a bug it's a feature. actually it just moves a shape that goes beyond the screen to the other side.
*/
protected void outOfScreeFix(){
if(locX < 0 - sWidth)
locX = pWidth;
else if (locX > pWidth+sWidth)
locX = 0-sWidth;
if(locY < 0 - sHeight)
locY = pHeight;
else if(locY > pHeight+sHeight)
locY = 0-sHeight ;
}
// protected void outOfScreeFix(){
// if(locX < 0 - sWidth)
// locX = pWidth;
// else if (locX > pWidth+sWidth)
// locX = 0-sWidth;
//
// if(locY < 0 - sHeight)
// locY = pHeight;
// else if(locY > pHeight+sHeight)
// locY = 0-sHeight ;
// }
/**
* abstract method for drawing sprite.