How to Make a Simple Brick Breaker Game with Python (Pygame)

Brick Breaker is one of those timeless games that’s easy to code and endlessly fun. Using Python’s Pygame library, we can build this game in under 100 lines of code.

Step 1: Install Pygame

pip install pygame

Step 2: Set Up the Game Window

import pygame
pygame.init()

WIDTH, HEIGHT = 600, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Brick Breaker")

Step 3: Create the Paddle and Ball

paddle = pygame.Rect(WIDTH//2 - 50, HEIGHT - 20, 100, 10)
ball = pygame.Rect(WIDTH//2, HEIGHT//2, 10, 10)
ball_speed = [4, -4]

Step 4: Build Bricks

bricks = [pygame.Rect(x*60+10, y*20+30, 50, 10) for x in range(9) for y in range(5)]

Step 5: Game Loop and Collision Detection

clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and paddle.left > 0:
        paddle.x -= 5
    if keys[pygame.K_RIGHT] and paddle.right < WIDTH:
        paddle.x += 5

    ball.x += ball_speed[0]
    ball.y += ball_speed[1]

    if ball.left <= 0 or ball.right >= WIDTH:
        ball_speed[0] = -ball_speed[0]
    if ball.top <= 0:
        ball_speed[1] = -ball_speed[1]

    if ball.colliderect(paddle):
        ball_speed[1] = -ball_speed[1]

    for brick in bricks[:]:
        if ball.colliderect(brick):
            bricks.remove(brick)
            ball_speed[1] = -ball_speed[1]
            break

    if ball.bottom >= HEIGHT:
        running = False

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (0, 255, 0), paddle)
    pygame.draw.ellipse(screen, (255, 255, 255), ball)
    for brick in bricks:
        pygame.draw.rect(screen, (255, 0, 0), brick)
    pygame.display.flip()
    clock.tick(60)
pygame.quit()

Key Takeaways:

You can extend this project with levels, sounds, and scoring.

Pygame makes 2D game development simple and accessible.

Brick Breaker is a great intro to collision and game loops.