diff --git a/app/src/main/java/brickbreaker/App.java b/app/src/main/java/brickbreaker/App.java index f35e67e03621c30e569ce7c684ec229ecdd7ecd7..4ab69aee324fe4900bbadee34b5b1c9debe34516 100644 --- a/app/src/main/java/brickbreaker/App.java +++ b/app/src/main/java/brickbreaker/App.java @@ -3,12 +3,17 @@ */ package brickbreaker; -public class App { - public String getGreeting() { - return "Hello World!"; - } +import javax.swing.JFrame; +public class App { public static void main(String[] args) { - System.out.println(new App().getGreeting()); + 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 0000000000000000000000000000000000000000..f946281ee346d71d59d365e836fe9f3c15800f60 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..eee55338ebf3dc32c85873916e9bbefc848e4cd9 --- /dev/null +++ b/app/src/main/java/brickbreaker/GamePanel.java @@ -0,0 +1,194 @@ +package brickbreaker; + +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 new file mode 100644 index 0000000000000000000000000000000000000000..4dd910a47555330673ec7e04b035377dd3315113 --- /dev/null +++ b/app/src/main/java/brickbreaker/LoginFrame.java @@ -0,0 +1,86 @@ +package brickbreaker; + +/** + * + * @author Suman + */ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class LoginFrame extends JFrame implements ActionListener { + + Container container=getContentPane(); + JLabel userLabel=new JLabel("USERNAME"); + JLabel passwordLabel=new JLabel("PASSWORD"); + JTextField userNameTextField=new JTextField(); + JPasswordField passwordField=new JPasswordField(); + JButton loginButton=new JButton("LOGIN"); + JButton registerButton=new JButton("REGISTER"); + + + LoginFrame() + { + //Calling methods inside constructor. + setLayoutManager(); + setLocationAndSize(); + addComponentsToContainer(); + addActionListeners(); + } + public void setLayoutManager() + { + container.setLayout(null); + } + public void setLocationAndSize() + { + //Setting location and Size of each components using setBounds() method. + userLabel.setBounds(50,150,100,30); + passwordLabel.setBounds(50,220,100,30); + userNameTextField.setBounds(150,150,150,30); + passwordField.setBounds(150,220,150,30); + loginButton.setBounds(50,300,100,30); + registerButton.setBounds(200,300,100,30); + + + } + public void addComponentsToContainer() + { + //Adding each components to the Container + container.add(userLabel); + container.add(passwordLabel); + container.add(userNameTextField); + container.add(passwordField); + container.add(loginButton); + container.add(registerButton); + } + + public void addActionListeners() { + loginButton.addActionListener(this); + registerButton.addActionListener(this); + } + + + @Override + public void actionPerformed(ActionEvent e) { + String actionCommand = e.getActionCommand(); + switch(actionCommand) { + case "LOGIN": + handleLogin(); + break; + + case "REGISTER": + handleRegister(); + } + } + + private void handleLogin() { + String password = new String(passwordField.getPassword()); + String userName = userNameTextField.getText(); + System.out.println("user Nmae: " + userName + " password: " + password); + } + + private void handleRegister() { + System.out.println("brickbreaker.LoginFrame.handleRegister()"); + } +} diff --git a/app/src/main/java/brickbreaker/Vector.java b/app/src/main/java/brickbreaker/Vector.java new file mode 100644 index 0000000000000000000000000000000000000000..258cb988cb4cd26a28f93d46c72bc673e3edbebd --- /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; + } +}