Pygame window not responding
A frozen window or a “Not Responding” title almost always means the event queue isn't being read, or something is blocking the loop. Here's how to find it.
On this page
Cause 1: events aren't processed#
The operating system expects every window to answer messages regularly. Pygame answers them when you call pygame.event.get() (or pygame.event.pump()). If your loop doesn't, the window is marked "Not Responding" after a few seconds.
# Broken: no event handling, the window freezes and can't be closed
while True:
screen.fill("black")
pygame.display.flip()# Fixed
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill("black")
pygame.display.flip()
pygame.quit()Read the queue every frame, including during menus, pauses, loading screens and game-over screens.
Cause 2: something blocks the loop#
These calls stop the loop while they wait, so events pile up:
input()in the terminal: the game waits for typing in another window. Use key events or a text input box instead.time.sleep()orpygame.time.wait()for more than a moment, for example to pause between turns. Use a timer that counts down bydtinstead (see the memory game).- Long calculations, such as generating a big level or path-finding over a huge grid, done in one go. Split the work across frames, show a progress screen, or call
pygame.event.pump()periodically inside the loop. - Network requests. Run them in a thread and poll the result.
Cause 3: the close button doesn't end the program#
If the window stays open when you click ×, you're not handling pygame.QUIT, or you handle it but the loop continues. Set your loop flag to False (or return from your main function) and call pygame.quit() after the loop. Calling sys.exit() after pygame.quit() is fine too.
Cause 4: an infinite loop in your code#
A while loop inside a frame whose condition never becomes false freezes everything:
while player.y < 400: # if player.y never changes inside this loop, it never ends
player.y += velocity # velocity might be 0Game loops should have one main while loop; inner loops should always finish quickly.
In the browser#
In Pygame Online the browser needs your loop to yield every frame with await asyncio.sleep(0). The editor adds it automatically for typical loops and offers Force stop if a program blocks.
Debugging tips#
- Print a counter each frame to see whether the loop is still running.
- Press Ctrl+C in the terminal: the traceback shows the line that was executing.
- Comment out sections of the update phase until the freeze disappears.