<?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 5 :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-5/index.html</link><description>Today you give Dojo Defender a depth effect with a parallax star background and explosion particles. Asteroids also become destructible: big asteroids split into smaller ones.
What you’ll learn today Parallax maths: several layers of stars with their own speed Particle lists: a list of dictionaries for particles Position-based effects: spawning particles in the right spot Recursive splitting: big asteroid → 2 medium → 2 small → gone Step 0: Download the starter The starter is the solution of session 4 — Dojo Defender with a menu, three enemy types and waves.</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/dojo-defender/session-5/index.xml" rel="self" type="application/rss+xml"/><item><title>Dojo Defender Session 5: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-5/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-5/cheatsheet/index.html</guid><description>Parallax stars stars = [] def init_stars(): global stars stars = [] layers = [ (40, 0.3, 2, 120), (40, 0.7, 3, 180), (30, 1.5, 4, 255), ] for count, speed, size, brightness in layers: for _ in range(count): stars.append({ 'x': random.randint(0, WIDTH), 'y': random.randint(0, HEIGHT), 'speed': speed, 'size': size, 'brightness': brightness, }) def update_stars(): for star in stars: star['y'] += star['speed'] if star['y'] > HEIGHT + star['size']: star['y'] = -star['size'] star['x'] = random.randint(0, WIDTH) def draw_stars(surface): for star in stars: c = (star['brightness'], star['brightness'], star['brightness']) if star['size'] &lt;= 2: pygame.draw.rect(surface, c, (star['x'], star['y'], star['size'], star['size'])) else: pygame.draw.circle(surface, c, (int(star['x']), int(star['y'])), star['size'] // 2) Creating particles particles = [] def spawn_particles(x, y, count, colors, speed_range, size_range, life_range): for _ in range(count): angle = random.uniform(0, 2 * math.pi) speed = random.uniform(speed_range[0], speed_range[1]) particles.append({ 'x': x, 'y': y, 'vx': math.cos(angle) * speed, 'vy': math.sin(angle) * speed, 'life': random.randint(life_range[0], life_range[1]), 'max_life': life_range[1], 'color': random.choice(colors), 'size': random.uniform(size_range[0], size_range[1]), }) Updating and drawing particles def update_particles(): for p in particles[:]: p['x'] += p['vx'] p['y'] += p['vy'] p['life'] -= 1 if p['life'] &lt;= 0: particles.remove(p) def draw_particles(surface): for p in particles: ratio = p['life'] / p['max_life'] color = tuple(int(c * ratio) for c in p['color']) pygame.draw.circle(surface, color, (int(p['x']), int(p['y'])), int(p['size'] * ratio)) Exhaust fumes if moving and ship.visible: spawn_particles( ship.rect.centerx, ship.rect.bottom, 1, [(255, 100, 0), (255, 60, 0)], (0.5, 1.5), (2, 4), (10, 20), ) Explosion debris spawn_particles( enemy.rect.centerx, enemy.rect.centery, random.randint(8, 12), [(255, 140, 0), (255, 0, 0), (255, 255, 0), (255, 255, 255)], (1, 4), (2, 5), (20, 40), ) Splitting asteroids def spawn_asteroid(size='big'): rock = GameObject(f'asteroid_{size}.png', random.randint(30, WIDTH - 30), -30) rock.speed = random.uniform(3, 5) rock.asteroid_size = size asteroids.append(rock) return rock def split_asteroid(rock): spawn_particles( rock.rect.centerx, rock.rect.centery, random.randint(8, 12), [(255, 140, 0), (255, 0, 0), (255, 255, 0), (255, 255, 255)], (1, 4), (2, 5), (20, 40), ) if rock.asteroid_size == 'big': for _ in range(2): r = spawn_asteroid('med') r.rect.center = rock.rect.center r.rect.x += random.randint(-20, 20) r.speed = random.uniform(2, 4) elif rock.asteroid_size == 'med': for _ in range(2): r = spawn_asteroid('small') r.rect.center = rock.rect.center r.rect.x += random.randint(-15, 15) r.speed = random.uniform(1, 3) asteroids.remove(rock)</description></item><item><title>Dojo Defender Session 5: Coach notes</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-5/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-5/mentor-notes/index.html</guid><description>Goal of this session After this session a ninja can:
Make a parallax star background with three layers Build a particle system with lists of dictionaries Add position-based effects (exhaust fumes, explosions) Apply recursive splitting to asteroids Preparation (30 min before the session) Starter ZIPs ready on USB and on the site: dojo-defender-s6-starter.en.zip and dojo-defender-s6-solution.en.zip. Run the starter once on the dojo room laptop. Check whether asteroid_med.png and asteroid_small.png are in the images/ folder of the starter. They were added to the shared assets and have to be in the ZIP. Explain parallax on the board: draw three layers of stars with different speeds. Explain the particle life cycle: born → moves → fades → dies. Schedule (3 hours) This session is an integral part of the normal session 6 (Platformer). Use the time like this:</description></item><item><title>Dojo Defender Session 5: Demo</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-5/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-5/demo/index.html</guid><description>How to demo the solution Open a terminal in the project folder. Run: uv run python3 code/sessions/06-platformer/dojo-defender-solution/main.py Show:
In the MENU you see the parallax star background scrolling (three speeds) SPACE starts the game Move the ship — see orange exhaust fumes under the engine Shoot an enemy — 8-12 particles fly in all directions Let an enemy escape — 3-5 white sparks at the bottom Shoot a big asteroid — it splits into two medium ones Shoot a medium asteroid — it splits into two small ones Shoot a small asteroid — it disappears with a particle explosion Code highlights to show:</description></item></channel></rss>