Skip to content
Snippets Groups Projects
Commit 1cbc4581 authored by roguecomp's avatar roguecomp
Browse files

Added comments and changed few variables from public to private

parent 048427a6
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ public class Bird { ...@@ -5,7 +5,7 @@ public class Bird {
public double velocity = 0; public double velocity = 0;
private static int frameCounterSinceStart = 1; private static int frameCounterSinceStart = 1;
public Image birdWingUp = new Image("res/birdWingUp.png"); private Image birdWingUp = new Image("res/birdWingUp.png");
public Image birdWingDown = new Image("res/birdWingDown.png"); public Image birdWingDown = new Image("res/birdWingDown.png");
public final double acceleration = 0.4; public final double acceleration = 0.4;
...@@ -14,8 +14,8 @@ public class Bird { ...@@ -14,8 +14,8 @@ public class Bird {
public final double x = 200; public final double x = 200;
public double y = 350; public double y = 350;
public void Bird() { // public void Bird() {
} // }
public void Render() { public void Render() {
...@@ -23,6 +23,7 @@ public class Bird { ...@@ -23,6 +23,7 @@ public class Bird {
this.birdWingUp.draw(this.x, this.y); /* repeat every 10 frames */ this.birdWingUp.draw(this.x, this.y); /* repeat every 10 frames */
} }
else { else {
/* start with windDown */
this.birdWingDown.draw(this.x, this.y); this.birdWingDown.draw(this.x, this.y);
} }
this.frameCounterSinceStart++; this.frameCounterSinceStart++;
......
...@@ -6,9 +6,9 @@ import bagel.util.Rectangle; ...@@ -6,9 +6,9 @@ import bagel.util.Rectangle;
public class Pipe { public class Pipe {
private final double velocity = -5; private final double velocity = -5;
private double pos = Window.getWidth(); /* x-coord at spawn */ public double pos = Window.getWidth(); /* x-coord at spawn */
private final int pixelGap = 168; private final int pixelGap = 168; /* gap between pipes */
public Point centrePipe1, centrePipe2; private Point centrePipe1, centrePipe2;
/* the higher this value the higher the pipe gap spawns, vice versa */ /* the higher this value the higher the pipe gap spawns, vice versa */
private final int initialY = 50; /* y-value of pipe at spawn */ private final int initialY = 50; /* y-value of pipe at spawn */
...@@ -18,14 +18,18 @@ public class Pipe { ...@@ -18,14 +18,18 @@ public class Pipe {
private Rectangle rectPipe1, rectPipe2; private Rectangle rectPipe1, rectPipe2;
public void Pipe() { // public void Pipe() {
//
// }
} public String Render(Rectangle bird) {
String gameState; /* String to contain the state of the game */
public void Render(Rectangle bird) {
/* upside down pipe */ /* upside down pipe */
this.pipeImage1.draw(this.pos, -this.initialY); this.pipeImage1.draw(this.pos, -this.initialY);
centrePipe1 = new Point(this.pos, -this.initialY); centrePipe1 = new Point(this.pos, -this.initialY);
/* generate hit-point rectangle for top pipe */
rectPipe1 = this.pipeImage1.getBoundingBoxAt(centrePipe1); rectPipe1 = this.pipeImage1.getBoundingBoxAt(centrePipe1);
/* straight up pipe */ /* straight up pipe */
...@@ -33,12 +37,17 @@ public class Pipe { ...@@ -33,12 +37,17 @@ public class Pipe {
this.pipeImage2.draw(this.pos, Window.getHeight() -this.initialY +this.pixelGap, this.pipeImage2.draw(this.pos, Window.getHeight() -this.initialY +this.pixelGap,
options.setRotation(Math.PI)); options.setRotation(Math.PI));
centrePipe2 = new Point(this.pos, Window.getHeight() -this.initialY +this.pixelGap); centrePipe2 = new Point(this.pos, Window.getHeight() -this.initialY +this.pixelGap);
/* generate hit-point rectangle for bottom pipe */
rectPipe2 = this.pipeImage2.getBoundingBoxAt(centrePipe2); rectPipe2 = this.pipeImage2.getBoundingBoxAt(centrePipe2);
if(rectPipe2.intersects(bird)) { if(rectPipe1.intersects(bird) || rectPipe2.intersects(bird)) {
System.out.println("COLLISION!\n"); /* bird collides with pipes */
gameState = "outOfBounds";
return gameState;
} }
this.pos += this.velocity; this.pos += this.velocity; /* update pipe location */
return null;
} }
} }
\ No newline at end of file
...@@ -14,11 +14,17 @@ public class ShadowFlap extends AbstractGame { ...@@ -14,11 +14,17 @@ public class ShadowFlap extends AbstractGame {
private Pipe pipe; private Pipe pipe;
private int score = 0; private int score = 0;
private String gameState = "mainMenu", temp = ""; private String gameState = "mainMenu", temp = "";
/* Messages to display on screen */
private final String startString = "PRESS SPACE TO START"; private final String startString = "PRESS SPACE TO START";
private final String outOfBounds = "GAME OVER!"; private final String outOfBounds = "GAME OVER!";
private final String finalScore = "Final Score: "; private final String finalScore = "Final Score: ";
private final String winFont = "CONGRATULATIONS!"; private final String winFont = "CONGRATULATIONS!";
private final String scoreFont = "Score: ";
private final Font font = new Font("res/slkscr.ttf", 48); private final Font font = new Font("res/slkscr.ttf", 48);
private Point scorePoint = new Point(100, 100); /* Score position on screen */
private final int secondLineGap = 75; /* 75 pixel gap between lines on screen */
public ShadowFlap() { public ShadowFlap() {
super(1024, 768, "Flappy Bird"); super(1024, 768, "Flappy Bird");
...@@ -45,30 +51,39 @@ public class ShadowFlap extends AbstractGame { ...@@ -45,30 +51,39 @@ public class ShadowFlap extends AbstractGame {
if(this.gameState == "started") { if(this.gameState == "started") {
/* game has started */ /* game has started */
bird.velocity -= bird.acceleration;
font.drawString(scoreFont + String.valueOf(this.score), scorePoint.x, scorePoint.y);
bird.velocity -= bird.acceleration; /* update bird velocity */
bird.velocity = Math.max(bird.velocity, bird.terminalVelocity); /* set fall velocity limit */ bird.velocity = Math.max(bird.velocity, bird.terminalVelocity); /* set fall velocity limit */
bird.y -= bird.velocity; bird.y -= bird.velocity; /* update bird position on screen */
if(input.wasPressed(Keys.SPACE)) { bird.velocity = 6; } if(input.wasPressed(Keys.SPACE)) { bird.velocity = 6; }
if(bird.y > Window.getHeight() || bird.y < 0) { if(bird.y > Window.getHeight() || bird.y < 0) {
/* bird went out of bound */
gameState = "outOfBounds"; gameState = "outOfBounds";
}; };
bird.Render(); bird.Render(); /* render bird every frame */
Point birdPos = new Point(bird.x, bird.y); Point birdPos = new Point(bird.x, bird.y);
temp = pipe.Render(bird.birdWingDown.getBoundingBoxAt(birdPos)); temp = pipe.Render(bird.birdWingDown.getBoundingBoxAt(birdPos));
if(temp == null) { if(temp == null) {
/* bird did not go out of bounds or collide */
temp = ""; temp = "";
} }
else { else {
gameState = temp; /* bird went out of bounds or collided with pipe*/
gameState = temp; /* update gamestate to gameOutOfBounds */
} }
if(bird.x > pipe.pos) { if(bird.x > pipe.pos) {
/* successfully passed a pair of pipes */
this.score = 1; this.score = 1;
} }
if(pipe.pos <= 0) { if(pipe.pos <= 0) {
/* if centre of pipe hits left border, win condition activates */
this.gameState = "win"; this.gameState = "win";
} }
} }
...@@ -79,7 +94,7 @@ public class ShadowFlap extends AbstractGame { ...@@ -79,7 +94,7 @@ public class ShadowFlap extends AbstractGame {
Window.getHeight() / 2.0); Window.getHeight() / 2.0);
font.drawString(finalScore + String.valueOf(this.score), font.drawString(finalScore + String.valueOf(this.score),
(Window.getWidth() / 2.0) - (font.getWidth(finalScore + String.valueOf(this.score)) / 2.0), (Window.getWidth() / 2.0) - (font.getWidth(finalScore + String.valueOf(this.score)) / 2.0),
(Window.getHeight() / 2.0) + 75); (Window.getHeight() / 2.0) + secondLineGap);
} }
else if (gameState == "win") { else if (gameState == "win") {
Background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); Background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0);
...@@ -88,7 +103,7 @@ public class ShadowFlap extends AbstractGame { ...@@ -88,7 +103,7 @@ public class ShadowFlap extends AbstractGame {
Window.getHeight() / 2.0); Window.getHeight() / 2.0);
font.drawString(finalScore + String.valueOf(this.score), font.drawString(finalScore + String.valueOf(this.score),
(Window.getWidth() / 2.0) - (font.getWidth(finalScore + String.valueOf(this.score)) / 2.0), (Window.getWidth() / 2.0) - (font.getWidth(finalScore + String.valueOf(this.score)) / 2.0),
(Window.getHeight() / 2.0) + 75); (Window.getHeight() / 2.0) + secondLineGap);
} }
else { else {
/* loading screen */ /* loading screen */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment