Pygame error: display surface quit: Why?

前端 未结 8 1516
孤独总比滥情好
孤独总比滥情好 2020-12-04 01:54

Can anyone tell me why my app quits with:

pygame error: display Surface quit.

8条回答
  •  囚心锁ツ
    2020-12-04 02:25

    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 was:

        Traceback (most recent call last):
          File "", line 1, in 
          File "bounce.py", line 22, in 
            screen.fill(black)
        pygame.error: display Surface quit
    

    So I put

        import pdb
    

    at the top after

        pygame.init()
    

    and used

        pdb.set_trace()
    

    after the line with

        pygame.quit()
    

    Now I ran the program, clicked to close the window and was actually a bit surprised to see that I fell into the debugger (after all, I expected the quit to completely take me out immediately). So I concluded that the quit doesn't actually stop everything at that point. Looked like the program was continuing beyond the quit, was reaching

        screen.fill(black)
    

    and this was causing the problem. So I added

        break
    

    after the

        pygame.quit()
    

    and all works happily now.

    [ Added later: It occurs to me now that

        pygame.quit()
    

    is quitting the module, and not the program that is running, so you need the break to get out of this simple program. ]

    Just for the record, this means the good version is

        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)
    

提交回复
热议问题