<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dojo Defender: Session 1 :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-1/index.html</link><description>Your spaceship has landed in dark space. Time to bring it to life. In this session you add a ship you can steer and you make asteroids fall down. Dodge them or lose a life. How long do you survive?
What you’ll learn today Using lists to keep track of multiple asteroids Spawning and removing objects Detecting collisions with colliderect Keeping track of lives and showing an explosion effect Forcing game over when your lives run out Step 0: Setup Open Thonny. Download the Dojo Defender starter, unpack the ZIP and open main.py. Click Run. You see a dark blue screen with your spaceship at the bottom. You can already move it with the arrow keys. Problems? Take a look at Installing Thonny. Step 1: Let asteroids spawn A game without danger is boring. Add a list in which you keep track of all the asteroids, and a function that creates new asteroids:</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/dojo-defender/session-1/index.xml" rel="self" type="application/rss+xml"/><item><title>Dojo Defender Session 1: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-1/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-1/cheatsheet/index.html</guid><description># Move the ship if keyboard.left and ship.x > 30: ship.x -= 5 if keyboard.right and ship.x &lt; WIDTH - 30: ship.x += 5 # Spawn an asteroid def spawn_asteroid(): rock = Actor('asteroid_big') rock.x = random.randint(30, WIDTH - 30) rock.y = -30 rock.speed = random.uniform(3, 5) asteroids.append(rock) # Timer every second (60 frames) timer += 1 if timer % 60 == 0: spawn_asteroid() # Make asteroids fall and remove them for rock in asteroids[:]: rock.y += rock.speed if rock.y > HEIGHT + 30: asteroids.remove(rock) # Collision detection if rock.colliderect(ship) and not invincible: lives -= 1 # start explosion # Play the explosion explosion.pos = (ship.x, ship.y) explosion.image = 'explosion_1' explosion_frame = 1 explosion_timer = 5 explosion_active = True ship.visible = False # Game over screen if game_over: screen.draw.text("GAME OVER", center=(WIDTH/2, HEIGHT/2), color="red", fontsize=60) # Show lives screen.draw.text(f"Lives: {lives}", (10, 40), color="red", fontsize=24)</description></item><item><title>Dojo Defender Session 1: Coach notes</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-1/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-1/mentor-notes/index.html</guid><description>Goal of this session After this session a ninja can:
Create and manage a list of asteroids with Actor Spawn asteroids with a timer and remove them when they are off screen Detect collisions with colliderect Keep track of lives and force game over Show a simple explosion animation Preparation (30 min before the session) Starter ZIPs ready on USB and on the site: dojo-defender-s2-starter.en.zip and dojo-defender-s2-solution.en.zip. Run the starter yourself once on the dojo room laptop. Explain that Dojo Defender comes back every session — encourage them to keep the code safe. Schedule (3 hours) This session is an integral part of the normal session 2 (Dodge Meteors). Use the time as follows:</description></item><item><title>Dojo Defender Session 1: Demo</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-1/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-1/demo/index.html</guid><description>How to demo the solution Open a terminal in the project folder. Run: uv run python3 code/sessions/02-dodge-meteors/dojo-defender-solution/main.py Show: The ship moves left/right with the arrow keys. Every second an asteroid appears at the top. Asteroids fall at varying speed (3-5). On a collision: the ship disappears, the explosion animation plays, the ship respawns. After 3 hits: game over with the “GAME OVER” text and the final score. The score counts up for every second you survive.</description></item></channel></rss>