Menu
CoddyTech

Colours as Numbers

Part of the Python in a Game Window section of Coddy's Pygame journey. Lesson 3 of 32.

A colour on a screen is three numbers: how much red, green and blue light to mix, each from 0 to 255. This is RGB.

  • (255, 0, 0) is red, (0, 255, 0) green, (0, 0, 255) blue.
  • (0, 0, 0) is black, every light off; (255, 255, 255) is white, every light full.
  • (255, 255, 0) is yellow: red and green light together.
  • (20, 24, 48) is a little light with more blue in it: night blue.

Numbers in round brackets, separated by commas, make a tuple. Python keeps them together and in order, so (48, 24, 20) is another colour, a dark brown.

screen.fill(colour) paints the whole window in one colour. It runs at the start of the draw part on every frame, so each frame starts on a clean window.

challenge icon

Challenge

Easy

The window is black. Fill it with night blue instead: change screen.fill((0, 0, 0)) to screen.fill((20, 24, 48)). Every program after this one starts in night blue.

Try it yourself

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Night Sky")
clock = pygame.time.Clock()

# Set things up here, before the game loop.

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Change things here, once a frame.

    screen.fill((0, 0, 0))
    # Draw here, after the fill.

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

All lessons in Python in a Game Window

Practice on your own: Pygame playground