Session 8: Sky Highway
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_xvariable - 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.pyin 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.
Find # STEP 1 and add:
# Move the background to the left
scroll_x -= speed
if scroll_x <= -WIDTH:
scroll_x = 0
# Draw the background twice
screen.blit(bg_image, (int(scroll_x), 0))
screen.blit(bg_image, (int(scroll_x) + WIDTH, 0))scroll_x starts at 0 and gets smaller every frame. When it reaches -WIDTH we jump back to 0: a seamless loop.
Click Run. The sky now scrolls past your ship!
Step 2: ✅ Basic: obstacles and lives
Obstacles come from the right and move to the left. If you hit them, you lose a life.
Updating obstacles (find # STEP 2):
for obs in obstacles[:]:
obs.x -= speed
if obs.x < -obs.width:
obstacles.remove(obs)
pygame.draw.rect(screen, RED, obs)
# Collision
if ship_rect.colliderect(obs):
obstacles.remove(obs)
lives -= 1
if lives <= 0:
running = FalseDrawing lives:
for i in range(lives):
pygame.draw.rect(screen, RED, (10 + i * 25, 10, 18, 18))Spawning obstacles (in the spawn timer event):
h = random.randint(40, 120)
obstacles.append(pygame.Rect(WIDTH, random.randint(50, HEIGHT - h - 50), 30, h))Step 3: ⭐ Stretch: collecting stars
Stars give points and disappear when you hit them.
Find # STEP 3:
for star in stars[:]:
star.x -= speed
if star.x < -20:
stars.remove(star)
continue
pygame.draw.circle(screen, YELLOW, star.center, 10)
if ship_rect.colliderect(star):
stars.remove(star)
score += 1
# Drawing the score:
score_surf = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_surf, (WIDTH - 120, 10))Spawning stars (in the same spawn timer, but less often):
if random.random() < 0.4:
stars.append(pygame.Rect(WIDTH, random.randint(40, HEIGHT - 40), 20, 20))Step 4: 🔥 Expert: spawning and scaling the speed
Now you make the game harder and harder as the score climbs.
Scaling the speed (find # STEP 4):
speed = BASE_SPEED + score * 0.1BASE_SPEED = 4 is already at the top. At score 20 everything goes twice as fast!
Adjusting the spawn interval: the higher the score, the more often new obstacles come:
spawn_interval = max(500, 1800 - score * 30) # min. 500ms
pygame.time.set_timer(SPAWN_EVENT, spawn_interval)Game over screen:
if not running:
go_surf = font.render("GAME OVER", True, RED)
screen.blit(go_surf, (WIDTH // 2 - go_surf.get_width() // 2, HEIGHT // 2 - 30))
sc_surf = font.render(f"Score: {score}", True, WHITE)
screen.blit(sc_surf, (WIDTH // 2 - sc_surf.get_width() // 2, HEIGHT // 2 + 10))
pygame.display.flip()
pygame.time.wait(3000)Showcase
Show your game to a coach and a buddy. Who gets the highest score?
See you next time!
“Next time: a boss that fights back, in THREE phases.”
Take it home
Try one of these extensions at home:
- Easy: add a second kind of obstacle (for example a blue one that flies higher or lower).
- Medium: add power-ups. Catch a green capsule for a temporary shield (the ship can be hit once).
- Hard: let the ship move up and down with the arrow keys, and keep it inside the screen so it can’t fly off.
- Very hard: add parallax scrolling: a second background layer (clouds) that scrolls slower than the first layer.
Stuck? Ask a coach at the next dojo, or just try something else. Programming is doing.
Dojo Defender: Your own version
Today you also keep building on Dojo Defender — but not with step-by-step instructions. You decide what you build.
Download the Dojo Defender starter — this is the complete game with everything in it (ship, enemies, asteroids, waves, boss). Pick a feature from the list, plan it briefly with a coach, and get started.
👉 Everything about Dojo Defender is on the Dojo Defender page.