<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 4: Pong :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/04-pong/index.html</link><description>Two paddles, one ball: the classic game of Pong. But there’s a surprise: this session you write the game in real pygame, without pgzrun. You learn to set up the game loop yourself. That’s how most real games work.
What you’ll learn today The pygame game loop: events, update, draw pygame.Rect for rectangles and collisions Making the ball bounce (reversing the speed) Keeping score for two players Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py. Click Run. You see two paddles and a yellow ball in the middle, but the ball doesn’t move yet. Difference with pgzrun: There are no draw() and update() functions any more. Everything sits in one while running: loop.</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/04-pong/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 4: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/04-pong/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/04-pong/mentor-notes/index.html</guid><description>Goal of this session After the Basic checkpoint a ninja can:
Explain what the pygame game loop is and how it differs from pgzrun. Make the ball move and bounce off the top and bottom edge and off both paddles. Understand what pygame.Rect is and how colliderect works. This is the first session with real pygame; expect confusion about the game loop. That’s normal.
Preparation Starter ZIP ready. Test on the dojo laptop whether pygame is installed (not pgzero). Prepare a short explanation: “pgzrun did the loop for us. Now we do it ourselves.” Dojo Defender: ninjas migrate their project to pygame as well (the starter contains the basics). Schedule (3 hours) Time Activity 0:00 to 0:15 Looking back + explaining pygame vs pgzrun 0:15 to 0:45 Step 1 to 2: moving the ball + bouncing 0:45 to 1:30 Step 3: keeping score 1:30 to 2:00 Step 4: win condition + speed 2:00 to 2:30 Dojo Defender: pygame migration 2:30 to 2:50 Showcase: 1-vs-1 matches 2:50 to 3:00 Cliffhanger Common questions “The ball moves but disappears straight away” ball_dx and ball_dy are 0. The ninja hasn’t changed the starting values. Point at the line ball_dx, ball_dy = 0, 0.</description></item><item><title>Session 4: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/04-pong/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/04-pong/cheatsheet/index.html</guid><description>Pygame game loop import pygame, sys pygame.init() screen = pygame.display.set_mode((600, 400)) 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() # --- update --- # --- draw --- pygame.display.flip() Rectangle (Rect) paddle = pygame.Rect(x, y, breedte, hoogte) paddle.y -= 5 # moving paddle.colliderect(andere_rect) # collision → True/False Keyboard in pygame keys = pygame.key.get_pressed() if keys[pygame.K_w]: paddle.y -= 5 if keys[pygame.K_s]: paddle.y += 5 if keys[pygame.K_UP]: paddle.y -= 5 if keys[pygame.K_DOWN]: paddle.y += 5 Ball bounces off the walls if ball.top &lt;= 0 or ball.bottom >= HEIGHT: ball_dy = -ball_dy Ball bounces off the paddle if ball.colliderect(paddle): ball_dx = -ball_dx Text in pygame font = pygame.font.SysFont(None, 48) txt = font.render(f"{score}", True, (255, 255, 255)) screen.blit(txt, (x, y)) Drawing a rectangle pygame.draw.rect(screen, (255, 255, 255), paddle) pygame.draw.ellipse(screen, (255, 220, 50), ball)</description></item><item><title>Session 4: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/04-pong/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/04-pong/demo/index.html</guid><description>Run the solution: cd code/sessions/04-pong/solution &amp;&amp; uv run python main.py.
Record ~10 seconds: play a few rallies and let the score go up. Save as pong.gif in static/sessions/04-pong/. Target size: ~600×400 pixels, ≤ 2 MB.</description></item></channel></rss>