SeeThinkExploreMarvel

UMPSA STEM LAB · Step 1 of 10

Step 1 — Variables & Setup

Understand pixels, colours and data types — then write the code that opens your Snake game window.

VariablesTuples int / str / tupleRGB ColoursPixel Coordinates
Step 1 / 10

What you'll build in Step 1

By the end of this step you will have a working Python program that opens a dark game window — the blank canvas your Snake game will be drawn on.

🎯 Learning goals

Understand pixels, screen coordinates, RGB colours, Python data types, and how to store values in variables.

🐍 Snake game connection

Every constant you write here (GRID_COLS, CELL_SIZE, colours) is used in all nine steps. Get them right once, use them everywhere.

Big idea A variable is a named box that holds a value. You give it a name; Python remembers the value. Constants (written in UPPER_CASE) are variables whose values you never change after setting them.

Pixels & Coordinates

Every image on a screen is made of tiny coloured squares called pixels. Pygame uses an (x, y) coordinate system to identify each one.

How the coordinate system works

Unlike a maths graph, the Pygame y-axis is flipped: (0, 0) is the top-left corner, and y increases downward.

👆 Hover over a cell to see its grid position and pixel x / pixel y

From grid to pixels

The snake game uses a grid of cells, not individual pixels. Each cell is CELL_SIZE × CELL_SIZE pixels. To find the pixel position of a cell:

pixel_x = col  * CELL_SIZE    # e.g. column 3  →  3 × 30 = 90 px from left
pixel_y = row  * CELL_SIZE    # e.g. row 2     →  2 × 30 = 60 px from top
Why this matters for Snake Every snake segment and every piece of food is drawn using this formula. When you store a snake cell as (col, row), you multiply by CELL_SIZE to get the actual screen coordinates before calling pygame.draw.rect().

What does Step 1 produce?

When you run the Step 1 program, Pygame opens a 600 × 450 pixel window, fills it with the dark background colour, holds for 3 seconds, then closes.

Snake

How the window size is calculated

Instead of typing 600 and 450 directly, we calculate them from the grid constants. This way, changing GRID_COLS automatically changes the window — we only update one number:

GRID_COLS = 20
CELL_SIZE = 30

WINDOW_W = GRID_COLS * CELL_SIZE   # 20 × 30 = 600 pixels
WINDOW_H = GRID_ROWS * CELL_SIZE   # 15 × 30 = 450 pixels

Colours — RGB Tuples

Every colour on a screen is mixed from three light channels: Red, Green, and Blue. Each channel is an integer from 0 (off) to 255 (full brightness).

Python: ( 0, 200, 80)
rgb(0, 200, 80)

🔴 Red = 0 → no red

🟢 Green = 200 → lots of green

🔵 Blue = 80 → a little blue

The colours used in Snake

VariableTupleSwatchUsed for
GREEN(0, 200, 80) Snake body segments
DKGREEN(0, 160, 50) Snake head (darker so it stands out)
RED(220, 50, 50) Food circle
WHITE(255, 255, 255) Score text on screen
BG(30, 30, 30) Background fill every frame

Data Types in Python

Python automatically assigns a type to every value. The type determines what you can do with the value. Step 1 uses three types.

int — Integer

A whole number. Used for grid dimensions, speed, and pixel sizes.

GRID_COLS = 20
CELL_SIZE = 30
SNAKE_SPEED = 8

tuple — Ordered group

An unchangeable sequence in ( ). Perfect for RGB colours and later for (col, row) positions.

GREEN = (0, 200, 80)
BG = (30, 30, 30)

str — String

Text in quotes. Used for the window title and later for direction names like "RIGHT".

"Snake"
"RIGHT"
"GAME OVER"

Why tuples for colours?

Tuple = sealed container A tuple cannot be changed after creation. Colour values should never change mid-game, so a tuple is the correct choice — it makes the intent clear. Later you'll use tuples for snake body positions (col, row) too, for the same reason: a cell's identity shouldn't accidentally mutate.

Checking types in Python

You can always ask Python what type a value is:

print(type(20))           # <class 'int'>
print(type((0, 200, 80))) # <class 'tuple'>
print(type("Snake"))      # <class 'str'>
print(type(3.14))         # <class 'float'>  ← decimals

Variables — Named Boxes in Memory

When Python runs GRID_COLS = 20, it sets aside a slot in memory, labels it GRID_COLS, and stores the value 20 there. Any time the program needs that value, it reads it by name.

GRID_COLS → 20 int
GRID_ROWS → 15 int
CELL_SIZE → 30 int
WINDOW_W → 600 int   (= 20 × 30)
GREEN → (0, 200, 80) tuple
RED → (220, 50, 50) tuple
BG → (30, 30, 30) tuple
UPPER_CASE naming convention Python doesn't enforce constants — you could change GRID_COLS. But writing it in capitals is a signal to yourself and other programmers: "don't change this". It's a convention, not a rule.

How Step 1 Connects to the Snake Game

Every variable you define in Step 1 is reused unchanged in Steps 2–9. Changing a constant here changes the whole game.

GRID_COLS / GRID_ROWS →
Defines the playing field size. The snake's (col, row) position must stay within 0–19 and 0–14. Used in: Steps 4, 6, 7, 8, 9
CELL_SIZE →
Converts grid coordinates to pixels. col × CELL_SIZE gives the screen x position. Used in: every step that draws anything (Steps 1–9)
SNAKE_SPEED →
Controls how many times per second the game loop runs (frames per second). Higher = faster snake. Used in: Steps 2–9 via clock.tick(SNAKE_SPEED)
GREEN / DKGREEN →
Colour for body segments and head. Head uses DKGREEN so students can see which end is leading. Used in: Steps 4–9 in pygame.draw.rect()
RED →
Colour for the food circle drawn with pygame.draw.circle(). Used in: Steps 4–9
BG →
The game loop calls screen.fill(BG) every frame to erase the previous frame before redrawing. Used in: Steps 2–9 — essential for animation

The Code — Step 1

Type this into Thonny and run it. A dark window titled "Snake" will appear for 3 seconds, then close.

# Step 1-1: Import the Pygame library so we can use its functions
import pygame
pygame.init()

# Step 1-2: Constants — grid size (never change these mid-game)
GRID_COLS   = 20        # 20 columns → 20 cells wide
GRID_ROWS   = 15        # 15 rows    → 15 cells tall
CELL_SIZE   = 30        # each cell is 30 × 30 pixels
SNAKE_SPEED = 8         # game loop runs 8 times per second

# Step 1-3: Colour constants — tuples of (Red, Green, Blue), 0-255 each
WHITE   = (255, 255, 255)   # score text
GREEN   = (  0, 200,  80)   # snake body
DKGREEN = (  0, 160,  50)   # snake head (darker)
RED     = (220,  50,  50)   # food
BG      = ( 30,  30,  30)   # background — very dark grey

# Step 1-4: Calculate window size in pixels
WINDOW_W = GRID_COLS * CELL_SIZE   # 20 × 30 = 600 px wide
WINDOW_H = GRID_ROWS * CELL_SIZE   # 15 × 30 = 450 px tall

# Step 1-5: Create the window and set its title
screen = pygame.display.set_mode((WINDOW_W, WINDOW_H))
pygame.display.set_caption("Snake")

# Step 1-6: Fill with background colour and show the window
screen.fill(BG)
pygame.display.flip()

# Step 1-7: Wait 3 seconds so you can see it, then quit
pygame.time.wait(3000)
pygame.quit()

You've covered all the concepts for Step 1. Time to apply them.

Start Activities → Tier 1 → 2 → 3 → 4