📝 Worksheet← Lesson← Course
BitWithBite
Pygame Mastery · Quick Reference

Getting Started with Pygame Cheat Sheet

Pygame Mastery
In one line: The five lines every Pygame program shares — init, window, clock, event loop, quit — and why each one exists.

Key Ideas

1pygame.init(). Boots every Pygame subsystem (display, font, mixer). Always call it before touching anything else.
2The Display Surface. pygame.display.set_mode((w,h)) returns the screen Surface — the one object everything else gets blitted onto.
3The Game Loop. A while running: loop that repeats every frame: process events, update state, draw, flip.
4Clock & Frame Rate. clock.tick(60) caps the loop at 60 FPS and returns elapsed milliseconds (dt) since the last call.
5Events. pygame.event.get() returns this frame's queued events; always check for pygame.QUIT to let the window close.
6Quitting Properly. Set running=False on QUIT, exit the loop, then call pygame.quit() once, outside the loop.

Minimal Game Loop

import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
running = True
while running:
  for e in pygame.event.get():
    if e.type == pygame.QUIT:
      running = False
  screen.fill((5,9,26))
  pygame.display.flip()
  clock.tick(60)
pygame.quit()
InitEventsUpdateDraw
The core loop repeats roughly 60 times a second: Init runs once, then Events, Update, and Draw cycle forever.