Brick breaker game

A classic!

Brick Breaker is a popular arcade-style game where you control a paddle to break bricks on a screen. Here's a simple implementation of the game:

Gameplay

  1. The game starts with a set of bricks arranged in a grid on the screen.
  2. You control a paddle at the bottom of the screen using the left and right arrow keys.
  3. A ball is launched from the top of the screen, and you must use the paddle to bounce the ball and break the bricks.
  4. Each brick has a specific point value, and when you break a brick, you earn those points.
  5. The game ends when all the bricks are broken or the ball hits the bottom of the screen and is lost.

Code

Here's a simple implementation of Brick Breaker in Python using the Pygame library:

import pygame
import random

# Initialize pygame
pygame.init()

# Set the screen dimensions
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Set the title of the window
pygame.display.set_caption("Brick Breaker")

# Define some colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Define the brick properties
BRICK_WIDTH = 60
BRICK_HEIGHT = 20
BRICK_SPACING = 10
BRICKS_PER_ROW = 10
BRICKS_PER_COLUMN = 5

# Create the brick grid
bricks = []
for i in range(BRICKS_PER_COLUMN):
    for j in range(BRICKS_PER_ROW):
        x = j * (BRICK_WIDTH + BRICK_SPACING)
        y = i * (BRICK_HEIGHT + BRICK_SPACING)
        bricks.append(pygame.Rect(x, y, BRICK_WIDTH, BRICK_HEIGHT))

# Define the paddle properties
PADDLE_WIDTH = 100
PADDLE_HEIGHT = 20
PADDLE_SPEED = 5

# Create the paddle
paddle = pygame.Rect(SCREEN_WIDTH / 2, SCREEN_HEIGHT - PADDLE_HEIGHT - BRICK_SPACING, PADDLE_WIDTH, PADDLE_HEIGHT)

# Define the ball properties
BALL_SIZE = 10
BALL_SPEED = 5

# Create the ball
ball = pygame.Rect(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, BALL_SIZE, BALL_SIZE)
ball_x_speed = BALL_SPEED
ball_y_speed = -BALL_SPEED

# Main game loop
while True:
    # Handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Move the paddle
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        paddle.x -= PADDLE_SPEED
    if keys[pygame.K_RIGHT]:
        paddle.x += PADDLE_SPEED

    # Move the ball
    ball.x += ball_x_speed
    ball.y += ball_y_speed

    # Check for collisions with the paddle
    if ball.colliderect(paddle):
        ball_y_speed = -ball_y_speed

    # Check for collisions with the bricks
    for brick in bricks:
        if ball.colliderect(brick):
            ball_y_speed = -ball_y_speed
            bricks.remove(brick)

    # Check for collisions with the edges
    if ball.y < 0:
        ball_y_speed = -ball_y_speed
    if ball.y > SCREEN_HEIGHT - BRICK_SPACING:
        ball_y_speed = -ball_y_speed
    if ball.x < 0 or ball.x > SCREEN_WIDTH - BRICK_SPACING:
        ball_x_speed = -ball_x_speed

    # Draw everything
    screen.fill(WHITE)
    for brick in bricks:
        pygame.draw.rect(screen, RED, brick)
    pygame.draw.rect(screen, RED, paddle)
    pygame.draw.rect(screen, RED, ball)
    pygame.display.flip()

    # Cap the frame rate
    pygame.time.Clock().tick(60)

This code creates a simple Brick Breaker game with a paddle, ball, and bricks. You can move the paddle using the left and right arrow keys, and the ball will bounce off the paddle and bricks. The game ends when all the bricks are broken or the ball hits the bottom of the screen.

Note: This is a basic implementation, and you can add features like scoring, levels, and power-ups to make the game more interesting.