Menu
CoddyTech

Fill the Background

Part of the The Game Loop section of Coddy's Pygame journey. Lesson 2 of 32.

Pygame describes a colour as three numbers: how much red, green and blue it has, each from 0 to 255. This is called RGB.

  • (255, 0, 0) is red.
  • (0, 0, 0) is black, (255, 255, 255) is white.
  • (30, 30, 46) has a little more blue than red and green: a dark blue.

screen.fill(color) paints the whole window in one colour. Games fill the screen at the start of every frame, which wipes out the last frame before the new one is drawn.

screen.fill((0, 0, 0))
pygame.display.flip()

Note the double brackets: the colour is one tuple, the only argument of fill.

challenge icon

Challenge

Easy

Inside the game loop, above pygame.display.flip(), fill the screen with the dark blue (30, 30, 46).

Try it yourself

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 360))
pygame.display.set_caption("Move the Square")

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

    pygame.display.flip()

pygame.quit()

All lessons in The Game Loop

Practice on your own: Pygame playground