Session 6: Coach notes

Goal of this session

At the end of the Basic checkpoint a ninja can:

  • Make the player fall thanks to vel_y and gravity.
  • Stop correctly on platforms using colliderect.
  • Jump with SPACE (only when on_ground is True).

What is not expected after session 6: sound, win screen, double jump. Those are Expert material.


Preparation (30 min before the session)

  • Test the starter ZIP on a dojo laptop. Check that pygame is installed (not pgzero; this is the first session with pure pygame).
  • sounds/jump.wav present? Check that the sound file is in the ZIP.
  • Project Step 1 live on the screen before ninjas start. The jump from pgzrun to pygame is a mental hurdle. Explain that pygame is the same library, but without the pgzrun wrapper.

Schedule (3 hours)

TimeActivity
0:00 to 0:10Welcome, recapping session 5
0:10 to 0:25Projector: explaining the pygame game loop vs. pgzrun
0:25 to 1:30Working on your own: steps 1 and 2
1:30 to 2:15Stretch and Expert
2:15 to 2:45Buffer / walking around / helping
2:45 to 3:00Showcase + cliffhanger

Common questions

“The player falls through the platforms” Check the order: increase vel_y → adjust player_rect.y → platform loop. If the platform loop comes before the movement, it doesn’t work. Get the ninja to compare the order in their code with the worksheet.

“The player jumps while in the air” on_ground isn’t set correctly. Check that on_ground = False comes before the platform loop and is only set to True when a collision is detected. Common mistake: on_ground is set to True globally.

“No sound” Two causes:

  1. pygame.mixer.init() doesn’t come before pygame.mixer.Sound(...).
  2. The script is started from a different folder. Python then looks for sounds/jump.wav in the working folder, not next to main.py. The starter uses _DIR = os.path.dirname(os.path.abspath(__file__)) to solve this.

“The window closes straight away on the win screen” running = False stops the game loop. If the ninja wants the screen to stay, replace running = False with your own waiting loop: pygame.time.wait(3000) followed by break.


Hints for Stretch and Expert

Give hints, not full code.

Stretch: collecting stars: Point at stars[:]. Without the copy slice you’d get a RuntimeError when removing while iterating. This is a good teaching moment about list mutation.

Expert: sound: If pygame.mixer throws an error, check whether the system has an audio device. In a VM or on some Chromebooks there’s no audio output. As a fallback: skip the sound and focus on the win screen.


Dojo Defender today

Dojo Defender session 5 is integrated into this session. The snapshot adds:

  • Parallax stars (three layers, they scroll in the menu too)
  • Particle system (spawn/update/draw with alpha fade)
  • Exhaust, explosion debris, sparks
  • Destructible asteroids (big → med → small → particles)

See content/dojo-defender/session-5/ for the full worksheet.

The starter ZIP is at: /sessions/python/06-platformer/dojo-defender-s6-starter.en.zip. The solution ZIP is at: /sessions/python/06-platformer/dojo-defender-s6-solution.en.zip.

Time split: you work on the platformer for the first 75 minutes, and on Dojo Defender for the rest of the time.


Cliffhanger script

Read this out to the group:

“Today your player jumped and collected stars. Next time we write our OWN classes: bugs that fall down, and you have to ZAP them before they hit you. And you make them yourself, from scratch.”