Brick Breaker Game in Java
What is brick breaker game?
In the classic arcade game Brick Breaker, the player uses a paddle at the bottom of the screen to bounce a ball in the direction of a brick wall. The goal is to hit every brick with the ball without letting it drop below the paddle in order to break them all. The player's score rises as more bricks are broken. Either all the bricks are destroyed or the ball touches the paddle to end the game.
In a Java version of Brick Breaker, the game window, user input, and game objects like the paddle, ball, and bricks are typically created using graphics libraries like AWT (Abstract Window Toolkit) or Swing.
To control the movement of the paddle and ball, detect ball-brick collisions, and update the game state appropriately, you would also need to implement game logic.
Requirement of brick breaker game in java
You would typically require the following specifications to create a Brick Breaker game in Java:
- Java Development kit: if you want to develop a brick breaker game than first make sure you have the Java development kit is set up on computer. It offers the tools and libraries required to run and compile Java code.
- Integrated Development Environment: An integrated development environment (IDE), though not strictly necessary, can make the development process much simpler. Eclipse, IntelliJ IDEA, and NetBeans are a few well-known Java IDEs. Select an IDE with which you are familiar.
- Graphics Library: In order to create the game window, manage user input, and render the game elements, a graphics library is required. AWT (Abstract Window Toolkit) and Swing are two examples of Java libraries that you can use.
- Game Loop: Setting up a game loop is essential for handling user input, updating game state, and rendering graphics. The game loop makes sure the game is played continuously and smoothly.
- Collision Detection: To identify collisions between the ball, paddle, and bricks, you must implement collision detection logic. Determining when the ball hits a brick and should bounce off or when it drops below the paddle depends on this.
- Game Logic: Put the rules and logic of the game into practise, such as deciding when the game ends, updating the score, and monitoring the brick status.
- Sound Effects and Music (Optional): it is optional but if you want you can choose to include sound effects and background music to improve the gaming experience. For playing back audio, Java offers built-in libraries like Java Sound API or third-party libraries like JFugue.
These are the essential conditions for starting to create a Java Brick Breaker game. You might require additional libraries or tools depending on your specific implementation and desired features.
Features of brick breaker game
Different features can be added to Java brick-breaking games to improve the gameplay. The following are some typical features you might want to implement:
- Multiple Levels: Create the game with multiple, progressively harder levels. Each level may have a different brick arrangement or present fresh difficulties.
- Power-ups: Include power-up items in your game plan, which the paddle can collect to gain momentary advantages. Power-ups can give you extra features like expanding the size of the paddle, adding more balls, or allowing laser shots to smash through bricks.
- Different Brick Types: Introduce various brick types with a range of qualities. For instance, some bricks might need multiple strikes to break while others might be invulnerable or explode nearby bricks.
- Game Modes: Use various game modes to provide variety. For instance, you might include a timed mode where players must break as many bricks as they can in a set amount of time or a survival mode where they must survive as long as they can with a finite number of lives.
- Scoring and High Scores: Throughout the game, keep track of each player's score and display it on the screen. Implement a high score system to save and show the players' best scores.
- Sound effects: Music and sound effects should be added for actions like collecting power-ups, smashing bricks, and colliding with balls. To improve the game's ambiance and engagement, add background music.
- Visual effects: Add animations or other effects to improve the visual experience. For instance, you could implement seamless level transitions, add particle effects to the destruction of bricks, or add visual cues to the collection of power-ups.
Source code
import javax.swing.*; import java.awt.*; import java.awt.event.*; class MapGenerator { public int map [][]; public int brickWidth; public int brickHeight; // this creates the brick of size 3x7 public MapGenerator(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; } } class GamePlay 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 int ballposX = 120; private int ballposY = 350; private int ballXdir = -1; private int ballYdir = -2; private MapGenerator map; public GamePlay() { map = new MapGenerator(3, 7); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); timer = new Timer(delay, this); timer.start(); } public void paint(Graphics g) { //background color g.setColor(Color.YELLOW); g.fillRect(1, 1, 692, 592); map.draw((Graphics2D)g); g.fillRect(0, 0, 3, 592); g.fillRect(0, 0, 692, 3); g.fillRect(691, 0, 3, 592); g.setColor(Color.blue); g.fillRect(playerX, 550, 100, 12); g.setColor(Color.RED); // ball color g.fillOval(ballposX, ballposY, 20, 20); g.setColor(Color.black); g.setFont(new Font("MV Boli", Font.BOLD, 25)); g.drawString("Score: " + score, 520, 30); if (totalBricks <= 0) { // if all bricks are destroyed then you win play = false; ballXdir = 0; ballYdir = 0; g.setColor(new Color(0XFF6464)); g.setFont(new Font("MV Boli", Font.BOLD, 30)); g.drawString("You Won, Score: " + score, 190, 300); g.setFont(new Font("MV Boli", Font.BOLD, 20)); g.drawString("Press Enter to Restart.", 230, 350); } if(ballposY > 570) { // if ball goes below the paddle then you lose play = false; ballXdir = 0; ballYdir = 0; g.setColor(Color.BLACK); g.setFont(new Font("MV Boli", Font.BOLD, 30)); g.drawString("Game Over, Score: " + score, 190, 300); g.setFont(new Font("MV Boli", Font.BOLD, 20)); g.drawString("Press Enter to Restart", 230, 350); } g.dispose(); } @Override public void actionPerformed(ActionEvent arg0) { timer.start(); if(play) { // Ball - Pedal interaction if(new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) { ballYdir = - ballYdir; } for( int i = 0; i 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(ballposX, ballposY, 20,20); Rectangle brickRect = rect; if(ballRect.intersects(brickRect) ) { map.setBrickValue(0, i, j); totalBricks--; score+=5; if(ballposX + 19 <= brickRect.x || ballposX +1 >= brickRect.x + brickRect.width) ballXdir = -ballXdir; else { ballYdir = -ballYdir; } } } } } ballposX += ballXdir; ballposY += ballYdir; if(ballposX < 0) { // if ball hits the left wall then it bounces back ballXdir = -ballXdir; } if(ballposY < 0) { // if ball hits the top wall then it bounces back ballYdir = -ballYdir; } if(ballposX > 670) { // if ball hits the right wall then it bounces back ballXdir = -ballXdir; } } 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; ballposX = 120; ballposY = 350; ballXdir = -1; ballYdir = -2; score = 0; totalBricks = 21; map = new MapGenerator(3,7); 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) { } } class Main { public static void main(String[] args) { JFrame obj = new JFrame(); GamePlay gamePlay = new GamePlay(); obj.setBounds(10, 10, 700, 600); obj.setTitle("Brick Breaker"); obj.setResizable(false); obj.setVisible(true); obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); obj.add(gamePlay); } }
Output:
