Learn / Intermediate / Lesson 10

Project: build a dodge game with pygame

25 min Tested on Python 3.15Updated By
In short

A pygame game is a loop that runs about 60 times a second: read input, update positions, check collisions, then draw the frame. In this project you build Dodge — move a player left and right to avoid falling blocks — in about 100 lines, using classes, functions and everything else from the Intermediate path.

What you'll build

  • A window with a player at the bottom that moves with the arrow keys.
  • Blocks that fall from the top, faster and more of them as your score grows.
  • Collision detection, a score counter and a restart key.
  • Clean structure: game rules in small pure functions you can test without a window.

Setup

pygame opens a real window, so this project runs on your computer rather than in the browser. Use pygame-ce, the actively maintained community edition — your code still says import pygame.

terminal
uv init dodge
cd dodge
uv add pygame-ce
uv run python -c "import pygame; print(pygame.version.ver)"

Step 1: the game rules, without graphics

Start with the logic. A Box dataclass describes anything rectangular, and collides checks whether two boxes overlap on both axes. Because it doesn't need pygame, you can run and test it right here.

Python
from dataclasses import dataclass

@dataclass
class Box:
    x: float
    y: float
    w: int
    h: int

def collides(a: Box, b: Box) -> bool:
    return a.x < b.x + b.w and b.x < a.x + a.w and a.y < b.y + b.h and b.y < a.y + a.h

player = Box(100, 500, 50, 80)
print(collides(player, Box(120, 450, 60, 60)))   # overlapping
print(collides(player, Box(300, 450, 60, 60)))   # far to the right
print(collides(player, Box(150, 500, 60, 60)))   # just touching edges
Output
True
False
False

Step 2: the game loop

  • pygame.event.get() — handle closing the window and key presses.
  • Update — move the player and the blocks, add score, speed up.
  • Check — did the player hit a block? Then it's game over.
  • Draw — clear the screen, draw shapes and text, then pygame.display.flip().
  • clock.tick(60) — wait so the game runs at 60 frames per second on every computer.

Step 3: the complete game

Save this as main.py and run uv run main.py. Use the left and right arrow keys; press space to restart after a crash.

main.py
import random
from dataclasses import dataclass

import pygame

WIDTH, HEIGHT = 480, 640
PLAYER_W, PLAYER_H = 50, 80
BLOCK_W, BLOCK_H = 60, 60
FPS = 60


@dataclass
class Box:
    x: float
    y: float
    w: int
    h: int


def collides(a: Box, b: Box) -> bool:
    """True when two rectangles overlap."""
    return a.x < b.x + b.w and b.x < a.x + a.w and a.y < b.y + b.h and b.y < a.y + a.h


def move_player(player: Box, direction: int, speed: float) -> None:
    """Move left (-1) or right (+1), staying inside the window."""
    player.x = min(max(player.x + direction * speed, 0), WIDTH - player.w)


def new_block() -> Box:
    return Box(random.randint(0, WIDTH - BLOCK_W), -BLOCK_H, BLOCK_W, BLOCK_H)


def main() -> None:
    pygame.init()
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Dodge — a PythonMates project")
    clock = pygame.time.Clock()
    font = pygame.font.Font(None, 36)

    player = Box(WIDTH / 2 - PLAYER_W / 2, HEIGHT - PLAYER_H - 20, PLAYER_W, PLAYER_H)
    blocks = [new_block()]
    speed = 4.0
    score = 0
    running, game_over = True, False

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN and game_over and event.key == pygame.K_SPACE:
                blocks, speed, score, game_over = [new_block()], 4.0, 0, False

        if not game_over:
            keys = pygame.key.get_pressed()
            direction = keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
            move_player(player, direction, speed=7)

            for block in blocks:
                block.y += speed
            passed = [b for b in blocks if b.y > HEIGHT]
            score += len(passed)
            blocks = [b for b in blocks if b.y <= HEIGHT]
            if passed:
                blocks.append(new_block())
                speed += 0.3
            if score >= 5 * len(blocks) and len(blocks) < 4:
                blocks.append(new_block())

            game_over = any(collides(player, b) for b in blocks)

        screen.fill((15, 23, 42))
        pygame.draw.rect(screen, (255, 212, 59), (player.x, player.y, player.w, player.h), border_radius=8)
        for b in blocks:
            pygame.draw.rect(screen, (55, 118, 171), (b.x, b.y, b.w, b.h), border_radius=6)
        screen.blit(font.render(f"Score: {score}", True, (230, 238, 249)), (12, 10))
        if game_over:
            text = font.render("Crash! Press SPACE to restart", True, (248, 113, 113))
            screen.blit(text, text.get_rect(center=(WIDTH / 2, HEIGHT / 2)))

        pygame.display.flip()
        clock.tick(FPS)

    pygame.quit()


if __name__ == "__main__":
    main()

How it works

  • keys[pygame.K_RIGHT] - keys[pygame.K_LEFT] gives 1, -1 or 0: a neat way to get a direction from two keys.
  • move_player uses min(max(...)) to clamp the player inside the window.
  • Blocks that leave the screen add to the score and are replaced by a new one, and the speed goes up a little each time.
  • Shapes are drawn with pygame.draw.rect, so the game needs no image files.

Ideas to extend it

  • Save a high score to a file with pathlib (see Files and errors).
  • Replace the rectangles with images using pygame.image.load().
  • Add sound with pygame.mixer.Sound.
  • Write pytest tests for collides and move_player.
  • Ask an AI assistant to explain one function you don't fully understand — then check its answer by changing the code.

Practice

Write the game's move_player rule as a pure function: clamp_x(x, step, width, player_w) returns x + step but never less than 0 and never more than width - player_w. Print the results for (100, 7, 480, 50), (3, -7, 480, 50) and (428, 7, 480, 50).

Expected output:
107
0
430
Your code

Quick check

1. What does clock.tick(60) do?

2. Two boxes overlap when...

3. Why is pygame.display.flip() needed?

Use AI at work

Python is the base of AI. On AIMates you learn to use AI tools well in your daily job.

Visit AIMates →