From 5a894833bdcd9412869c4186749eff9eb8cfb2f9 Mon Sep 17 00:00:00 2001 From: Aarti Bhattarai <abhatta5@myune.edu.au> Date: Thu, 21 Sep 2023 13:54:02 +0545 Subject: [PATCH] Make circle radius dynamic --- app/src/main/java/brickbreaker/Ball.java | 2 +- app/src/main/java/brickbreaker/GamePanel.java | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/brickbreaker/Ball.java b/app/src/main/java/brickbreaker/Ball.java index 42c5006..24987c1 100644 --- a/app/src/main/java/brickbreaker/Ball.java +++ b/app/src/main/java/brickbreaker/Ball.java @@ -11,7 +11,7 @@ package brickbreaker; public class Ball { private Vector position; private Vector direction; - private int radius; + public int radius; public Ball(int posX, int posY, int dirX, int dirY, int radius) { this.position = new Vector(posX, posY); diff --git a/app/src/main/java/brickbreaker/GamePanel.java b/app/src/main/java/brickbreaker/GamePanel.java index 4601626..2bc524f 100644 --- a/app/src/main/java/brickbreaker/GamePanel.java +++ b/app/src/main/java/brickbreaker/GamePanel.java @@ -31,7 +31,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { public GamePanel() { this.balls.add(new Ball(350, 450, 1, -1, 20)); // Adjust the initial ball position and direction - this.balls.add(new Ball(380, 420, -1, 1, 20)); + this.balls.add(new Ball(380, 420, -1, 1, 50)); this.totalBricks = rows * columns; map = new GameMapBuilder(rows, columns); @@ -58,7 +58,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { graphics.fillRect(691, 0, 3, 592); graphics.setColor(Color.blue); - graphics.fillRect(paddle, 550, 100, 12); + graphics.fillRect(paddle, 550, 100, 20); graphics.setColor(Color.RED); // ball color Iterator<Ball> ballsIterator = balls.iterator(); @@ -67,7 +67,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { Ball ball = ballsIterator.next(); Vector ballPosition = ball.getPosition(); - if (ballPosition.y > 570) { + if (ballPosition.y > 560) { ballsIterator.remove(); continue; } else { @@ -75,7 +75,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { } graphics.setColor(Color.RED); // ball color - graphics.fillOval(ballPosition.x, ballPosition.y, 20, 20); + graphics.fillOval(ballPosition.x, ballPosition.y, ball.radius, ball.radius); } graphics.setColor(Color.black); @@ -86,6 +86,10 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { Color color = new Color(0XFF6464); gameOver(graphics, "You Won", color); } + + if (balls.isEmpty()) { + gameOver(graphics, "Game Over", Color.BLACK); + } } @@ -106,7 +110,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { Vector ballPosition = ball.getPosition(); Vector ballDirection = ball.getDirection(); - if (new Rectangle(ballPosition.x, ballPosition.y, 20, 20).intersects(new Rectangle(paddle, 550, 100, 8))) { + if (new Rectangle(ballPosition.x, ballPosition.y, ball.radius, ball.radius).intersects(new Rectangle(paddle, 550, 100, 8))) { ballDirection.y = -ballDirection.y; ball.setDirection(ballDirection.x, ballDirection.y); } @@ -120,7 +124,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { int brickHeight = map.brickHeight; Rectangle brickRect = new Rectangle(brickX, brickY, brickWidth, brickHeight); - Rectangle ballRect = new Rectangle(ballPosition.x, ballPosition.y, 20, 20); + Rectangle ballRect = new Rectangle(ballPosition.x, ballPosition.y, ball.radius, ball.radius); if (ballRect.intersects(brickRect)) { -- GitLab