<?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 3 :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-3/index.html</link><description>Today Dojo Defender moves from pgzero to real pygame. No more pgzrun.go() — you write the game loop yourself. The game stays the same, but the engine underneath is different. And at the end you add SOUND.
What you’ll learn today The difference between pgzero and raw pygame Writing the game loop yourself: while running: clock.tick(60) Converting actors to Surface + Rect Loading and playing sound with pygame.mixer Looping background music Step 0: Download the starter Today’s starter is a full port of Dojo Defender Session 2 to raw pygame. No more pgzero, but it plays the same.</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/dojo-defender/session-3/index.xml" rel="self" type="application/rss+xml"/><item><title>Dojo Defender Session 3: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-3/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-3/cheatsheet/index.html</guid><description>Pygame game loop import pygame, sys pygame.init() screen = pygame.display.set_mode((800, 600)) clock = pygame.time.Clock() running = True while running: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: # shoot keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: ship.rect.x -= 5 screen.fill((10, 10, 30)) ship.draw(screen) pygame.display.flip() GameObject (Surface + Rect) class GameObject: def __init__(self, image, x, y): self.image = pygame.image.load(f'images/{image}').convert_alpha() self.rect = self.image.get_rect(center=(x, y)) def draw(self, surface): surface.blit(self.image, self.rect) Text in pygame font = pygame.font.SysFont(None, 36) txt = font.render(f"Score: {score}", True, (255, 255, 255)) screen.blit(txt, (10, 10)) Sound pygame.mixer.init() shoot = pygame.mixer.Sound('sounds/shoot.wav') shoot.play() pygame.mixer.music.load('sounds/bg_music.wav') pygame.mixer.music.play(-1) pgzero → pygame pgzero pygame Actor('ship') GameObject('ship.png', x, y) keyboard.left keys[pygame.K_LEFT] on_key_down(key) event.type == pygame.KEYDOWN screen.draw.text(...) font.render() + screen.blit() rock.colliderect(ship) rock.rect.colliderect(ship.rect) pgzrun.go() while running: loop</description></item><item><title>Dojo Defender Session 3: Coach notes</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-3/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-3/mentor-notes/index.html</guid><description>Goal of this session After this session a ninja can:
Explain the difference between pgzero and raw pygame Write the game loop themselves (while running: clock.tick(60)) Convert actors to GameObject with Surface and Rect Handle input with pygame.key.get_pressed() and KEYDOWN Load and play sound with pygame.mixer Preparation (30 min before the session) Starter ZIPs ready on USB and on the site: dojo-defender-s4-starter.en.zip and dojo-defender-s4-solution.en.zip. Run the starter once on the dojo room laptop. Make sure the sounds are ready in dojo-defender-assets/sounds/, or point ninjas to the solution. Explain why we switch: “pgzero is a set of training wheels. Now you become real Pygame developers.” Schedule (3 hours) This session is an integral part of the normal session 4 (Pong). Use the time as follows:</description></item><item><title>Dojo Defender Session 3: Demo</title><link>https://python.coderdojohasselt.be/en/dojo-defender/session-3/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/dojo-defender/session-3/demo/index.html</guid><description>How to demo the solution Open a terminal in the project folder. Run: uv run python3 code/sessions/04-pong/dojo-defender-solution/main.py Show: The game starts with background music. Shooting (SPACE) makes a laser sound. Explosions (enemy/bullet collision) make an explode sound. Ship hit makes a hit sound. The game works without pgzero — show the import line at the top. The game loop (while running:) is visible in the code. Show pygame.mixer.init() and the sound variables.</description></item></channel></rss>