From 864ddd930ad72d84ba335cb0f317ad5d6d5fbd74 Mon Sep 17 00:00:00 2001 From: roguecomp <roguecomp001@gmail.com> Date: Fri, 10 Sep 2021 17:48:56 +1000 Subject: [PATCH] Added collision detection logic --- src/Bird.java | 6 +++--- src/Pipe.java | 26 +++++++++++++++++++++----- src/ShadowFlap.java | 4 +++- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/Bird.java b/src/Bird.java index c4d1967..15151df 100644 --- a/src/Bird.java +++ b/src/Bird.java @@ -2,14 +2,14 @@ import bagel.Image; public class Bird { /* positive means moving up, negative means moving down */ - public double velocity; - private static int frameCounterSinceStart = 0; + public double velocity = 0; + private static int frameCounterSinceStart = 1; public Image birdWingUp = new Image("res/birdWingUp.png"); public Image birdWingDown = new Image("res/birdWingDown.png"); public final double acceleration = 0.4; - public final double terminalVelocity = 10; + public final double terminalVelocity = -10; public final double x = 200; public double y = 350; diff --git a/src/Pipe.java b/src/Pipe.java index 3ebc9f0..0abe366 100644 --- a/src/Pipe.java +++ b/src/Pipe.java @@ -1,27 +1,43 @@ -import bagel.*; +import bagel.DrawOptions; +import bagel.Image; +import bagel.Window; +import bagel.util.Point; +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; /* the higher this value the higher the pipe gap spawns, vice versa */ 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 Render() { + public void Render(Rectangle bird) { /* 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 */ 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)); + 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; } diff --git a/src/ShadowFlap.java b/src/ShadowFlap.java index 3ce420b..4f03e4c 100644 --- a/src/ShadowFlap.java +++ b/src/ShadowFlap.java @@ -1,4 +1,5 @@ import bagel.*; +import bagel.util.Point; /** * Skeleton Code for SWEN20003 Project 1, Semester 2, 2021 @@ -51,7 +52,8 @@ public class ShadowFlap extends AbstractGame { }; bird.Render(); - pipe.Render(); + Point birdPos = new Point(bird.x, bird.y); + pipe.Render(bird.birdWingDown.getBoundingBoxAt(birdPos)); } else if (gameState == "outOfBounds") { Background.draw(Window.getWidth() / 2.0, Window.getHeight() / 2.0); -- GitLab