<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Session 8: Sky Highway :: CoderDojo Python &amp; Pygame</title><link>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/index.html</link><description>Your ship flies through an endless highway in the sky. The background scrolls past you, red obstacles come from the right, and stars give points. The longer you survive, the faster it gets!
What you’ll learn today Making a scrolling background with a scroll_x variable Spawning objects that move from right to left Collision detection with lives Difficulty scaling: raising the speed as the score climbs Step 0: Setup Open Thonny. Download the starter via this link, unpack the ZIP and open main.py in Thonny. Click Run. You see a static screen with a ship and a sky background, but nothing moves yet. Problems? Ask a coach. Step 1: Let the background scroll A scrolling background gives the illusion of movement. We draw the background twice next to each other and shift the position every frame.</description><generator>Hugo</generator><language>en</language><atom:link href="https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/index.xml" rel="self" type="application/rss+xml"/><item><title>Session 8: Coach notes</title><link>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/mentor-notes/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/mentor-notes/index.html</guid><description>Goal of this session At the end of the Basic checkpoint a ninja can:
Scroll the background seamlessly with the double-blit technique. Move obstacles from right to left and remove them when they are off the screen. Subtract lives on a collision and stop the game at 0 lives. What is not expected after session 8: stars, difficulty scaling, game over screen. Those are Stretch/Expert material.
Preparation (30 min before the session) The background image in the starter has to be exactly WIDTH pixels wide (800px by default) so the double blit works seamlessly. speed is a float. Point out to ninjas that pygame.Rect only accepts integers. The starter already uses int(scroll_x); check that ninjas don’t remove it. Spawn timer: the starter uses pygame.USEREVENT + 1. Explain briefly how pygame.time.set_timer works: it sends an event automatically every N milliseconds. Schedule (3 hours) Time Activity 0:00 to 0:10 Welcome, looking back at session 7 0:10 to 0:20 Projector: explaining the scrolling background 0:20 to 1:30 Working on your own: steps 1 and 2 1:30 to 2:15 Stretch (stars) 2:15 to 2:45 Expert (scaling the speed) + buffer 2:45 to 3:00 Showcase + cliffhanger Common questions “The background flickers” int(scroll_x) is missing when blitting. pygame.Surface.blit expects integers; if you pass a float, pygame makes an int of it internally, but this can cause frame consistency problems. Check both blit calls.</description></item><item><title>Session 8: Cheatsheet</title><link>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/cheatsheet/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/cheatsheet/index.html</guid><description>Scrolling background scroll_x = 0 speed = 4 # Every frame: scroll_x -= speed if scroll_x &lt;= -WIDTH: scroll_x = 0 screen.blit(bg_image, (int(scroll_x), 0)) screen.blit(bg_image, (int(scroll_x) + WIDTH, 0)) Spawning objects from the right # Create an obstacle (off the right of the screen): obs = pygame.Rect(WIDTH, random.randint(50, HEIGHT - 100), 30, 80) obstacles.append(obs) # Move to the left every frame: for obs in obstacles[:]: obs.x -= speed if obs.x &lt; -obs.width: obstacles.remove(obs) Spawn timer with USEREVENT SPAWN_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(SPAWN_EVENT, 1500) # every 1.5 seconds for event in pygame.event.get(): if event.type == SPAWN_EVENT: obstacles.append(pygame.Rect(WIDTH, ...)) Drawing lives for i in range(lives): pygame.draw.rect(screen, RED, (10 + i * 25, 10, 18, 18)) Scaling the speed BASE_SPEED = 4 speed = BASE_SPEED + score * 0.1 # Scaling the spawn interval: interval = max(500, 1800 - score * 30) pygame.time.set_timer(SPAWN_EVENT, interval) Collision with lives if ship_rect.colliderect(obs): obstacles.remove(obs) lives -= 1 if lives &lt;= 0: running = False Game over screen # After the while loop: screen.fill(BLACK) go = font.render("GAME OVER", True, RED) screen.blit(go, (WIDTH // 2 - go.get_width() // 2, HEIGHT // 2)) pygame.display.flip() pygame.time.wait(3000)</description></item><item><title>Session 8: Demo recording</title><link>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/demo/index.html</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://python.coderdojohasselt.be/en/sessions/python/08-sky-highway/demo/index.html</guid><description>Session 8 uses pure pygame. Show a GIF of the solution on the session page.
How to record Run the solution: cd code/sessions/08-sky-highway/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 ~15 seconds: show the scrolling, dodging obstacles, catching a star and the game over screen. Save as sky-highway.gif in static/sessions/08-sky-highway/. Link to it in the worksheet via: ![Demo](/sessions/08-sky-highway/sky-highway.gif). Target size: ~800×500 pixels, ≤ 2 MB.</description></item></channel></rss>