Computers think in numbers. We think in words. Stage 3 is where we teach the
computer that (255, 0, 0) is just our friend RED.
Before we dive in โ can you remember from Lesson 2 (Setup Screen)?
pygame.init() do?It turns pygame ON. Starts all the parts of pygame (graphics, sound, fonts) so they're ready to use. Always run this once near the top.
clock.tick(60) makes the game run at 60 frames per second on any computer โ smooth on fast laptops, not too slow on old ones.
โถ
โ
You're on Stage 3 of 10. Welcome to The Body of the rocket. ๐ช
A screen is made of tiny dots called pixels. Every pixel can glow in three lights at once: Red, Green, and Blue. Mix the three lights at different brightness levels and you get every colour you've ever seen on a screen.
In pygame, a colour is just three numbers: (red, green, blue).
Each number is from 0 (off) to 255 (full brightness).
So:
(0, 0, 0) โ all lights off โ black(255, 255, 255) โ all lights on full โ white(255, 0, 0) โ only red โ pure red(0, 255, 0) โ only green โ pure green(255, 215, 0) โ red and a lot of green โ gold/yellow(0, 0, 50) โ a tiny bit of blue โ deep space-blue๐จ Try opening the Google colour picker and copy the "RGB" values it shows. You can use those numbers directly in pygame.
Imagine writing a whole game and seeing this everywhere:
pygame.draw.rect(screen, (255, 215, 0), player)
pygame.draw.rect(screen, (255, 215, 0), bullet)
screen.fill((0, 0, 50))
What does (255, 215, 0) mean again? You'd forget in 10 minutes.
Instead, we store each colour in a variable with a clear name:
YELLOW = (255, 215, 0)
SPACE = (0, 0, 50)
pygame.draw.rect(screen, YELLOW, player)
pygame.draw.rect(screen, YELLOW, bullet)
screen.fill(SPACE)
Same code, but readable. Anyone who looks at it (you, next month, or your friend) instantly knows what's going on. Programmers do this for almost everything โ it's one of the most important habits you'll learn.
โจ Style tip: colours that never change are written in
ALL_CAPS. That's how other coders know "don't change
this โ it's a constant."
# COLOURS.RED = (255, 0, 0).# Stage 1 โ Imports
import pygame
# Stage 2 โ Setup the Screen
pygame.init()
WIDTH = 800
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Galaxy Defender")
clock = pygame.time.Clock()
# Stage 3 โ Colours (R, G, B) โจ
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 215, 0)
SPACE = (0, 0, 50)
# Tiny test loop โ paint the screen SPACE blue and a YELLOW square
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(SPACE)
pygame.draw.rect(screen, YELLOW, (350, 250, 100, 100))
pygame.display.flip()
clock.tick(60)
pygame.quit()
Run this and you'll see a deep space-blue window with a glowing yellow square in the middle. Notice how the code reads like a sentence: "fill the screen SPACE, draw a YELLOW rectangle." That's the whole point of Stage 3. ๐
๐ฎ Want to play with more code? Scroll down to the Lesson 3 Code Library (5 colour games). Copy any program, paste it into the editor below, and press the green โถ Run button.
Type the code above into the editor, then click โถ Run.
PINK = (255, 105, 180) and use it for the square.SPACE to (50, 0, 0) โ what mood does the game have now?PURPLE and draw a second rectangle next to the first.YELLOW nicer than writing (255, 215, 0) over and over? Can you say it in your own words?Print these, keep them open in another tab, or download them to your computer.
๐ Lesson 3 Code Library โ 5 brand-new pygame mini-games all about colour: Palette Test Card, Rainbow Stripes, Traffic Light, RGB Mixer, Random Sky.
๐ Lesson 3 Code Library (5 colour games) ๐ Lesson 2 Code Library ๐ Lesson 1 Code Library
๐บ๏ธ The big-picture handouts โ keep these by your side for the whole course.
๐ Rocket Ladder ๐ Pixel Cheat Sheet ๐ Code Recipe ๐ผ๏ธ 6 Parts of a Turtle Program
๐ค Want to try your own idea? Ask an AI like Claude or ChatGPT something like "Write me a short pygame program that draws a rainbow", then paste the code into the Trinket above and click Run.