<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 6: Platformer :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/index.html</link><description>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 Open Thonny. Download the starter via this link, unpack the ZIP and open main.py in Thonny. Click the green Run button. You see a window with a player, platforms and stars, but the player doesn’t fall yet. 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.</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/06-platformer/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 6: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/mentor-notes/index.html</guid><description>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) Time Activity 0:00 to 0:10 Welcome, recapping session 5 0:10 to 0:25 Projector: explaining the pygame game loop vs. pgzrun 0:25 to 1:30 Working on your own: steps 1 and 2 1:30 to 2:15 Stretch and Expert 2:15 to 2:45 Buffer / walking around / helping 2:45 to 3:00 Showcase + 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.</description></item><item><title>Session 6: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/cheatsheet/index.html</guid><description>Opening a Pygame window import pygame pygame.init() WIDTH, HEIGHT = 800, 500 screen = pygame.display.set_mode((WIDTH, HEIGHT)) clock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((30, 30, 60)) pygame.display.flip() clock.tick(60) Gravity loop GRAVITY = 0.5 vel_y = 0 # Every frame: vel_y += GRAVITY player_rect.y += int(vel_y) Platform collision 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 Jumping keys = pygame.key.get_pressed() if keys[pygame.K_SPACE] and on_ground: vel_y = -12 Creating a Rect platform = pygame.Rect(x, y, breedte, hoogte) pygame.draw.rect(screen, kleur, platform) Collision with items (list) for item in items[:]: if player_rect.colliderect(item): items.remove(item) score += 1 Sound import os import pygame pygame.mixer.init() # _DIR makes sure the path is right wherever you start the script from _DIR = os.path.dirname(os.path.abspath(__file__)) try: jump_snd = pygame.mixer.Sound(os.path.join(_DIR, "sounds/jump.wav")) except Exception: jump_snd = None if jump_snd: jump_snd.play() Drawing text font = pygame.font.SysFont(None, 36) tekst = font.render(f"Score: {score}", True, (255, 255, 255)) screen.blit(tekst, (10, 10))</description></item><item><title>Session 6: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/06-platformer/demo/index.html</guid><description>Session 6 uses pure pygame (no pgzrun), so a WebAssembly demo doesn’t apply. Show a GIF of the solution on the session page.
How to record Run the solution: cd code/sessions/06-platformer/solution &amp;&amp; python main.py. Use a screen recorder: Linux: Peek or SimpleScreenRecorder. Windows: ScreenToGif. macOS: QuickTime → export to GIF via ffmpeg or Kap. Record ~12 seconds: show the player jumping, collecting a star and the win screen. Save as platformer.gif in static/sessions/06-platformer/. Refer to it in the worksheet via: ![Demo](/sessions/06-platformer/platformer.gif). Target size: ~800×500 pixels, ≤ 2 MB.</description></item></channel></rss>