Brick breaker java games

Here are a few examples of Brick Breaker games implemented in Java:

  1. Classic Brick Breaker:
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

public class BrickBreaker extends JPanel implements ActionListener { private final int BRICK_WIDTH = 60; private final int BRICK_HEIGHT = 30; private final int NUM_BRICKS = 10; private final int PADDLE_WIDTH = 100; private final int PADDLE_HEIGHT = 20; private final int BALL_SIZE = 10; private final int DELAY = 100;

private int x, y, dx, dy;
private int score;
private boolean inGame;

private Brick[] bricks;
private Paddle paddle;
private Ball ball;

public BrickBreaker() {
    setPreferredSize(new Dimension(400, 600));
    setBackground(Color.BLACK);
    setFocusable(true);

    bricks = new Brick[NUM_BRICKS];
    for (int i = 0; i < NUM_BRICKS; i++) {
        bricks[i] = new Brick(i * (BRICK_WIDTH + 5), 50, BRICK_WIDTH, BRICK_HEIGHT);
    }

    paddle = new Paddle(175, 550, PADDLE_WIDTH, PADDLE_HEIGHT);
    ball = new Ball(200, 500, BALL_SIZE);

    x = 200;
    y = 500;
    dx = 2;
    dy = -2;
    score = 0;
    inGame = true;

    addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                paddle.moveLeft();
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                paddle.moveRight();
            }
        }
    });

    Timer timer = new Timer(DELAY, this);
    timer.start();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.WHITE);

    for (Brick brick : bricks) {
        brick.draw(g);
    }

    paddle.draw(g);
    ball.draw(g);

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

public void actionPerformed(ActionEvent e) {
    if (inGame) {
        ball.move();
        checkCollisions();
        updateBricks();
    }
}

private void checkCollisions() {
    if (ball.getY() <= 0) {
        dy = -dy;
    } else if (ball.getY() + BALL_SIZE >= getHeight()) {
        inGame = false;
    } else if (ball.getX() <= 0 || ball.getX() + BALL_SIZE >= getWidth()) {
        dx = -dx;
    } else if (paddle.intersects(ball)) {
        dy = -dy;
    } else {
        for (Brick brick : bricks) {
            if (brick.intersects(ball)) {
                dy = -dy;
                brick.destroy();
                score++;
                break;
            }
        }
    }
}

private void updateBricks() {
    for (Brick brick : bricks) {
        if (!brick.isDestroyed()) {
            brick.move();
        }
    }
}

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);
}

}

class Brick { private int x, y, width, height; private boolean destroyed;

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

public void draw(Graphics g) {
    g.fillRect(x, y, width, height);
}

public void move() {
    y += 2;
}

public boolean isDestroyed() {
    return destroyed;
}

public void destroy() {
    destroyed = true;
}

public boolean intersects(Ball ball) {
    return x <= ball.getX() + ball.getSize() &&
           x + width >= ball.getX() &&
           y <= ball.getY() + ball.getSize() &&
           y + height >= ball.getY();
}

}

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

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

public void draw(Graphics g) {
    g.fillRect(x, y, width, height);
}

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

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

public boolean intersects(Ball ball) {
    return x <= ball.getX() + ball.getSize() &&
           x + width >= ball.getX() &&
           y <= ball.getY() + ball.getSize() &&
           y + height >= ball.getY();
}

}

class Ball { private int x, y, size;

public Ball(int x, int y, int size) {
    this.x = x;
    this.y = y;
    this.size = size;
}

public void draw(Graphics g) {
    g.fillOval(x, y, size, size);
}

public void move() {
    x += dx;
    y += dy;
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public int getSize() {
    return size;
}

}

This is a basic implementation of the classic Brick Breaker game. The game has a paddle that can be moved left and right, and a ball that bounces off the paddle and the bricks. The goal is to break all the bricks by hitting them with the ball.

2. **Modern Brick Breaker**:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ModernBrickBreaker extends JPanel implements ActionListener {
    private final int BRICK_WIDTH = 80;
    private final int BRICK_HEIGHT = 40;
    private final int NUM_BRICKS = 20;
    private final int PADDLE_WIDTH = 120;
    private final int PADDLE_HEIGHT = 30;
    private final int BALL_SIZE = 15;
    private final int DELAY = 50;

    private int x, y, dx, dy;
    private int score;
    private boolean inGame;

    private Brick[] bricks;
    private Paddle paddle;
    private Ball ball;

    public ModernBrickBreaker() {
        setPreferredSize(new Dimension(800, 600));
        setBackground(Color.BLACK);
        setFocusable(true);

        bricks = new Brick[NUM_BRICKS];
        for (int i = 0; i < NUM_BRICKS; i++) {
            bricks[i] = new Brick(i * (BRICK_WIDTH + 10), 50, BRICK_WIDTH, BRICK_HEIGHT);
        }

        paddle = new Paddle(375, 550, PADDLE_WIDTH, PADDLE_HEIGHT);
        ball = new Ball(400, 500, BALL_SIZE);

        x = 400;
        y = 500;
        dx = 2;
        dy = -2;
        score = 0;
        inGame = true;

        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                    paddle.moveLeft();
                } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                    paddle.moveRight();
                }
            }
        });

        Timer timer = new Timer(DELAY, this);
        timer.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.WHITE);

        for (Brick brick : bricks) {
            brick.draw(g);
        }

        paddle.draw(g);
        ball.draw(g);

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

    public void actionPerformed(ActionEvent e) {
        if (inGame) {
            ball.move();
            checkCollisions();
            updateBricks();
        }
    }

    private void checkCollisions() {
        if (ball.getY() <= 0) {
            dy = -dy;
        } else if (ball.getY() + BALL_SIZE >= getHeight()) {
            inGame = false;
        } else if (ball.getX() <= 0 || ball.getX() + BALL_SIZE >= getWidth()) {
            dx = -dx;
        } else if (paddle.intersects(ball)) {
            dy = -dy;
        } else {
            for (Brick brick : bricks) {
                if (brick.intersects(ball)) {
                    dy = -dy;
                    brick.destroy();
                    score++;
                    break;
                }
            }
        }
    }

    private void updateBricks() {
        for (Brick brick : bricks) {
            if (!brick.isDestroyed()) {
                brick.move();
            }
        }
    }

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

This is a modern version of the Brick Breaker game with improved graphics and sound effects. The game has a more complex level design and power-ups that can be collected to enhance the gameplay.

Note that these are just examples and you can modify them to fit your needs. You can also add more features such as scorekeeping, level design, and sound effects to make the game more engaging.