Brick breaker java game

Here is a simple implementation of the classic game Brick Breaker in Java:

Game Description:

Brick Breaker is a popular arcade game where the player controls a paddle at the bottom of the screen and must break bricks at the top of the screen by bouncing a ball off the paddle. The game ends when all the bricks are broken or the ball hits the bottom of the screen.

Game Requirements:

Game Code:

Here is the game code in Java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class BrickBreaker extends JPanel implements ActionListener {
    // Game constants
    private static final int WIDTH = 400;
    private static final int HEIGHT = 600;
    private static final int PADDLE_WIDTH = 100;
    private static final int PADDLE_HEIGHT = 20;
    private static final int BALL_SIZE = 10;
    private static final int BRICK_WIDTH = 50;
    private static final int BRICK_HEIGHT = 20;
    private static final int BRICKS_PER_ROW = 10;
    private static final int BRICKS_PER_COLUMN = 5;
    private static final int BALL_SPEED = 5;
    private static final int PADDLE_SPEED = 5;

    // Game variables
    private Ball ball;
    private Paddle paddle;
    private List<Brick> bricks;
    private int score;
    private boolean gameOver;

    // Game constructor
    public BrickBreaker() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);

        // Initialize game variables
        ball = new Ball(WIDTH / 2, HEIGHT / 2, BALL_SIZE);
        paddle = new Paddle(PADDLE_WIDTH, PADDLE_HEIGHT, WIDTH / 2);
        bricks = new ArrayList<>();
        score = 0;
        gameOver = false;

        // Create bricks
        for (int i = 0; i < BRICKS_PER_COLUMN; i++) {
            for (int j = 0; j < BRICKS_PER_ROW; j++) {
                Brick brick = new Brick(BRICK_WIDTH, BRICK_HEIGHT, j * (BRICK_WIDTH + 5) + 5, i * (BRICK_HEIGHT + 5) + 5);
                bricks.add(brick);
            }
        }

        // Add event listeners
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    paddle.moveLeft();
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    paddle.moveRight();
                }
            }
        });
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (!gameOver) {
                    ball.setDirection(new Point(1, -1));
                }
            }
        });
    }

    // Game loop
    public void actionPerformed(ActionEvent e) {
        if (!gameOver) {
            // Update game state
            ball.update();
            paddle.update();
            for (Brick brick : bricks) {
                brick.update();
            }

            // Check collisions
            if (ball.getRect().intersects(paddle.getRect())) {
                ball.setDirection(new Point(1, -1));
            }
            for (Brick brick : bricks) {
                if (ball.getRect().intersects(brick.getRect())) {
                    ball.setDirection(new Point(-1, -1));
                    score++;
                    bricks.remove(brick);
                }
            }

            // Check game over
            if (ball.getRect().y > HEIGHT) {
                gameOver = true;
            }
        }

        // Repaint the game
        repaint();
    }

    // Paint the game
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);

        // Draw paddle
        g.fillRect(paddle.getX(), paddle.getY(), paddle.getWidth(), paddle.getHeight());

        // Draw ball
        g.fillOval(ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());

        // Draw bricks
        for (Brick brick : bricks) {
            g.fillRect(brick.getX(), brick.getY(), brick.getWidth(), brick.getHeight());
        }

        // Draw score
        g.drawString("Score: " + score, 10, 20);

        // Draw game over message
        if (gameOver) {
            g.drawString("Game Over!", WIDTH / 2 - 50, HEIGHT / 2);
        }
    }

    // Main method
    public static void main(String[] args) {
        JFrame frame = new JFrame("Brick Breaker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new BrickBreaker());
        frame.pack();
        frame.setVisible(true);
    }
}

// Ball class
class Ball {
    private int x;
    private int y;
    private int width;
    private int height;
    private Point direction;

    public Ball(int x, int y, int size) {
        this.x = x;
        this.y = y;
        this.width = size;
        this.height = size;
        this.direction = new Point(1, -1);
    }

    public void update() {
        x += direction.x * BALL_SPEED;
        y += direction.y * BALL_SPEED;

        if (x < 0 || x + width > BrickBreaker.WIDTH) {
            direction.x *= -1;
        }
        if (y < 0 || y + height > BrickBreaker.HEIGHT) {
            direction.y *= -1;
        }
    }

    public Rectangle getRect() {
        return new Rectangle(x, y, width, height);
    }

    public void setDirection(Point direction) {
        this.direction = direction;
    }
}

// Paddle class
class Paddle {
    private int x;
    private int y;
    private int width;
    private int height;

    public Paddle(int width, int height, int x) {
        this.x = x;
        this.y = BrickBreaker.HEIGHT - height;
        this.width = width;
        this.height = height;
    }

    public void update() {
        if (x > 0 && x + width < BrickBreaker.WIDTH) {
            x += PADDLE_SPEED;
        }
    }

    public void moveLeft() {
        x -= PADDLE_SPEED;
    }

    public void moveRight() {
        x += PADDLE_SPEED;
    }

    public Rectangle getRect() {
        return new Rectangle(x, y, width, height);
    }
}

// Brick class
class Brick {
    private int x;
    private int y;
    private int width;
    private int height;

    public Brick(int width, int height, int x, int y) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public void update() {
        // No-op
    }

    public Rectangle getRect() {
        return new Rectangle(x, y, width, height);
    }
}

This code creates a BrickBreaker panel that contains a Ball, a Paddle, and a list of Brick objects. The game loop updates the game state, checks for collisions, and repaints the game. The Ball class updates its position and direction based on the game state, and the Paddle class updates its position based on user input.

To run the game, simply compile the code and run the main method. You can use the arrow keys to move the paddle and the mouse to start the game.

Note that this is a basic implementation of the game, and you may want to add additional features such as sound effects, smoother animations, and more levels.