Colours as Numbers
Part of the Python in a Game Window section of Coddy's Pygame journey. Lesson 4 of 33.
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
EasyFill the black window with night blue.
- Find
screen.fill((0, 0, 0)). - Change it to
screen.fill((20, 24, 48)).
Press Play: the window turns night blue. Then Check.
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.
comet_x = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Change things here, once a frame.
comet_x = comet_x + 3
if comet_x > 700:
comet_x = -60
screen.fill((0, 0, 0))
# Draw here, after the fill.
pygame.draw.circle(screen, (240, 230, 160), (500, 110), 80)
pygame.draw.circle(screen, (255, 255, 255), (comet_x, 300), 6)
pygame.display.flip()
clock.tick(60)
pygame.quit()
All lessons in Python in a Game Window
Practice on your own: Pygame playground