Pygame.error: display surface quit

Explanation:

The error message “pygame.error: display surface quit” occurs when the display surface in Pygame is closed or has been quit, and your code tries to perform actions on it, such as updating the display or blitting images.

To understand this error better, let’s consider an example:


    import pygame

    pygame.init()
    screen = pygame.display.set_mode((800, 600))
    pygame.display.set_caption("Display Surface Quit Example")

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        # Perform other game logic
        
        screen.fill((255, 255, 255))  # Fill the screen with white color
        pygame.display.update()  # Update the display

    pygame.quit()  # Quit Pygame
  

In the above example, we initialize Pygame, create a display surface with a window size of 800×600 pixels, set the window caption, and enter the main game loop.

We capture events using the `pygame.event.get()` method and check for the QUIT event. If a QUIT event is detected (e.g., the user closes the window), we set the `running` variable to False and exit the game loop.

Inside the game loop, we perform other game logic and continuously update the display surface by filling it with white color using `screen.fill((255, 255, 255))` and then calling `pygame.display.update()`.

However, if we accidentally call `pygame.quit()` within the game loop, the display surface will be closed or quit, leading to the “pygame.error: display surface quit” error.

To fix this error, make sure to call `pygame.quit()` only once, preferably outside the game loop, when the game is meant to exit.

Leave a comment