Session 6: Platformer

In this session you build a micro-platformer. Your player has gravity, can jump, and collects stars on platforms. For the first time you use pygame directly, without pgzrun, and you add sound effects with pygame.mixer.

What you’ll learn today

  • Simulating gravity with a vel_y variable
  • Jumping by making vel_y negative
  • Detecting platform collisions with pygame.Rect
  • Collecting items (collision with stars)
  • Playing sounds with pygame.mixer

Step 0: Setup

  1. Open Thonny.
  2. Download the starter via this link, unpack the ZIP and open main.py in Thonny.
  3. Click the green Run button. You see a window with a player, platforms and stars, but the player doesn’t fall yet.
  4. Problems? Ask a coach.

Step 1: Gravity

In a platformer the player falls down all the time, unless they’re standing on a platform. We simulate that with vel_y: a vertical speed that grows every frame because of gravity.

Find the # STEP 1 comment in main.py and replace the pass with:

# Apply gravity
vel_y += GRAVITY
player_rect.y += vel_y

# Check whether the player is standing on a platform
on_ground = False
for platform in platforms:
    if (player_rect.colliderect(platform) and vel_y > 0):
        player_rect.bottom = platform.top
        vel_y = 0
        on_ground = True

GRAVITY = 0.5 is already at the top of the file. Click Run. The player now falls onto the nearest platform and stays there.

What happens here? Every frame vel_y gets a bit bigger (the player accelerates downwards). If the player touches a platform while falling down (vel_y > 0), you put their bottom edge (bottom) exactly on top of the platform and stop the fall (vel_y = 0).

Step 2: ✅ Basic: jumping

A platformer without jumping is only half finished. Press SPACE to jump: that’s nothing more than suddenly making vel_y negative.

Find the # STEP 2 comment and add:

keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
    player_rect.x -= SPEED
if keys[pygame.K_RIGHT]:
    player_rect.x += SPEED

# Jumping — only when you're standing on the ground
if keys[pygame.K_SPACE] and on_ground:
    vel_y = -12

Click Run and test:

  • Arrow keys: move left/right
  • SPACE: jump up

Tip: If the jump feels too high or too low, change -12 to -10 (jump lower) or -15 (jump higher).

Step 3: ⭐ Stretch: collecting stars

Let’s add something to collect. The starter already has a stars list with pygame.Rect objects. You write the collision detection.

Find the # STEP 3 comment:

for star in stars[:]:  # [:] so you can remove safely while looping
    if player_rect.colliderect(star):
        stars.remove(star)
        score += 1

And in the drawing function (look for # DRAW STARS):

for star in stars:
    pygame.draw.polygon(screen, YELLOW, star_points(star.centerx, star.centery, 12, 5))

star_points() is already defined in the starter. It works out the points of a 5-pointed star.

At the top of the game there’s score = 0. Draw the score on the screen (look for # DRAW SCORE):

score_surf = font.render(f"Stars: {score}", True, WHITE)
screen.blit(score_surf, (10, 10))

Step 4: 🔥 Expert: sound and win screen

Now you add a jump sound and a win screen for when all the stars have been collected.

Loading sound (this is already in the starter; check that jump.wav is in the sounds/ folder):

pygame.mixer.init()
jump_snd = pygame.mixer.Sound("sounds/jump.wav")

Playing the sound when you jump: add it to the jump code:

if keys[pygame.K_SPACE] and on_ground:
    vel_y = -12
    jump_snd.play()

Win screen: add it after the star loop:

if len(stars) == 0:
    win_surf = font.render("You win! All stars collected!", True, YELLOW)
    screen.blit(win_surf, (WIDTH // 2 - win_surf.get_width() // 2, HEIGHT // 2))
    pygame.display.flip()
    pygame.time.wait(3000)
    running = False

Showcase

Show your game to a coach and a buddy. Can you collect all the stars before the time runs out?

Dojo Defender

Today’s Dojo Defender snapshot builds on session 5:

  • Parallax star background — three layers of stars, each with its own speed
  • Particle effects — exhaust, explosion debris, sparks
  • Destructible asteroids — big → med → small → gone

Download the Dojo Defender starter or solution.

See you next time!

“Next time: click on bugs and ZAP them away, with your own classes.”

Take it home

Try one of these extensions at home:

  1. Easy: change the colour of the player when they’re in the air.
  2. Medium: add a timer. The player has 30 seconds to collect all the stars.
  3. Hard: add an enemy that walks back and forth on a platform; if the player touches it, the game restarts.
  4. Very hard: make the player double jump (press SPACE twice for a second jump in the air).

Stuck? Ask a coach at the next dojo, or just try something else. Programming is doing.

View the cheatsheet for this session