Stage 3 ยท COLOURS

Colours โ€” give them friendly names

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.

๐Ÿง  Quick Recap (30 seconds)

Before we dive in โ€” can you remember from Lesson 2 (Setup Screen)?

Q1. What does 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.

Q2. What is the Clock for?

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.

Where we are on the rocket

Rocket Ladder infographic โ€” Stage 3 highlighted by arrows. โ–ถ โ—€

You're on Stage 3 of 10. Welcome to The Body of the rocket. ๐Ÿ’ช

What are colours, really?

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:

๐ŸŽจ Try opening the Google colour picker and copy the "RGB" values it shows. You can use those numbers directly in pygame.

So why give them names?

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."

The recipe

  1. After your imports and screen setup, add a section called # COLOURS.
  2. Pick a name (in ALL_CAPS) and a (R, G, B) value for each colour you'll use.
  3. Save each pair into a variable: RED = (255, 0, 0).
  4. Anywhere you'd type the numbers, type the name instead.
  5. Run the game โ€” if a colour needs tweaking, change ONE place and it updates everywhere.

The code so far

# 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.

Try it yourself

Type the code into this in-browser editor and press Run

Type the code above into the editor, then click โ–ถ Run.

๐Ÿ† Mini-challenges

Handouts for this lesson

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.

โ† Previous Lesson 2 ยท Setup Screen