0% found this document useful (0 votes)
49 views7 pages

Brick Breaker Game

This Java code defines a Brick Breaker game. It initializes variables for game objects like the paddle, ball and bricks. It includes methods for drawing the game, handling input and updating the game state over time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

Brick Breaker Game

This Java code defines a Brick Breaker game. It initializes variables for game objects like the paddle, ball and bricks. It includes methods for drawing the game, handling input and updating the game state over time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

import javax.swing.

*;

import java.awt.*;

import java.awt.event.*;

public class BrickBreaker extends JFrame implements ActionListener {

private static final int WIDTH = 800;

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 = 20;

private static final int BRICK_ROWS = 5;

private static final int BRICK_COLS = 10;

private static final int BRICK_WIDTH = 70;

private static final int BRICK_HEIGHT = 30;

private static final int INITIAL_SCORE = 0;

private Timer timer;

private int paddleX;

private int ballX, ballY, ballXSpeed, ballYSpeed;

private int score;

private boolean gameStarted;

private boolean gameOver;

private boolean bricks[][];


public BrickBreaker() {

setTitle("Brick Breaker");

setSize(WIDTH, HEIGHT);

setResizable(false);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

paddleX = WIDTH / 2 - PADDLE_WIDTH / 2;

ballX = WIDTH / 2 - BALL_SIZE / 2;

ballY = HEIGHT - PADDLE_HEIGHT - BALL_SIZE;

ballXSpeed = 3;

ballYSpeed = -3;

score = INITIAL_SCORE;

gameStarted = false;

gameOver = false;

bricks = new boolean[BRICK_ROWS][BRICK_COLS];

addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_LEFT) {

if (paddleX > 0) {

paddleX -= 20;

} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {


if (paddleX < WIDTH - PADDLE_WIDTH) {

paddleX += 20;

} else if (e.getKeyCode() == KeyEvent.VK_SPACE) {

if (!gameStarted) {

gameStarted = true;

timer.start();

});

timer = new Timer(10, this);

setVisible(true);

public void paint(Graphics g) {

super.paint(g);

g.setColor(Color.BLACK);

g.fillRect(0, 0, WIDTH, HEIGHT);

if (gameOver) {

g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 30));

g.drawString("Game Over", WIDTH / 2 - 100, HEIGHT / 2);

g.setFont(new Font("Arial", Font.BOLD, 20));

g.drawString("Press Space to Restart", WIDTH / 2 - 120, HEIGHT / 2 + 30);

return;

g.setColor(Color.WHITE);

g.fillRect(paddleX, HEIGHT - PADDLE_HEIGHT, PADDLE_WIDTH, PADDLE_HEIGHT);

g.setColor(Color.RED);

g.fillOval(ballX, ballY, BALL_SIZE, BALL_SIZE);

for (int row = 0; row < BRICK_ROWS; row++) {

for (int col = 0; col < BRICK_COLS; col++) {

if (bricks[row][col]) {

g.setColor(Color.GREEN);

g.fillRect(col * BRICK_WIDTH, row * BRICK_HEIGHT, BRICK_WIDTH, BRICK_HEIGHT);

g.setColor(Color.WHITE);

g.setFont(new Font("Arial", Font.BOLD, 20));

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


}

public void actionPerformed(ActionEvent e) {

if (gameOver) {

return;

ballX += ballXSpeed;

ballY += ballYSpeed;

if (ballX < 0 || ballX > WIDTH - BALL_SIZE) {

ballXSpeed = -ballXSpeed;

if (ballY < 0) {

ballYSpeed = -ballYSpeed;

if (ballY > HEIGHT - PADDLE_HEIGHT - BALL_SIZE) {

if (ballX >= paddleX && ballX <= paddleX + PADDLE_WIDTH) {

ballYSpeed = -ballYSpeed;

score++;

} else {

gameOver = true;

}
}

for (int row = 0; row < BRICK_ROWS; row++) {

for (int col = 0; col < BRICK_COLS; col++) {

if (bricks[row][col]) {

Rectangle brickRect = new Rectangle(col * BRICK_WIDTH, row * BRICK_HEIGHT,


BRICK_WIDTH, BRICK_HEIGHT);

Rectangle ballRect = new Rectangle(ballX, ballY, BALL_SIZE, BALL_SIZE);

if (brickRect.intersects(ballRect)) {

bricks[row][col] = false;

ballYSpeed = -ballYSpeed;

score++;

if (score == BRICK_ROWS * BRICK_COLS) {

gameOver = true;

repaint();

public static void main(String[] args) {


new BrickBreaker();

You might also like