diff --git a/src/Bird.java b/src/Bird.java index 15151dfd2317fe46916242a810148a63a179f3ae..c0011f78cadba4fc0d11c59a45c1482785ff4483 100644 --- a/src/Bird.java +++ b/src/Bird.java @@ -5,7 +5,7 @@ public class Bird { public double velocity = 0; 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 final double acceleration = 0.4; @@ -14,8 +14,8 @@ public class Bird { public final double x = 200; public double y = 350; - public void Bird() { - } +// public void Bird() { +// } public void Render() { @@ -23,6 +23,7 @@ public class Bird { this.birdWingUp.draw(this.x, this.y); /* repeat every 10 frames */ } else { + /* start with windDown */ this.birdWingDown.draw(this.x, this.y); } this.frameCounterSinceStart++; diff --git a/src/Pipe.java b/src/Pipe.java index 0abe366c921b44919086ffa27713932de393556a..51005db4f3e1766fa9b95bd9818ae563eec2604b 100644 --- a/src/Pipe.java +++ b/src/Pipe.java @@ -6,9 +6,9 @@ import bagel.util.Rectangle; public class Pipe { private final double velocity = -5; - private double pos = Window.getWidth(); /* x-coord at spawn */ - private final int pixelGap = 168; - public Point centrePipe1, centrePipe2; + public double pos = Window.getWidth(); /* x-coord at spawn */ + private final int pixelGap = 168; /* gap between pipes */ + private Point centrePipe1, centrePipe2; /* the higher this value the higher the pipe gap spawns, vice versa */ private final int initialY = 50; /* y-value of pipe at spawn */ @@ -18,14 +18,18 @@ public class Pipe { 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 */ this.pipeImage1.draw(this.pos, -this.initialY); centrePipe1 = new Point(this.pos, -this.initialY); + + /* generate hit-point rectangle for top pipe */ rectPipe1 = this.pipeImage1.getBoundingBoxAt(centrePipe1); /* straight up pipe */ @@ -33,12 +37,17 @@ public class Pipe { this.pipeImage2.draw(this.pos, Window.getHeight() -this.initialY +this.pixelGap, options.setRotation(Math.PI)); centrePipe2 = new Point(this.pos, Window.getHeight() -this.initialY +this.pixelGap); + + /* generate hit-point rectangle for bottom pipe */ rectPipe2 = this.pipeImage2.getBoundingBoxAt(centrePipe2); - if(rectPipe2.intersects(bird)) { - System.out.println("COLLISION!\n"); + if(rectPipe1.intersects(bird) || rectPipe2.intersects(bird)) { + /* 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 diff --git a/src/ShadowFlap.java b/src/ShadowFlap.java index a53841fbfb49ab1cd8a6fc2605b5d0ab3d8fce12..1f67fb5d3da64f426d707c480cb9fbd16407aa2b 100644 --- a/src/ShadowFlap.java +++ b/src/ShadowFlap.java @@ -14,11 +14,17 @@ public class ShadowFlap extends AbstractGame { private Pipe pipe; private int score = 0; private String gameState = "mainMenu", temp = ""; + + /* Messages to display on screen */ private final String startString = "PRESS SPACE TO START"; private final String outOfBounds = "GAME OVER!"; private final String finalScore = "Final Score: "; private final String winFont = "CONGRATULATIONS!"; + private final String scoreFont = "Score: "; + 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() { super(1024, 768, "Flappy Bird"); @@ -45,30 +51,39 @@ public class ShadowFlap extends AbstractGame { if(this.gameState == "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.y -= bird.velocity; + bird.y -= bird.velocity; /* update bird position on screen */ if(input.wasPressed(Keys.SPACE)) { bird.velocity = 6; } if(bird.y > Window.getHeight() || bird.y < 0) { + /* bird went out of bound */ gameState = "outOfBounds"; }; - bird.Render(); + bird.Render(); /* render bird every frame */ Point birdPos = new Point(bird.x, bird.y); temp = pipe.Render(bird.birdWingDown.getBoundingBoxAt(birdPos)); if(temp == null) { + /* bird did not go out of bounds or collide */ temp = ""; } else { - gameState = temp; + /* bird went out of bounds or collided with pipe*/ + gameState = temp; /* update gamestate to gameOutOfBounds */ } if(bird.x > pipe.pos) { + /* successfully passed a pair of pipes */ this.score = 1; } if(pipe.pos <= 0) { + /* if centre of pipe hits left border, win condition activates */ this.gameState = "win"; } } @@ -79,7 +94,7 @@ public class ShadowFlap extends AbstractGame { Window.getHeight() / 2.0); font.drawString(finalScore + String.valueOf(this.score), (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") { Background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); @@ -88,7 +103,7 @@ public class ShadowFlap extends AbstractGame { Window.getHeight() / 2.0); font.drawString(finalScore + String.valueOf(this.score), (Window.getWidth() / 2.0) - (font.getWidth(finalScore + String.valueOf(this.score)) / 2.0), - (Window.getHeight() / 2.0) + 75); + (Window.getHeight() / 2.0) + secondLineGap); } else { /* loading screen */