Skip to content
Snippets Groups Projects
Commit 5a894833 authored by abhatta5's avatar abhatta5
Browse files

Make circle radius dynamic

parent 248bd0bb
No related branches found
No related tags found
1 merge request!1Game graphics
......@@ -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);
......
......@@ -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);
......@@ -87,6 +87,10 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener {
gameOver(graphics, "You Won", color);
}
if (balls.isEmpty()) {
gameOver(graphics, "Game Over", Color.BLACK);
}
}
public void gameOver(Graphics graphics, String gameOverText, Color color) {
......@@ -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)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment