Added documentation
This commit is contained in:
parent
18e9a26ef3
commit
57575947f4
7 changed files with 45 additions and 40 deletions
|
@ -19,17 +19,17 @@ import java.util.Vector;
|
||||||
|
|
||||||
|
|
||||||
public class GameEngine extends MouseAdapter {
|
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;
|
public boolean gameOn , gameOver, isFirstGame, canScore;
|
||||||
private Bird bird;
|
private Bird bird;
|
||||||
private Vector<Sprite> pipes; //will save all laser shots and asteroids which are currently on the screen.
|
private Vector<Sprite> pipes; //will save all laser shots and asteroids which are currently on the screen.
|
||||||
private int pWidth, pHeight; //panel dimensions
|
private int pWidth, pHeight; //panel dimensions
|
||||||
private Timer pipeTimer;
|
private Timer pipeTimer; //interval to create new pipe
|
||||||
private Random r;
|
private Random r;
|
||||||
private int score;
|
private int score;
|
||||||
private BufferedImage sceneImage;
|
private BufferedImage sceneImage;
|
||||||
|
|
||||||
private Vector<SideScollerBackground> backgrounds;
|
private Vector<SideScollerBackground> backgrounds; //Background elements
|
||||||
private Vector<AudioClip> successSounds;
|
private Vector<AudioClip> successSounds;
|
||||||
|
|
||||||
private AudioClip themeAudioClip;
|
private AudioClip themeAudioClip;
|
||||||
|
@ -50,8 +50,10 @@ public class GameEngine extends MouseAdapter {
|
||||||
themeAudioClip = null;
|
themeAudioClip = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(themeAudioClip != null)
|
if(themeAudioClip != null) {
|
||||||
|
|
||||||
themeAudioClip.loop();
|
themeAudioClip.loop();
|
||||||
|
}
|
||||||
r = new Random();
|
r = new Random();
|
||||||
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
|
sceneImage = new BufferedImage(width, height, Image.SCALE_SMOOTH);
|
||||||
loadSuccessSounds();
|
loadSuccessSounds();
|
||||||
|
@ -67,7 +69,7 @@ public class GameEngine extends MouseAdapter {
|
||||||
*/
|
*/
|
||||||
private void startNewGame(){
|
private void startNewGame(){
|
||||||
this.gameOn = true;
|
this.gameOn = true;
|
||||||
pipeTimer = new Timer(2000, new PipeTimerListener());
|
pipeTimer = new Timer(PIPE_INTERVAL_TIME, new PipeTimerListener());
|
||||||
backgrounds = new Vector<>();
|
backgrounds = new Vector<>();
|
||||||
initBackgrounds();
|
initBackgrounds();
|
||||||
initGame();
|
initGame();
|
||||||
|
@ -87,13 +89,14 @@ public class GameEngine extends MouseAdapter {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Loads soundFX Vector
|
||||||
private void loadSuccessSounds(){
|
private void loadSuccessSounds(){
|
||||||
successSounds = new Vector<>();
|
successSounds = new Vector<>();
|
||||||
for (int i = 1 ; i < 5 ; i++){
|
for (int i = 1 ; i < 5 ; i++){
|
||||||
successSounds.add(Applet.newAudioClip(getClass().getResource("/Sounds/pass"+i+".wav")));
|
successSounds.add(Applet.newAudioClip(getClass().getResource("/Sounds/pass"+i+".wav")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//initialize background Sprites
|
||||||
private void initBackgrounds(){
|
private void initBackgrounds(){
|
||||||
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 2, "skyLine.png", pWidth, pHeight));
|
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 2, "skyLine.png", pWidth, pHeight));
|
||||||
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 5, "trees.png", pWidth + 50, pHeight));
|
backgrounds.add(new SideScollerBackground(pWidth, pHeight, 5, "trees.png", pWidth + 50, pHeight));
|
||||||
|
@ -175,6 +178,7 @@ public class GameEngine extends MouseAdapter {
|
||||||
pipeTimer.stop();
|
pipeTimer.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//passed the pipe?
|
||||||
if(canScore && !pipes.isEmpty() && bird.getLocX() >= pipes.elementAt(0).getLocX() + pipes.elementAt(0).getSWidth()) {
|
if(canScore && !pipes.isEmpty() && bird.getLocX() >= pipes.elementAt(0).getLocX() + pipes.elementAt(0).getSWidth()) {
|
||||||
score++;
|
score++;
|
||||||
canScore = false;
|
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){
|
if(gameOver){
|
||||||
// gameOn = true;
|
|
||||||
this.isFirstGame = false;
|
this.isFirstGame = false;
|
||||||
gameOver = false;
|
gameOver = false;
|
||||||
initGame();
|
initGame();
|
||||||
|
|
|
@ -16,7 +16,6 @@ public class GamePanel extends JPanel implements Runnable{
|
||||||
private final URL startURL= getClass().getResource("/Images/start.png");
|
private final URL startURL= getClass().getResource("/Images/start.png");
|
||||||
|
|
||||||
|
|
||||||
// private int width, height;
|
|
||||||
private GameEngine engine;
|
private GameEngine engine;
|
||||||
private JLabel lbl_score, lbl_gameOver, lbl_bg, lbl_start;
|
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 = new JLabel(img_bg);
|
||||||
lbl_bg.setBounds(0,0,width,height);
|
lbl_bg.setBounds(0,0,width,height);
|
||||||
|
|
||||||
|
//create "start game" & "game over" labels
|
||||||
img_start = new ImageIcon(startURL);
|
img_start = new ImageIcon(startURL);
|
||||||
lbl_start = new JLabel(img_start);
|
lbl_start = new JLabel(img_start);
|
||||||
lbl_start.setVisible(true);
|
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
|
g2d.drawImage(engine.getScene(),0,0,this); //Draw the scene
|
||||||
|
|
||||||
//Some labels - Score & countDown
|
//Update Score
|
||||||
lbl_score.setText("SCORE : " + engine.getScore());
|
lbl_score.setText("SCORE : " + engine.getScore());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 31 KiB |
|
@ -8,24 +8,28 @@ package Sprites;
|
||||||
public class Bird extends Sprite {
|
public class Bird extends Sprite {
|
||||||
|
|
||||||
|
|
||||||
|
private final int ACC = 10, MAX_ANGLE=45;
|
||||||
|
|
||||||
public Bird(int xPosition, int pWidth, int pHeight, int size){
|
public Bird(int xPosition, int pWidth, int pHeight, int size){
|
||||||
super(xPosition, pHeight/2, pWidth, pHeight, 0, "birdSprite.png", 0 ,size, size);
|
super(xPosition, pHeight/2, pWidth, pHeight, 0, "birdSprite.png", 0 ,size, size);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//"pull" sprite down up to ACC speed
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
if(acceleration > -10 ) {
|
if(acceleration > -ACC ) {
|
||||||
acceleration--;
|
acceleration--;
|
||||||
}
|
}
|
||||||
locY -= acceleration;
|
locY -= acceleration;
|
||||||
if(angle <= 45)
|
if(angle <= MAX_ANGLE)
|
||||||
angle += 5;
|
angle += 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//bird "jumps" ACC pixels up, and img angle is set to MAX_ANGLE
|
||||||
public void jump() {
|
public void jump() {
|
||||||
this.acceleration = 10;
|
this.acceleration = 10;
|
||||||
angle = -45;
|
angle = -1*MAX_ANGLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ public class Pipe extends Sprite {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//move pipe left
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
locX -= acceleration;
|
locX -= acceleration;
|
||||||
|
|
|
@ -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
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
locX -= acceleration;
|
locX -= acceleration;
|
||||||
|
@ -51,6 +37,7 @@ public class SideScollerBackground extends Sprite {
|
||||||
locXCopy = pWidth;
|
locXCopy = pWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//draws 2 bg instances that follow each other
|
||||||
@Override
|
@Override
|
||||||
public void drawSprite(Graphics g, JPanel p){
|
public void drawSprite(Graphics g, JPanel p){
|
||||||
super.drawSprite(g,p);
|
super.drawSprite(g,p);
|
||||||
|
|
|
@ -77,8 +77,18 @@ public abstract class Sprite {
|
||||||
*/
|
*/
|
||||||
public double getLocY() {return locY;}
|
public double getLocY() {return locY;}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns sprite image width
|
||||||
|
* @return
|
||||||
|
* int
|
||||||
|
*/
|
||||||
public int getSWidth() {return sWidth;}
|
public int getSWidth() {return sWidth;}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns sprite image height
|
||||||
|
* @return
|
||||||
|
* int
|
||||||
|
*/
|
||||||
public int getsHeight() {return sHeight;}
|
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.
|
* 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(){
|
// protected void outOfScreeFix(){
|
||||||
if(locX < 0 - sWidth)
|
// if(locX < 0 - sWidth)
|
||||||
locX = pWidth;
|
// locX = pWidth;
|
||||||
else if (locX > pWidth+sWidth)
|
// else if (locX > pWidth+sWidth)
|
||||||
locX = 0-sWidth;
|
// locX = 0-sWidth;
|
||||||
|
//
|
||||||
if(locY < 0 - sHeight)
|
// if(locY < 0 - sHeight)
|
||||||
locY = pHeight;
|
// locY = pHeight;
|
||||||
else if(locY > pHeight+sHeight)
|
// else if(locY > pHeight+sHeight)
|
||||||
locY = 0-sHeight ;
|
// locY = 0-sHeight ;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* abstract method for drawing sprite.
|
* abstract method for drawing sprite.
|
||||||
|
|
Loading…
Reference in a new issue