Skip to content
Snippets Groups Projects
Commit 864ddd93 authored by roguecomp's avatar roguecomp
Browse files

Added collision detection logic

parent 7dc8f20b
Branches master
No related tags found
No related merge requests found
...@@ -2,14 +2,14 @@ import bagel.Image; ...@@ -2,14 +2,14 @@ import bagel.Image;
public class Bird { public class Bird {
/* positive means moving up, negative means moving down */ /* positive means moving up, negative means moving down */
public double velocity; public double velocity = 0;
private static int frameCounterSinceStart = 0; private static int frameCounterSinceStart = 1;
public Image birdWingUp = new Image("res/birdWingUp.png"); public 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;
public final double terminalVelocity = 10; public final double terminalVelocity = -10;
public final double x = 200; public final double x = 200;
public double y = 350; public double y = 350;
......
import bagel.*; import bagel.DrawOptions;
import bagel.Image;
import bagel.Window;
import bagel.util.Point;
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 */ private double pos = Window.getWidth(); /* x-coord at spawn */
private final int pixelGap = 168; private final int pixelGap = 168;
public 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 */
private Image pipeImage = new Image("res/pipe.png"); private Image pipeImage1 = new Image("res/pipe.png");
private Image pipeImage2 = new Image("res/pipe.png");
private Rectangle rectPipe1, rectPipe2;
public void Pipe() { public void Pipe() {
} }
public void Render() { public void Render(Rectangle bird) {
/* upside down pipe */ /* upside down pipe */
this.pipeImage.draw(this.pos, -this.initialY); this.pipeImage1.draw(this.pos, -this.initialY);
centrePipe1 = new Point(this.pos, -this.initialY);
rectPipe1 = this.pipeImage1.getBoundingBoxAt(centrePipe1);
/* straight up pipe */ /* straight up pipe */
DrawOptions options = new DrawOptions(); DrawOptions options = new DrawOptions();
this.pipeImage.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);
rectPipe2 = this.pipeImage2.getBoundingBoxAt(centrePipe2);
if(rectPipe2.intersects(bird)) {
System.out.println("COLLISION!\n");
}
this.pos += this.velocity; this.pos += this.velocity;
} }
......
import bagel.*; import bagel.*;
import bagel.util.Point;
/** /**
* Skeleton Code for SWEN20003 Project 1, Semester 2, 2021 * Skeleton Code for SWEN20003 Project 1, Semester 2, 2021
...@@ -51,7 +52,8 @@ public class ShadowFlap extends AbstractGame { ...@@ -51,7 +52,8 @@ public class ShadowFlap extends AbstractGame {
}; };
bird.Render(); bird.Render();
pipe.Render(); Point birdPos = new Point(bird.x, bird.y);
pipe.Render(bird.birdWingDown.getBoundingBoxAt(birdPos));
} }
else if (gameState == "outOfBounds") { else if (gameState == "outOfBounds") {
Background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); Background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment