diff --git a/app/src/main/java/brickbreaker/Ball.java b/app/src/main/java/brickbreaker/Ball.java index 24987c141fc926c744d30a45f809c873f3930943..3cc1a80a41117588f3eed7890d8c6c484debec03 100644 --- a/app/src/main/java/brickbreaker/Ball.java +++ b/app/src/main/java/brickbreaker/Ball.java @@ -4,6 +4,9 @@ */ package brickbreaker; +import java.awt.Color; +import java.awt.Graphics; + /** * * @author Suman @@ -12,11 +15,13 @@ public class Ball { private Vector position; private Vector direction; public int radius; + public Color color; - public Ball(int posX, int posY, int dirX, int dirY, int radius) { + public Ball(int posX, int posY, int dirX, int dirY, int radius,Color color) { this.position = new Vector(posX, posY); this.direction = new Vector(dirX, dirY); this.radius = radius; + this.color = color; } public void setPosition(int posX, int posY) { @@ -55,6 +60,10 @@ public class Ball { position.y = 0; // Set the ball at the top edge direction.y = Math.abs(direction.y); // Reverse the y direction } - + } + + public void draw(Graphics graphics) { + graphics.setColor(this.color); // ball color + graphics.fillOval(position.x, position.y, radius, radius); } } diff --git a/app/src/main/java/brickbreaker/GameComponents/Brick.java b/app/src/main/java/brickbreaker/GameComponents/Brick.java new file mode 100644 index 0000000000000000000000000000000000000000..5c03328ae0a154f8d76c8e2420ad846b0b395a2f --- /dev/null +++ b/app/src/main/java/brickbreaker/GameComponents/Brick.java @@ -0,0 +1,37 @@ +/* + * 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.GameComponents; + +import brickbreaker.Vector; +import java.awt.Color; +import java.awt.Graphics2D; + +/** + * + * @author Suman + */ +public class Brick { + private Vector position; + private int width; + private int height; + private Color color; + private Boolean hasPowerUp; + + public Brick(int x, int y, int width, int height, Color color, Boolean hasPowerUp) { + this.position = new Vector(x, y); + this.width = width; + this.height = height; + this.color = color; + this.hasPowerUp = hasPowerUp; + } + + public void draw(Graphics2D g) { + g.setColor(this.color); // brick color + g.fillRect(this.position.x, this.position.y, width, height); + + g.setColor(Color.BLACK); + g.drawRect(this.position.x, this.position.y, width, height); + } +} diff --git a/app/src/main/java/brickbreaker/GameComponents/DefaultBrickFactory.java b/app/src/main/java/brickbreaker/GameComponents/DefaultBrickFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..a086497915ea29eaa5f346db78717e6b7205c463 --- /dev/null +++ b/app/src/main/java/brickbreaker/GameComponents/DefaultBrickFactory.java @@ -0,0 +1,21 @@ +/* + * 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.GameComponents; + +import brickbreaker.GameComponents.Brick; +import brickbreaker.Interfaces.BrickFactory; +import java.awt.Color; + +/** + * + * @author Suman + */ +public class DefaultBrickFactory implements BrickFactory { + @Override + public Brick createBrick(int x, int y, int width, int height, Boolean hasPowerUp) { + Color color = Color.decode("#FF0000"); + return new Brick(x, y, width, height, color, hasPowerUp); + } +} \ No newline at end of file diff --git a/app/src/main/java/brickbreaker/GameMapBuilder.java b/app/src/main/java/brickbreaker/GameMapBuilder.java index 2ebb2f2928442e885ac53cf5160521865e1526f8..3b6562e4b87d12743c7916572ee3c3f81a38b2c2 100644 --- a/app/src/main/java/brickbreaker/GameMapBuilder.java +++ b/app/src/main/java/brickbreaker/GameMapBuilder.java @@ -4,6 +4,8 @@ */ package brickbreaker; +import brickbreaker.GameComponents.Brick; +import brickbreaker.Interfaces.BrickFactory; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; @@ -17,39 +19,44 @@ public class GameMapBuilder { public int map [][]; public int brickWidth; public int brickHeight; - + private BrickFactory brickfactory; + private final int maxNoOfPowerUps = 5; // this creates the brick of size 4x8 - public GameMapBuilder(int row, int col) { - map = new int [row][col]; - for (int i = 0; i < map.length; i++) { - for (int j=0; j< map[0].length;j++) { - map[i][j] = 1; - } - } - - brickWidth = 50; - brickHeight = 20; + public GameMapBuilder(int row, int col, BrickFactory brickFactory) { + map = new int [row][col]; + for (int i = 0; i < map.length; i++) { + for (int j=0; j< map[0].length;j++) { + map[i][j] = 1; + } + } + brickWidth = 50; + brickHeight = 20; + this.brickfactory = brickFactory; } // this draws the bricks public void draw(Graphics2D g) { - for (int i = 0; i < map.length; i++) { - for (int j=0; j< map[0].length;j++) { - if(map[i][j] > 0) { - g.setColor(new Color(0X0BB5FB)); // brick color - g.fillRect(j*brickWidth, i*brickHeight, brickWidth, brickHeight); - - g.setColor(Color.BLACK); - g.drawRect(j*brickWidth, i*brickHeight, brickWidth, brickHeight); - } - } - - } + + int numberOfPowerUps = (int) (Math.random() * maxNoOfPowerUps ); + for (int i = 0; i < map.length; i++) { + for (int j=0; j< map[0].length;j++) { + if(map[i][j] > 0) { + + boolean hasPowerUp = false; + if(numberOfPowerUps > 0) { + hasPowerUp = Math.random() < 0.5; + } + if(hasPowerUp) numberOfPowerUps--; + Brick brick = brickfactory.createBrick(j*brickWidth, i*brickHeight, brickWidth, brickHeight, hasPowerUp); + brick.draw(g); + } + } + } } // this sets the value of brick to 0 if it is hit by the ball public void setBrickValue(int value, int row, int col) { - map[row][col] = value; + map[row][col] = value; } } \ No newline at end of file diff --git a/app/src/main/java/brickbreaker/GamePanel.java b/app/src/main/java/brickbreaker/GamePanel.java index 2bc524fdf8e3170927d7ef005a75c12031873aa9..9dc12eee9e0e1054f323ff8b01f449f6fb9d14d3 100644 --- a/app/src/main/java/brickbreaker/GamePanel.java +++ b/app/src/main/java/brickbreaker/GamePanel.java @@ -1,5 +1,8 @@ package brickbreaker; +import brickbreaker.Interfaces.BrickFactory; +import brickbreaker.Interfaces.GameObserver; +import brickbreaker.GameComponents.DefaultBrickFactory; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; @@ -30,11 +33,14 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { private GameMapBuilder map; 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, 50)); + Color color = Color.decode("#8B4513"); + this.balls.add(new Ball(350, 450, 2, -2, 20, color)); // Adjust the initial ball position and direction + color = Color.decode("#008080"); + this.balls.add(new Ball(380, 420, -2, 2, 50, color)); this.totalBricks = rows * columns; - map = new GameMapBuilder(rows, columns); + BrickFactory brickfactory = new DefaultBrickFactory(); + map = new GameMapBuilder(rows, columns, brickfactory); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); @@ -42,6 +48,16 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { timer.start(); } + private List<GameObserver> observers = new ArrayList<>(); + + public void addObserver(GameObserver observer) { + observers.add(observer); + } + + public void removeObserver(GameObserver observer) { + observers.remove(observer); + } + protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); if (balls.isEmpty()) { @@ -53,14 +69,10 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { map.draw((Graphics2D) graphics); - graphics.fillRect(0, 0, 3, 592); - graphics.fillRect(0, 0, 692, 3); - graphics.fillRect(691, 0, 3, 592); graphics.setColor(Color.blue); graphics.fillRect(paddle, 550, 100, 20); - graphics.setColor(Color.RED); // ball color Iterator<Ball> ballsIterator = balls.iterator(); while (ballsIterator.hasNext()) { @@ -74,8 +86,7 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { ball.checkForWallCollisions(); } - graphics.setColor(Color.RED); // ball color - graphics.fillOval(ballPosition.x, ballPosition.y, ball.radius, ball.radius); + ball.draw(graphics); } graphics.setColor(Color.black); @@ -173,10 +184,12 @@ public class GamePanel extends JPanel implements KeyListener, ActionListener { if (arg0.getKeyCode() == KeyEvent.VK_ENTER) { if (!play) { play = true; - this.balls.add(new Ball(350, 450, 1, -1, 20)); // Adjust the initial ball position and direction + Color color = Color.decode("#8B4513"); + this.balls.add(new Ball(350, 450, 1, -1, 20, color)); // Adjust the initial ball position and direction score = 0; totalBricks = rows * columns; - map = new GameMapBuilder(rows, columns); + BrickFactory brickfactory = new DefaultBrickFactory(); + map = new GameMapBuilder(rows, columns, brickfactory); repaint(); } diff --git a/app/src/main/java/brickbreaker/Interfaces/BrickFactory.java b/app/src/main/java/brickbreaker/Interfaces/BrickFactory.java new file mode 100644 index 0000000000000000000000000000000000000000..ba3cedf49965393e114c2bcd82789b8ef0849bbe --- /dev/null +++ b/app/src/main/java/brickbreaker/Interfaces/BrickFactory.java @@ -0,0 +1,16 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package brickbreaker.Interfaces; + +import brickbreaker.GameComponents.Brick; +import java.awt.Color; + +/** + * + * @author Suman + */ +public interface BrickFactory { + Brick createBrick(int x, int y, int width, int height, Boolean hasPowerup); +} diff --git a/app/src/main/java/brickbreaker/Interfaces/GameObserver.java b/app/src/main/java/brickbreaker/Interfaces/GameObserver.java new file mode 100644 index 0000000000000000000000000000000000000000..e30d97edfd0ee602fcef7ab61fee8d9ee2e0c55f --- /dev/null +++ b/app/src/main/java/brickbreaker/Interfaces/GameObserver.java @@ -0,0 +1,14 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template + */ +package brickbreaker.Interfaces; + +/** + * + * @author Suman + */ +public interface GameObserver { + void onGameOver(int finalScore); + void onGameWin(int finalScore); +}