From 80adfca54cfc9c27600a9725f4e9bfa29b43f511 Mon Sep 17 00:00:00 2001 From: Samita Adhikari <sadhika7@myune.edu.au> Date: Tue, 19 Sep 2023 23:47:05 +0545 Subject: [PATCH] Basic Game logic Implementation --- app/src/main/java/brickbreaker/App.java | 14 +- .../java/brickbreaker/GameMapBuilder.java | 56 +++++ app/src/main/java/brickbreaker/GamePanel.java | 199 +++++++++++++++++- .../main/java/brickbreaker/LoginFrame.java | 4 - app/src/main/java/brickbreaker/Vector.java | 19 ++ 5 files changed, 273 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/brickbreaker/GameMapBuilder.java create mode 100644 app/src/main/java/brickbreaker/Vector.java diff --git a/app/src/main/java/brickbreaker/App.java b/app/src/main/java/brickbreaker/App.java index 1a2f5fb..4ab69ae 100644 --- a/app/src/main/java/brickbreaker/App.java +++ b/app/src/main/java/brickbreaker/App.java @@ -7,11 +7,13 @@ import javax.swing.JFrame; public class App { public static void main(String[] args) { - LoginFrame frame=new LoginFrame(); - frame.setTitle("Register || Brick Breaker"); - frame.setVisible(true); - frame.setBounds(10,10,370,600); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - frame.setResizable(false); + JFrame obj = new JFrame(); + GamePanel gamePanel = new GamePanel(); + obj.setBounds(10, 10, 700, 600); + obj.setTitle("Brick Breaker"); + obj.setResizable(false); + obj.setVisible(true); + obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + obj.add(gamePanel); } } diff --git a/app/src/main/java/brickbreaker/GameMapBuilder.java b/app/src/main/java/brickbreaker/GameMapBuilder.java new file mode 100644 index 0000000..f946281 --- /dev/null +++ b/app/src/main/java/brickbreaker/GameMapBuilder.java @@ -0,0 +1,56 @@ +/* + * 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; + +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Graphics2D; + +/** + * + * @author Suman + */ +class GameMapBuilder { + + public int map [][]; + public int brickWidth; + public int brickHeight; + + // 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 = 540/col; + brickHeight = 150/row; + } + + // 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(0XFF8787)); // brick color + g.fillRect(j*brickWidth + 80, i*brickHeight + 50, brickWidth, brickHeight); + + g.setStroke(new BasicStroke(4)); + g.setColor(Color.BLACK); + g.drawRect(j*brickWidth + 80, i*brickHeight + 50, brickWidth, brickHeight); + } + } + + } + } + + // 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; + } + +} \ 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 f1e6ec7..eee5533 100644 --- a/app/src/main/java/brickbreaker/GamePanel.java +++ b/app/src/main/java/brickbreaker/GamePanel.java @@ -1,13 +1,194 @@ -/* - * 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 GamePanel { +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +public class GamePanel extends JPanel implements KeyListener, ActionListener { + private boolean play = true; + private int score = 0; + + private int totalBricks = 21; + + private Timer timer; + private int delay = 8; + + private int playerX = 310; + + private Vector ballPosition; + private Vector ballDirection; + private String font = "MV Boli"; + + private GameMapBuilder map; + + public GamePanel() { + this.ballPosition = new Vector(120, 350); + this.ballDirection = new Vector(-1, -2); + + map = new GameMapBuilder(4, 8); + addKeyListener(this); + setFocusable(true); + setFocusTraversalKeysEnabled(false); + timer = new Timer(delay, this); + timer.start(); + } + + protected void paintComponent(Graphics graphics) { + super.paintComponent(graphics); + + // background color + graphics.setColor(Color.green); + graphics.fillRect(1, 1, 692, 592); + + 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(playerX, 550, 100, 12); + + graphics.setColor(Color.RED); // ball color + graphics.fillOval(ballPosition.x, ballPosition.y, 20, 20); + + graphics.setColor(Color.black); + graphics.setFont(new Font("MV Boli", Font.BOLD, 25)); + graphics.drawString("Score: " + score, 520, 30); + + if (totalBricks <= 0) { // if all bricks are destroyed then you win + + Color color = new Color(0XFF6464); + 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) { + play = false; + ballDirection.x = 0; + ballDirection.y = 0; + graphics.setColor(color); + graphics.setFont(new Font(font, Font.BOLD, 30)); + graphics.drawString(gameOverText + ", Your Score: " + score, 190, 300); + + graphics.setFont(new Font(font, Font.BOLD, 20)); + graphics.drawString("Press Enter to Restart", 230, 350); + } + @Override + public void actionPerformed(ActionEvent arg0) { + if (play) { + // Ball - Pedal interaction + if (new Rectangle(ballPosition.x, ballPosition.y, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) { + ballDirection.y = -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 + if (map.map[i][j] > 0) { + int brickX = j * map.brickWidth + 80; + int brickY = i * map.brickHeight + 50; + int brickWidth = map.brickWidth; + int brickHeight = map.brickHeight; + + Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight); + Rectangle ballRect = new Rectangle(ballPosition.x, ballPosition.y, 20, 20); + Rectangle brickRect = rect; + + if (ballRect.intersects(brickRect)) { + map.setBrickValue(0, i, j); + totalBricks--; + score += 5; + + if (ballPosition.x + 19 <= brickRect.x || ballPosition.x + 1 >= brickRect.x + brickRect.width) + ballDirection.x = -ballDirection.x; + else { + 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 + ballDirection.x = -ballDirection.x; + } + + } + + repaint(); + } + + @Override + public void keyTyped(KeyEvent arg0) { + + } + + @Override + public void keyPressed(KeyEvent arg0) { + if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) { // if right arrow key is pressed then paddle moves right + if (playerX >= 600) { + playerX = 600; + } else { + moveRight(); + + } + } + if (arg0.getKeyCode() == KeyEvent.VK_LEFT) { // if left arrow key is pressed then paddle moves left + if (playerX < 10) { + playerX = 10; + } else { + moveLeft(); + + } + } + + if (arg0.getKeyCode() == KeyEvent.VK_ENTER) { // if enter key is pressed then game restarts + if (!play) { + play = true; + ballPosition.x = 120; + ballPosition.y = 350; + ballDirection.x = -1; + ballDirection.y = -2; + score = 0; + totalBricks = 21; + map = new GameMapBuilder(7, 8); + + repaint(); + } + } + + } + + public void moveRight() { // paddle moves right by 50 pixels + play = true; + playerX += 50; + } + + public void moveLeft() { // paddle moves left by 50 pixels + play = true; + playerX -= 50; + } + + @Override + public void keyReleased(KeyEvent arg0) { + + } } diff --git a/app/src/main/java/brickbreaker/LoginFrame.java b/app/src/main/java/brickbreaker/LoginFrame.java index 71252a9..4dd910a 100644 --- a/app/src/main/java/brickbreaker/LoginFrame.java +++ b/app/src/main/java/brickbreaker/LoginFrame.java @@ -1,7 +1,3 @@ -/* - * 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; /** diff --git a/app/src/main/java/brickbreaker/Vector.java b/app/src/main/java/brickbreaker/Vector.java new file mode 100644 index 0000000..258cb98 --- /dev/null +++ b/app/src/main/java/brickbreaker/Vector.java @@ -0,0 +1,19 @@ +/* + * 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 Vector { + public int x; + public int y; + + public Vector(int x, int y) { + this.x = x; + this.y = y; + } +} -- GitLab