Editor

The game loop

Pygame fundamentals · Lesson 1 of 7

Every pygame program is one loop that handles events, updates the game and draws a frame, about sixty times a second.

A window to draw on

pygame.init() starts pygame. pygame.display.set_mode((480, 320)) opens a window 480 pixels wide and 320 tall and returns a Surface: a picture you draw on, which programs usually call screen.

Drawing happens on a hidden copy of the screen. pygame.display.flip() shows it.

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 320))
screen.fill((48, 105, 152))  # paint the whole window blue
pygame.display.flip()        # show what was drawn
print("The window is", screen.get_width(), "by", screen.get_height())

The loop

The program above draws once and ends, so on a computer its window closes at once. A game keeps going with a while loop. Each pass through the loop is one frame, and it always does three things:

  • Handle events: key presses, mouse clicks, the window being closed.
  • Update the game: move things, check for collisions, change the score.
  • Draw the frame: fill the background, draw everything, then flip().

clock.tick(60) waits long enough that the loop runs at most 60 times a second, so the game runs at the same speed on a fast computer and a slow one.

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 320))
clock = pygame.time.Clock()

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

    # 2. Update the game (nothing moves yet)

    # 3. Draw the frame
    screen.fill((16, 18, 22))
    pygame.draw.circle(screen, (255, 212, 59), (240, 160), 40)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Something that changes every frame

A game is a loop where something changes between frames. Here x grows by 3 each frame, and x % 520 brings the ball back to the left once it has gone off the right edge.

The KEYDOWN event also ends the loop when Escape is pressed. In the browser, Stop ends it too.

import pygame

pygame.init()
screen = pygame.display.set_mode((480, 320))
clock = pygame.time.Clock()
x = 0

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False

    x = (x + 3) % 520  # move right, then start again on the left

    screen.fill((16, 18, 22))
    pygame.draw.circle(screen, (255, 212, 59), (x - 20, 160), 20)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Your turn

Change the programs above in the game panel, then press Run.

  • Change the background to a colour you like. Colours are (red, green, blue), each from 0 to 255.
  • Make the ball move twice as fast.
  • Make the ball fall from the top to the bottom instead of moving sideways.

Every classic game here uses this same loop. Find the three steps in Snake. Open Snake.