Debugging Pygame games
Games are hard to debug because things happen sixty times a second. These techniques make bugs visible: reading tracebacks, on-screen debug info, drawing hit boxes, pausing and stepping, and the bugs nearly everyone hits.
On this page
Read the traceback from the bottom#
When Python crashes, the last line names the error and the lines above show the path through your code. Find the lowest line that's in your file: that's usually where to look. TypeError: invalid rect points at a draw call with the wrong arguments; AttributeError: 'NoneType' object has no attribute 'rect' means something you expected to exist is None.
Put debug information on screen#
print() floods the terminal at 60 frames per second. Draw values in the corner of the game instead, and toggle them with a key:
debug = False
...
if event.type == pygame.KEYDOWN and event.key == pygame.K_F3:
debug = not debug
...
if debug:
lines = [f"FPS {clock.get_fps():.0f}", f"pos {player.pos.x:.1f}, {player.pos.y:.1f}",
f"vel {player.vel.x:.1f}, {player.vel.y:.1f}", f"on_ground {player.on_ground}"]
for i, text in enumerate(lines):
screen.blit(font.render(text, True, "white", "black"), (8, 8 + i * 20))Draw hit boxes#
Most collision bugs become obvious once you can see the rectangles:
if debug:
for sprite in all_sprites:
pygame.draw.rect(screen, "red", sprite.rect, 1)Seeing a hit box that's bigger than the art, offset from it or not moving with it explains most "it hit me when it shouldn't" reports. Try the collision visualizer to build intuition.
Pause and step#
Add a pause key that freezes updates but keeps drawing, plus a key that advances exactly one frame while paused. Fast bugs such as tunnelling through walls become visible one step at a time.
Use the debugger#
Insert breakpoint() where something goes wrong. Python stops there and opens the pdb prompt in the terminal: p player.rect prints a value, n runs the next line, c continues. Editors like VS Code and PyCharm give you the same with clickable breakpoints. Make breakpoints conditional so they don't stop every frame: if player.rect.top > 2000: breakpoint().
Log instead of printing#
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
logging.info("level %s loaded with %d enemies", level_no, len(enemies))Log events (level loaded, player died), not every frame. Switch to logging.DEBUG for more detail when needed.
Bugs nearly everyone hits#
| Symptom | Usual cause |
|---|---|
| Object doesn't move at low speeds | Fractions lost in a Rect; keep float positions. |
| Movement speed changes with FPS | Not multiplying by dt. |
| Items skipped when removing from a list | Modifying a list while looping over it; loop over a copy. |
| Something happens once per event, not per frame | Code indented inside the for event in ... loop. |
| Up is down | y grows downward on screen. |
| Old frames smear across the screen | Missing screen.fill() before drawing. |
| Changes to a "copy" affect the original | b = a shares the same Rect or list; use .copy(). |
| Black screen or freezing window | See black screen and not responding. |
Make bugs reproducible#
Random bugs are easier with a fixed seed: create your generator with random.Random(1234) while debugging so every run behaves the same. The game projects that use randomness accept a seed for exactly this reason, which is also what makes their automated tests possible.