Skip to content
Snippets Groups Projects
Commit 248bd0bb authored by abhatta5's avatar abhatta5
Browse files

Closes #11 Feature to spawn Multiple balls

parent b82fb7f3
No related branches found
No related tags found
1 merge request!1Game graphics
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package brickbreaker;
/**
*
* @author Suman
*/
public class Ball {
private Vector position;
private Vector direction;
private int radius;
public Ball(int posX, int posY, int dirX, int dirY, int radius) {
this.position = new Vector(posX, posY);
this.direction = new Vector(dirX, dirY);
this.radius = radius;
}
public void setPosition(int posX, int posY) {
this.position = new Vector(posX, posY);
}
public void setDirection(int dirX, int dirY) {
this.direction = new Vector(dirX, dirY);
}
public Vector getPosition() {
return this.position;
}
public Vector getDirection() {
return this.direction;
}
public void move(){
position.x += direction.x;
position.y += direction.y;
}
public void checkForWallCollisions() {
if (position.x <= 0) { // Left wall collision
position.x = 0; // Set the ball at the left edge
direction.x = Math.abs(direction.x); // Reverse the x direction
}
if (position.x + radius >= 680) { // Right wall collision
position.x = 680 - radius; // Set the ball at the right edge
direction.x = -Math.abs(direction.x); // Reverse the x direction
}
if (position.y <= 0) { // Top wall collision
position.y = 0; // Set the ball at the top edge
direction.y = Math.abs(direction.y); // Reverse the y direction
}
}
}
...@@ -12,7 +12,7 @@ import java.awt.Graphics2D; ...@@ -12,7 +12,7 @@ import java.awt.Graphics2D;
* *
* @author Suman * @author Suman
*/ */
class GameMapBuilder { public class GameMapBuilder {
public int map [][]; public int map [][];
public int brickWidth; public int brickWidth;
......
...@@ -6,6 +6,9 @@ import java.awt.event.ActionEvent; ...@@ -6,6 +6,9 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class GamePanel extends JPanel implements KeyListener, ActionListener { public class GamePanel extends JPanel implements KeyListener, ActionListener {
private boolean play = true; private boolean play = true;
...@@ -18,17 +21,17 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { ...@@ -18,17 +21,17 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener {
private Timer timer; private Timer timer;
private int delay = 8; private int delay = 8;
private int playerX = 310; private int paddle = 310;
private List<Ball> balls = new ArrayList<>(); // Use List interface instead of ArrayList
private Vector ballPosition;
private Vector ballDirection;
private String font = "MV Boli"; private String font = "MV Boli";
private GameMapBuilder map; private GameMapBuilder map;
public GamePanel() { public GamePanel() {
this.ballPosition = new Vector(120, 350); this.balls.add(new Ball(350, 450, 1, -1, 20)); // Adjust the initial ball position and direction
this.ballDirection = new Vector(-1, -2); this.balls.add(new Ball(380, 420, -1, 1, 20));
this.totalBricks = rows * columns; this.totalBricks = rows * columns;
map = new GameMapBuilder(rows, columns); map = new GameMapBuilder(rows, columns);
...@@ -41,7 +44,9 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { ...@@ -41,7 +44,9 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener {
protected void paintComponent(Graphics graphics) { protected void paintComponent(Graphics graphics) {
super.paintComponent(graphics); super.paintComponent(graphics);
if (balls.isEmpty()) {
gameOver(graphics, "Game Over", Color.BLACK);
}
// background color // background color
graphics.setColor(Color.green); graphics.setColor(Color.green);
graphics.fillRect(1, 1, 692, 592); graphics.fillRect(1, 1, 692, 592);
...@@ -53,31 +58,39 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { ...@@ -53,31 +58,39 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener {
graphics.fillRect(691, 0, 3, 592); graphics.fillRect(691, 0, 3, 592);
graphics.setColor(Color.blue); graphics.setColor(Color.blue);
graphics.fillRect(playerX, 550, 100, 12); graphics.fillRect(paddle, 550, 100, 12);
graphics.setColor(Color.RED); // ball color
Iterator<Ball> ballsIterator = balls.iterator();
while (ballsIterator.hasNext()) {
Ball ball = ballsIterator.next();
Vector ballPosition = ball.getPosition();
if (ballPosition.y > 570) {
ballsIterator.remove();
continue;
} else {
ball.checkForWallCollisions();
}
graphics.setColor(Color.RED); // ball color graphics.setColor(Color.RED); // ball color
graphics.fillOval(ballPosition.x, ballPosition.y, 20, 20); graphics.fillOval(ballPosition.x, ballPosition.y, 20, 20);
}
graphics.setColor(Color.black); graphics.setColor(Color.black);
graphics.setFont(new Font("MV Boli", Font.BOLD, 25)); graphics.setFont(new Font("MV Boli", Font.BOLD, 25));
graphics.drawString("Score: " + score, 520, 30); graphics.drawString("Score: " + score, 520, 30);
if (totalBricks <= 0) { // if all bricks are destroyed then you win if (totalBricks <= 0) {
Color color = new Color(0XFF6464); Color color = new Color(0XFF6464);
gameOver(graphics, "You Won", color); gameOver(graphics, "You Won", color);
} }
if (ballPosition.y > 570) { // if ball goes below the paddle then you lose
gameOver(graphics, "Game Over", Color.BLACK);
}
} }
public void gameOver(Graphics graphics, String gameOverText, Color color) { public void gameOver(Graphics graphics, String gameOverText, Color color) {
play = false; play = false;
ballDirection.x = 0;
ballDirection.y = 0;
graphics.setColor(color); graphics.setColor(color);
graphics.setFont(new Font(font, Font.BOLD, 30)); graphics.setFont(new Font(font, Font.BOLD, 30));
graphics.drawString(gameOverText + ", Your Score: " + score, 190, 300); graphics.drawString(gameOverText + ", Your Score: " + score, 190, 300);
...@@ -89,51 +102,44 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { ...@@ -89,51 +102,44 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener {
@Override @Override
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
if (play) { if (play) {
// Ball - Pedal interaction for (Ball ball : balls) {
if (new Rectangle(ballPosition.x, ballPosition.y, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) { Vector ballPosition = ball.getPosition();
Vector ballDirection = ball.getDirection();
if (new Rectangle(ballPosition.x, ballPosition.y, 20, 20).intersects(new Rectangle(paddle, 550, 100, 8))) {
ballDirection.y = -ballDirection.y; ballDirection.y = -ballDirection.y;
ball.setDirection(ballDirection.x, ballDirection.y);
} }
for (int i = 0; i < map.map.length; i++) { // Ball - Brick interaction
for (int j = 0; j < map.map[0].length; j++) { // map.map[0].length is the number of columns for (int i = 0; i < map.map.length; i++) {
for (int j = 0; j < map.map[0].length; j++) {
if (map.map[i][j] > 0) { if (map.map[i][j] > 0) {
int brickX = j * map.brickWidth; int brickX = j * map.brickWidth;
int brickY = i * map.brickHeight; int brickY = i * map.brickHeight;
int brickWidth = map.brickWidth; int brickWidth = map.brickWidth;
int brickHeight = map.brickHeight; int brickHeight = map.brickHeight;
Rectangle rect = new Rectangle(brickX, brickY, brickWidth, 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, 20, 20);
Rectangle brickRect = rect;
if (ballRect.intersects(brickRect)) { if (ballRect.intersects(brickRect)) {
map.setBrickValue(0, i, j); map.setBrickValue(0, i, j);
totalBricks--; totalBricks--;
score += 5; score += 5;
if (ballPosition.x + 19 <= brickRect.x || ballPosition.x + 1 >= brickRect.x + brickRect.width) if (ballPosition.x + 19 <= brickRect.x || ballPosition.x + 1 >= brickRect.x + brickRect.width) {
ballDirection.x = -ballDirection.x; ballDirection.x = -ballDirection.x;
else { } else {
ballDirection.y = -ballDirection.y; ballDirection.y = -ballDirection.y;
} }
} }
}
}
} }
ballPosition.x += ballDirection.x;
ballPosition.y += ballDirection.y;
if (ballPosition.x < 0) { // if ball hits the left wall then it bounces back
ballDirection.x = -ballDirection.x;
} }
if (ballPosition.y < 0) { // if ball hits the top wall then it bounces back
ballDirection.y = -ballDirection.y;
} }
if (ballPosition.x > 670) { // if ball hits the right wall then it bounces back ball.setDirection(ballDirection.x, ballDirection.y);
ballDirection.x = -ballDirection.x; ball.move(); // Call the move() method to update the ball's position
} }
} }
repaint(); repaint();
...@@ -141,57 +147,49 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { ...@@ -141,57 +147,49 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener {
@Override @Override
public void keyTyped(KeyEvent arg0) { public void keyTyped(KeyEvent arg0) {
} }
@Override @Override
public void keyPressed(KeyEvent arg0) { public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) { // if right arrow key is pressed then paddle moves right if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if (playerX >= 600) { if (paddle >= 600) {
playerX = 600; paddle = 600;
} else { } else {
moveRight(); moveRight();
} }
} }
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) { // if left arrow key is pressed then paddle moves left if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if (playerX < 10) { if (paddle < 10) {
playerX = 10; paddle = 10;
} else { } else {
moveLeft(); moveLeft();
} }
} }
if (arg0.getKeyCode() == KeyEvent.VK_ENTER) { // if enter key is pressed then game restarts if (arg0.getKeyCode() == KeyEvent.VK_ENTER) {
if (!play) { if (!play) {
play = true; play = true;
ballPosition.x = 120; this.balls.add(new Ball(350, 450, 1, -1, 20)); // Adjust the initial ball position and direction
ballPosition.y = 350;
ballDirection.x = -1;
ballDirection.y = -2;
score = 0; score = 0;
totalBricks = rows * columns;; totalBricks = rows * columns;
map = new GameMapBuilder(rows, columns); map = new GameMapBuilder(rows, columns);
repaint(); repaint();
} }
} }
} }
public void moveRight() { // paddle moves right by 50 pixels public void moveRight() {
play = true; play = true;
playerX += 50; paddle += 50;
} }
public void moveLeft() { // paddle moves left by 50 pixels public void moveLeft() {
play = true; play = true;
playerX -= 50; paddle -= 50;
} }
@Override @Override
public void keyReleased(KeyEvent arg0) { public void keyReleased(KeyEvent arg0) {
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment