<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 3: Space Blaster :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/index.html</link><description>Enemies stand in formation at the top of the screen. You’re at the bottom with a spaceship and one goal: knock them all out before they reach the bottom. Press SPACE to shoot, and don’t miss.
What you’ll learn today Keeping track of bullets in a list Creating a bullet when you press SPACE Collision between bullets and enemies Game over when an enemy reaches the bottom Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py. Click Run. You see the ship at the bottom and 12 enemies in a grid, but nothing moves and you can’t shoot yet. Step 1: Let the ship move The ship already moves with the arrow keys. Check the code in update(). Make sure the ship doesn’t go off the screen:</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 3: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/mentor-notes/index.html</guid><description>Goal of this session After the Basic checkpoint a ninja can:
Create a bullet when SPACE is pressed and store it in a list. Make bullets move up and remove them when they go off the screen. Understand why you use bullets[:] and not bullets when removing inside a loop. Preparation Starter ZIP ready on USB and the site. Run the solution once yourself so you know the timing of the steps. Dojo Defender: today ninjas add a shooting mechanism to their project. Schedule (3 hours) Time Activity 0:00 to 0:10 Looking back at session 2 + intro 0:10 to 0:20 Step 1: moving the ship (review) 0:20 to 1:00 Step 2: shooting + moving the bullets 1:00 to 1:45 Step 3: collision with enemies 1:45 to 2:15 Step 4: moving enemies + game over 2:15 to 2:45 Dojo Defender: adding the shooting mechanism 2:45 to 3:00 Showcase + cliffhanger Common questions "bullets gives a RuntimeError while removing" The ninja is looping over the original list while removing. Get them to use bullets[:] (a copy).</description></item><item><title>Session 3: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/cheatsheet/index.html</guid><description>Creating a bullet on SPACE def on_key_down(key): if key == keys.SPACE: kogel = Actor('bullet') kogel.x = ship.x kogel.y = ship.y - 20 bullets.append(kogel) Moving and removing bullets for kogel in bullets[:]: kogel.y -= 8 if kogel.y &lt; -10: bullets.remove(kogel) Collision bullet ↔ enemy for kogel in bullets[:]: for vijand in enemies[:]: if kogel.colliderect(vijand): bullets.remove(kogel) enemies.remove(vijand) score += 10 break Shooting cooldown cooldown = 0 def update(): global cooldown if cooldown > 0: cooldown -= 1 def on_key_down(key): global cooldown if key == keys.SPACE and cooldown == 0: shoot() cooldown = 15 Timer: making enemies drop timer = 0 def update(): global timer timer += 1 if timer % 90 == 0: for vijand in enemies: vijand.y += 20 Win / game over text if not enemies: screen.draw.text("YOU WIN!", center=(WIDTH/2, HEIGHT/2), color="yellow", fontsize=60)</description></item><item><title>Session 3: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/03-space-blaster/demo/index.html</guid><description>Run the solution: cd code/sessions/03-space-blaster/solution &amp;&amp; uv run pgzrun main.py.
Record ~10 seconds: shoot a few enemies and let the score climb. Save as space-blaster.gif in static/sessions/03-space-blaster/. Target size: ~600×500 pixels, ≤ 2 MB.</description></item></channel></rss>