<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 9: Boss Battle :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/index.html</link><description>The boss is waiting for you. It moves back and forth, shoots bullets and gets more dangerous as its HP drops. You shoot back, keep track of its HP on a bar, and survive three phases. This is the hardest session, and the most satisfying one if you make it through.
What you’ll learn today Drawing an HP bar on the screen A state machine: the boss behaves differently in each phase Making the boss shoot with a bullet timer Activating phases at HP thresholds Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py in Thonny. Click Run. You see the boss at the top, your ship at the bottom. No movement, no bullets yet. Problems? Ask a coach. Step 1: Making the boss move The boss moves back and forth across the screen. When it hits the edge, it turns around.</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 9: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/mentor-notes/index.html</guid><description>Goal of this session At the end of the Basic checkpoint a ninja can:
Make the boss move back and forth with direction switching. Make player bullets hit the boss and make boss_hp shrink. Draw an HP bar that follows the current HP. What is not expected after session 9: the boss shooting back, three phases. Those are Stretch/Expert material.
Preparation (30 min before the session) Explain state machines briefly: a variable (boss_phase) determines the behaviour. Use an everyday analogy: a traffic light that goes from green to orange to red. BOSS_MAX_HP has to be consistent throughout the code. If ninjas change it, they have to change the HP bar calculation too. Check the starter: player_bullets and boss_bullets are empty lists, boss_hp = 100, BOSS_MAX_HP = 100, boss_shoot_timer = 0. Schedule (3 hours) Time Activity 0:00 to 0:10 Welcome, looking back at session 8 0:10 to 0:20 Projector: explaining the HP bar and the state machine 0:20 to 1:30 Working on your own: steps 1 and 2 1:30 to 2:15 Stretch (boss shoots back) 2:15 to 2:45 Expert (three phases) + buffer 2:45 to 3:00 Showcase + cliffhanger for session 10 Common questions “The boss stops at the edge but doesn’t turn around” boss_speed = -boss_speed has to come after the edge check. If it comes before the movement, the boss turns around before it reaches the edge and it can get stuck at the edge (oscillating). Have them check the order.</description></item><item><title>Session 9: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/cheatsheet/index.html</guid><description>Moving the boss back and forth boss_rect.x += boss_speed if boss_rect.right >= WIDTH or boss_rect.left &lt;= 0: boss_speed = -boss_speed # reverse direction Drawing an HP bar BOSS_MAX_HP = 100 # Background pygame.draw.rect(screen, (80, 0, 0), (10, 10, 300, 20)) # Filled part breedte = max(0, int(300 * boss_hp / BOSS_MAX_HP)) pygame.draw.rect(screen, RED, (10, 10, breedte, 20)) Bullet collision with the boss for b in player_bullets[:]: b.y -= 8 if b.colliderect(boss_rect): player_bullets.remove(b) boss_hp -= 1 Boss shoots (timer pattern) boss_shoot_timer += 1 if boss_shoot_timer >= boss_shoot_interval: boss_shoot_timer = 0 boss_bullets.append( pygame.Rect(boss_rect.centerx - 4, boss_rect.bottom, 8, 16) ) Phase logic (state machine) if boss_hp > 50: boss_phase = 1 boss_speed_abs = 3 boss_shoot_interval = 60 elif boss_hp > 25: boss_phase = 2 boss_speed_abs = 5 boss_shoot_interval = 35 else: boss_phase = 3 boss_speed_abs = 7 boss_shoot_interval = 20 # Keep the direction: boss_speed = boss_speed_abs * (1 if boss_speed > 0 else -1) Win screen screen.fill((0, 0, 0)) win = font.render("BOSS DEFEATED!", True, YELLOW) screen.blit(win, (WIDTH // 2 - win.get_width() // 2, HEIGHT // 2)) pygame.display.flip() pygame.time.wait(3000) Player lives as hearts for i in range(player_lives): pygame.draw.circle(screen, RED, (WIDTH - 20 - i * 30, 20), 10)</description></item><item><title>Session 9: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/09-boss-battle/demo/index.html</guid><description>Session 9 uses pure pygame. Show a GIF of the solution on the session page.
How to record Run the solution: cd code/sessions/09-boss-battle/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 ~20 seconds: go through all three phases and show the win screen. Save as boss-battle.gif in static/sessions/09-boss-battle/. Link to it in the worksheet via: ![Demo](/sessions/09-boss-battle/boss-battle.gif). Target size: ~800×500 pixels, ≤ 2 MB.</description></item></channel></rss>