<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 2: Dodge Meteors :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/index.html</link><description>Your spaceship drifts in the dark of space while meteors come down from above. Dodge them, or lose a life. The longer you survive, the faster they go. And this time you also build Dojo Defender: the bigger project that grows every session.
What you’ll learn today Using lists to keep track of several objects Spawning (creating) and removing objects Keeping track of lives and showing a game-over screen Dojo Defender: adding your own spaceship to the bigger project Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py. Click Run. You see your spaceship at the bottom, but no meteors are falling yet. Problems? Take a look at Installing Thonny. Step 1: Let meteors spawn Fill in the spawn_meteor() function. A meteor is an Actor that starts at the top of the screen at a random x position:</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 2: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/mentor-notes/index.html</guid><description>Goal of this session After the Basic checkpoint a ninja can:
Create and manage a list of meteors. Make meteors spawn, move and disappear when they fall off the screen. Subtract lives on a collision and stop when all the lives are gone. Dojo Defender starts today. Ninjas add their spaceship to the bigger project.
Preparation (30 min before the session) Starter ZIPs ready on USB and the site: starter.zip and dojo-defender-s2.zip. Dojo Defender briefing to prepare: explain that this project grows every session and that it pays off to keep showing up. Run the starter once yourself on the dojo room laptop. Schedule (3 hours) Time Activity 0:00 to 0:10 Looking back at session 1 + intro to meteors 0:10 to 0:25 Step 1: spawn_meteor() 0:25 to 1:00 Step 2: moving + collision 1:00 to 1:45 Step 3 to 4: game over, speed, score 1:45 to 2:15 Dojo Defender intro + moving the spaceship 2:15 to 2:45 Buffer / stretch / expert 2:45 to 3:00 Showcase + cliffhanger Common questions “My list keeps getting bigger and the game slows down” The meteors aren’t being removed. Check whether the remove() call is correct and whether the loop goes over meteors[:] (a copy), not over meteors itself.</description></item><item><title>Session 2: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/cheatsheet/index.html</guid><description>Creating a list and adding to it meteors = [] # empty list rock = Actor('meteor') meteors.append(rock) # add it Looping over a list (with removal) for rock in meteors[:]: # [:] = copy rock.y += 4 if rock.y > HEIGHT + 30: meteors.remove(rock) Timer: doing something every N frames timer = 0 def update(): global timer timer += 1 if timer % 60 == 0: # every second spawn_meteor() Collision with several objects for rock in meteors[:]: if rock.colliderect(ship): meteors.remove(rock) lives -= 1 Stopping on game over def update(): if game_over: return # rest of the code... Text on screen screen.draw.text(f"Score: {score}", (10, 10), color="white") screen.draw.text( "GAME OVER", center=(WIDTH / 2, HEIGHT / 2), color="red", fontsize=60, ) Random position import random x = random.randint(30, WIDTH - 30)</description></item><item><title>Session 2: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/02-dodge-meteors/demo/index.html</guid><description>Run the solution: cd code/sessions/02-dodge-meteors/solution &amp;&amp; uv run pgzrun main.py.
Record ~10 seconds while dodging meteors and losing a life. Save as dodge-meteors.gif in static/sessions/02-dodge-meteors/. Target size: ~600×500 pixels, ≤ 2 MB.
Refer to it in the worksheet via: ![Demo](/sessions/02-dodge-meteors/dodge-meteors.gif).</description></item></channel></rss>