1

Having researched this for hours, I cannot figure out why this error is being triggered. Here is the entire message:

pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "snake.py", line 37, in <module>
    redraw_window()
  File "snake.py", line 23, in redraw_window
    win.fill((0, 0, 0))
pygame.error: display Surface quit

When I run the program, the window opens and closes instantly. I'm running Python v3.7 via a conda virtual environment. And here is my code:

import pygame
pygame.init()

#----------------------------
# CONSTANTS
#----------------------------

window_width = 256
window_height = 256

win = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Snake Game')

#----------------------------
# CLASSES
#----------------------------

#----------------------------
# REDRAW WINDOW
#----------------------------

def redraw_window():
    win.fill((0, 0, 0))
    pygame.display.update()

#----------------------------
# MAIN GAME LOOP
#----------------------------
running = True
while running:

    # listen for window closure
    for event in pygame.event.get():
        if event.type == pygame.quit():
            run = False

    redraw_window()

pygame.quit()

I even tried passing 'win' into the redraw_window function and that changed nothing.

inexile
  • 35
  • 5

1 Answers1

0

pygame.quit() is a function and uninitialize all PyGame modules. When you do

if event.type == pygame.quit():

the function is called and all PyGame modules are uninitialized.

The type attribute of pygame.event.Event() object indicates the type of event. You need to compare the event type to the enumeration constant that identifies the event. The quit event is identified by pygame.QUIT (see pygame.event module):

Hence, you have to compete with pygame.QUIT instead of pygame.quit():

running = True
while running:

    # listen for window closure
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redraw_window()

pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174