Level 3 · pgzero

Catch the Falling Star

Move the basket left/right with arrow keys to catch falling stars. This file must be run with pgzrun, not python directly.

What it teaches: Simple arcade games

How to run

Starter file: starter_pgzero_game.py

pgzrun starter_pgzero_game.py

Starter code

"""
LEVEL 3 - PGZERO (Pygame Zero)
Starter: Catch the Falling Star

IMPORTANT: This file must be run with pgzrun, not python directly:
    pgzrun starter_pgzero_game.py

Move the basket left/right with arrow keys to catch falling stars.
Your job: change it, upgrade it, make it yours!
"""
import random

WIDTH = 480
HEIGHT = 400

# We use simple rectangles/circles instead of image files so this runs
# anywhere. Replace with Actor("your_image") if you have sprite images.

basket_x = WIDTH // 2
basket_y = HEIGHT - 30
basket_width = 80
basket_speed = 8

star_x = random.randint(20, WIDTH - 20)
star_y = 0
star_speed = 3

score = 0

def draw():
    screen.clear()
    screen.fill((10, 10, 40))
    # draw basket
    screen.draw.filled_rect(
        Rect((basket_x - basket_width // 2, basket_y), (basket_width, 20)),
        "orange"
    )
    # draw star (as a circle for simplicity)
    screen.draw.filled_circle((star_x, star_y), 10, "yellow")
    screen.draw.text(f"Score: {score}", topleft=(10, 10), color="white", fontsize=24)

def update():
    global star_x, star_y, star_speed, score, basket_x

    if keyboard.left:
        basket_x -= basket_speed
    if keyboard.right:
        basket_x += basket_speed
    basket_x = max(basket_width // 2, min(WIDTH - basket_width // 2, basket_x))

    star_y += star_speed

    # check if star reaches basket height
    if star_y >= basket_y:
        if abs(star_x - basket_x) < basket_width // 2:
            score += 1
            star_speed += 0.3  # game gets a little faster each catch
        reset_star()

def reset_star():
    global star_x, star_y
    star_x = random.randint(20, WIDTH - 20)
    star_y = 0


# ---------------------------------------------------------------
# LEVEL UP! Try one (or more) of these upgrades:
# 1. Add a second falling star (or more!) at the same time.
# 2. Add a "bad" falling object that loses points if caught.
# 3. Add a timer - the game ends after 30 seconds, show "Game Over".
# 4. Add sound effects with sounds.catch.play() (needs a sounds folder).
# 5. Replace the shapes with real Actor() images for basket and star.
# 6. Ask your AI tool: "How do I add a start screen and restart button
#    to my Pygame Zero game?"
# ---------------------------------------------------------------

Challenge ideas

Easy

  • Change colors, basket size, or star fall speed.
  • Change scoring (e.g. +5 per catch instead of +1).

Medium

  • Add a second falling object, or make stars fall from random speeds.
  • Add a "miss" counter — after 3 misses, show Game Over.
  • Add a countdown timer using the update(dt) time parameter.

Hard

  • Add multiple lanes/levels of difficulty that increase over time.
  • Add a title screen and a "Press SPACE to start" state.
  • Add real sprite images and sound effects (catch/miss sounds).

Ask your AI tool

How do I add a game-over screen with a restart button in Pygame Zero?