Level 4 · pygame
Dodge the Blocks
Use LEFT/RIGHT arrow keys to dodge falling red blocks. Survive as long as you can — your score is how many seconds you survived.
What it teaches: Full game loop, collisions
How to run
Starter file: starter_pygame_game.py
python starter_pygame_game.pyStarter code
"""
LEVEL 4 - PYGAME
Starter: Dodge the Blocks
Run with: python starter_pygame_game.py
Use LEFT/RIGHT arrow keys to dodge the falling red blocks.
Survive as long as you can - your score is how many seconds you survived.
Your job: change it, upgrade it, make it yours!
"""
import pygame
import random
import sys
pygame.init()
WIDTH, HEIGHT = 480, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Level 4 - Dodge the Blocks")
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 36)
# ---- Player ----
player_width, player_height = 50, 20
player_x = WIDTH // 2 - player_width // 2
player_y = HEIGHT - 50
player_speed = 6
# ---- Falling blocks ----
block_width, block_height = 40, 40
blocks = [] # each block is [x, y]
block_speed = 4
spawn_timer = 0
spawn_interval = 40 # frames between new blocks
def spawn_block():
x = random.randint(0, WIDTH - block_width)
blocks.append([x, -block_height])
def collides(px, py, bx, by):
player_rect = pygame.Rect(px, py, player_width, player_height)
block_rect = pygame.Rect(bx, by, block_width, block_height)
return player_rect.colliderect(block_rect)
running = True
game_over = False
start_ticks = pygame.time.get_ticks()
while running:
dt = clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if not game_over:
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_x -= player_speed
if keys[pygame.K_RIGHT]:
player_x += player_speed
player_x = max(0, min(WIDTH - player_width, player_x))
spawn_timer += 1
if spawn_timer >= spawn_interval:
spawn_block()
spawn_timer = 0
for block in blocks:
block[1] += block_speed
blocks = [b for b in blocks if b[1] < HEIGHT]
for block in blocks:
if collides(player_x, player_y, block[0], block[1]):
game_over = True
# ---- draw ----
screen.fill((20, 20, 30))
pygame.draw.rect(screen, (0, 200, 100), (player_x, player_y, player_width, player_height))
for block in blocks:
pygame.draw.rect(screen, (200, 50, 50), (block[0], block[1], block_width, block_height))
if not game_over:
seconds_survived = (pygame.time.get_ticks() - start_ticks) // 1000
score_text = font.render(f"Time: {seconds_survived}s", True, (255, 255, 255))
else:
score_text = font.render("GAME OVER - close window to quit", True, (255, 255, 0))
screen.blit(score_text, (10, 10))
pygame.display.flip()
pygame.quit()
sys.exit()
# ---------------------------------------------------------------
# LEVEL UP! Try one (or more) of these upgrades:
# 1. Make blocks fall faster over time to increase difficulty.
# 2. Add a "power-up" (different color block) that gives bonus points
# instead of ending the game.
# 3. Add a restart key (e.g. press R) instead of closing the window.
# 4. Replace rectangles with images using pygame.image.load().
# 5. Add background music with pygame.mixer.
# 6. Ask your AI tool: "How do I add a high score that saves between
# game runs in Pygame?"
# ---------------------------------------------------------------
Challenge ideas
Easy
- Change player/block colors, sizes, and speeds.
- Change how fast new blocks spawn.
Medium
- Add a restart key (press R to play again after Game Over).
- Add a scoring system based on blocks dodged, not just time.
- Add a special "gold" block worth bonus points that doesn't end the game.
Hard
- Add real sprite images and animations.
- Add a start menu and pause screen.
- Add increasing difficulty levels (every 10 seconds, get faster).
Ask your AI tool
“How do I add a simple high-score save file to my Pygame game?”