<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 5: Breakout :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/index.html</link><description>A ball bounces back and forth and smashes rows of coloured bricks to pieces. You steer the paddle to keep the ball in the air. And this time you hear it too: there are sound effects on every collision.
What you’ll learn today Loading and playing sound with pygame.mixer Collisions with several objects (a list of bricks) Making the ball bounce in the right direction Building a win and lose screen Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py. Click Run. You see the paddle, the ball and the bricks. The ball already moves horizontally but not vertically. You’re going to change that. Step 1: Make the ball move vertically Find this line in the starter:</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/05-breakout/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 5: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/mentor-notes/index.html</guid><description>Goal of this session After the Basic checkpoint a ninja can:
Make the ball move vertically and bounce off all four edges and the paddle. Play a sound on a collision. Take off a life when the ball reaches the bottom. Preparation Starter ZIP ready. It contains sounds/hit.wav and sounds/explosion.wav. Test whether pygame.mixer works on the dojo laptop (sometimes the audio driver is missing). Dojo Defender: today ninjas add sound effects to their project. Schedule (3 hours) Time Activity 0:00 to 0:10 Looking back + intro to sound 0:10 to 0:30 Step 1 to 2: moving the ball + bouncing 0:30 to 1:15 Step 3: breaking bricks 1:15 to 1:50 Step 4: lives, sound, win/game over 1:50 to 2:20 Dojo Defender: adding sound 2:20 to 2:50 Buffer / stretch / expert 2:50 to 3:00 Showcase + cliffhanger Common questions “I can’t hear any sound” Two causes:</description></item><item><title>Session 5: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/cheatsheet/index.html</guid><description>Loading and playing sound import os import pygame pygame.mixer.init() # os.path.abspath(__file__) gives the folder of this file, not the folder you start the script from. # Without this the sound only works if you open the script from that same folder. _DIR = os.path.dirname(os.path.abspath(__file__)) try: snd_hit = pygame.mixer.Sound(os.path.join(_DIR, 'sounds/hit.wav')) except: snd_hit = None if snd_hit: snd_hit.play() Ball bounces off the walls if ball.left &lt;= 0 or ball.right >= WIDTH: ball_dx = -ball_dx if ball.top &lt;= 0: ball_dy = -ball_dy Ball bounces off the paddle if ball.colliderect(paddle): ball_dy = -ball_dy Breaking bricks (dict in a list) # Create a brick stenen.append({'rect': pygame.Rect(x, y, b, h), 'kleur': (220, 60, 60)}) # Check the collision for steen in stenen[:]: if ball.colliderect(steen['rect']): stenen.remove(steen) ball_dy = -ball_dy score += 10 break Drawing bricks for steen in stenen: pygame.draw.rect(screen, steen['kleur'], steen['rect']) Lives: resetting the ball if ball.top > HEIGHT: levens -= 1 ball.center = (paddle.centerx, paddle.top - 12) ball_dy = -4 Win / game over if not stenen: txt = font.render("YOU WIN!", True, (255, 220, 0)) screen.blit(txt, (WIDTH//2 - txt.get_width()//2, HEIGHT//2)) if levens &lt;= 0: txt = font.render("GAME OVER", True, (220, 50, 50)) screen.blit(txt, (WIDTH//2 - txt.get_width()//2, HEIGHT//2))</description></item><item><title>Session 5: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/05-breakout/demo/index.html</guid><description>Run the solution: cd code/sessions/05-breakout/solution &amp;&amp; uv run python main.py.
Record ~12 seconds: break several rows of bricks and let the score go up. Save as breakout.gif in static/sessions/05-breakout/. Target size: ~600×500 pixels, ≤ 2 MB.</description></item></channel></rss>