UMPSA STEM LAB · Game Development with Python
Learn programming concepts step by step — from variables to machine learning — through a real Pygame project.
Welcome to the UMPSA STEM Lab Learning Module on Python Programming. In this module, you will learn core programming concepts not through abstract exercises, but by building something real — the classic Snake game.
Along the way, you will also develop Computational Thinking skills — learning how to break problems into parts, spot patterns, and design step-by-step solutions. These are the same skills professional programmers use every day.
The Python foundation you build here will carry directly into your next challenge: applying these skills to Raspberry Pi IoT projects, where your code controls real-world hardware.
This is what you will build. The AI snake runs automatically — use arrow keys or click the button to take control. Collect bonus food ⭐ for 3× points!
Before writing any code, a programmer decomposes the problem — breaks it into smaller, manageable pieces. Click each game component below to discover what Python concept it maps to.
Where is it on the grid?
(col, row)How is the full snake stored?
[(col,row), …]insert(0, head) and shrinks with pop(). Lists are perfect here because the snake's length changes constantly.How does it appear randomly?
(randint(…), randint(…))random.randint() and stored as a tuple. Worth 1 point.How does it appear periodically?
[(col, row, timer), …]frame_count % 300 == 0 triggers spawning every 300 frames. Worth 3 points, disappears when timer hits 0.What kind of value is it?
score = 0How is the game board defined?
GRID_COLS = 20, GRID_ROWS = 15, CELL_SIZE = 25How does the snake know which way to go?
"RIGHT", "LEFT", "UP", "DOWN"MOVES = {"RIGHT":(1,0), …} — cleaner and faster.How does the game control speed?
frame_count += 1frame_count % N == 0 triggers periodic events like bonus food spawning.✅ Click all 8 components — then discuss: which components depend on each other?
An algorithm is a precise sequence of steps. For each function below, click the steps in the correct order (1 → 2 → 3 …). Then check your answer.
The game loop runs every frame. What order do these steps happen in? Click them 1 → 2 → 3 → 4 → 5 → 6.
direction variablenew_head = (head_x + dx, head_y + dy)What exactly happens when the snake eats regular food? Click the steps in order.
snake.pop() — the tail stays, making the snake one cell longerspawn_food() to place new food at a random empty cell(col, row)score and insert the new head at position 0 of the body listscore > high_score — if so, update and save the high scoreThe bonus food spawns periodically and disappears. Describe its lifecycle in order.
(col, row, 240) is appended to the bonus_foods listframe_count % 300 == 0 evaluates to True — it's time to spawn a new bonus foodfor bf in bonus_foods: bf[2] -= 1How does the game detect a self-collision (snake hits its own body)?
for segment in snake_body[1:]:running = False — the game loop exits on the next framenew_x = new_x % GRID_COLS so the snake crosses the wall instead of dyingsegment == new_head, a collision is detectedTen milestones from zero to a machine-learning snake. Each step builds on the last.
Which concept is first introduced at each step — and where it reappears.
| STEP | Variables | Tuples | Conditionals | Loops | Lists | Functions | Classes / OOP | Sets | Dicts | Modulo % | File I/O | Try / Except | f-strings | Random | numpy / ML | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 · Variables & Setup | ★ | ★ | ||||||||||||||
| 2 · Loop & Events | ● | ★ | ● | |||||||||||||
| 3 · Direction & Movement | ● | ● | ★ | ● | ||||||||||||
| 4 · Snake Body ⭐ | ● | ● | ● | ● | ★ | |||||||||||
| 5 · Bonus Food 🌟 | ● | ● | ● | ● | ● | ★ | ||||||||||
| 6 · Functions | ● | ● | ● | ● | ● | ★ | ● | |||||||||
| 7 · Wrap-Around ⭐ | ● | ● | ● | ● | ● | ● | ★ | ★ | ● | |||||||
| 8 · Save High Score | ● | ● | ● | ● | ● | ● | ● | ● | ★ | ★ | ★ | ● | ||||
| 9 · OOP Refactor | ● | ● | ● | ● | ● | ● | ★ | ★ | ● | ● | ● | ● | ● | ● | ||
| 10 · ML Extension 🤖 | ● | ● | ● | ● | ● | ● | ● | ● | ● | ● | ★ |
Scroll right to see all concepts. ★ = first time this concept appears.
Make sure your computer is ready.
Download from python.org and run the installer. Tick "Add Python to PATH".
Open a terminal (or Thonny's Shell) and type: pip install pygame
Thonny is a beginner-friendly Python editor. Download from thonny.org if not already installed.
import pygame; print(pygame.version.ver) without errors, you are set!