I had a similar problem in a very simple piece of code:
import sys, pygame pygame.init() size = width, height = 640, 480 speed = [2, 2] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("Golfball.png") ballrect = ball.get_rect() while 1: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() pygame.time.delay(5)
Error message:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "bounce.py", line 22, in <module> screen.fill(black) pygame.error: display Surface quit
So I put
import pdb
top after
pygame.init()
and used
pdb.set_trace()
after line with
pygame.quit()
Now I started the program, clicked to close the window, and actually was a little surprised to see that I got into the debugger (in the end, I expected that quitting smoking would completely take me out immediately). Therefore, I came to the conclusion that the throw does not really stop everything at that moment. It seems that the program continued beyond the exit, reached
screen.fill(black)
and it caused a problem. Therefore I added
break
after
pygame.quit()
and everything is working happily now.
[Added later: now it comes to me that
pygame.quit()
leaves the module, not the running program, so you need a break to exit this simple program. ]
For the record only, this means a good version
import sys, pygame pygame.init() size = width, height = 640, 480 speed = [2, 2] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("Golfball.png") ballrect = ball.get_rect() while 1: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() break ballrect = ballrect.move(speed) if ballrect.left < 0 or ballrect.right > width: speed[0] = -speed[0] if ballrect.top < 0 or ballrect.bottom > height: speed[1] = -speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() pygame.time.delay(5)