Brick Breaker Game
Brick Breaker Game
Brick Breaker Game
*;
import java.awt.*;
import java.awt.event.*;
setTitle("Brick Breaker");
setSize(WIDTH, HEIGHT);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
ballXSpeed = 3;
ballYSpeed = -3;
score = INITIAL_SCORE;
gameStarted = false;
gameOver = false;
addKeyListener(new KeyAdapter() {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
if (paddleX > 0) {
paddleX -= 20;
paddleX += 20;
if (!gameStarted) {
gameStarted = true;
timer.start();
});
setVisible(true);
super.paint(g);
g.setColor(Color.BLACK);
if (gameOver) {
g.setColor(Color.WHITE);
g.setFont(new Font("Arial", Font.BOLD, 30));
return;
g.setColor(Color.WHITE);
g.setColor(Color.RED);
if (bricks[row][col]) {
g.setColor(Color.GREEN);
g.setColor(Color.WHITE);
if (gameOver) {
return;
ballX += ballXSpeed;
ballY += ballYSpeed;
ballXSpeed = -ballXSpeed;
if (ballY < 0) {
ballYSpeed = -ballYSpeed;
ballYSpeed = -ballYSpeed;
score++;
} else {
gameOver = true;
}
}
if (bricks[row][col]) {
if (brickRect.intersects(ballRect)) {
bricks[row][col] = false;
ballYSpeed = -ballYSpeed;
score++;
gameOver = true;
repaint();