Pygame - “error: display Surface quit”

2019-09-15 16:58发布

I've made a menu screen where clicking on a button leads to a different screen in the same window.

def main():
    import pygame, random, time
    pygame.init()

    size=[800, 600]
    screen=pygame.display.set_mode(size)
    pygame.display.set_caption("Game")
    done=False
    clock=pygame.time.Clock()

    while done==False:
        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            if event.type == pygame.QUIT:
                done=True
                break
            if button_VIEW.collidepoint(pos):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    print("VIEW.")
                    view()
                    break

         screen.fill(black)
            ...

def view():
    done=False
    clock=pygame.time.Clock()

    while done==False:
        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            if event.type == pygame.QUIT:
                done=True
                break
            ...

If possible, I'd like to know how I can avoid the error:

    screen.fill(black)
error: display Surface quit
>>> 

After looking at other questions on here, I tried adding breaks to the exits of any loops, but still the error occurs.

I understand the issue is that the program is trying to execute screen.fill(black) after the window has been closed, but I have no further ideas on how to prevent the error.

I appreciate any help. Sorry if it seems simple.

1条回答
\"骚年 ilove
2楼-- · 2019-09-15 17:26

Several possibilities:

  • end the process (with e.g. sys.exit()) in the view function. Not ideal.
  • return a value from the view function to indicate that the application shoud end (e.g. return done), and check for that return value in the main function (if done: return). Better.
  • make done global and check for its value in the main function. I really would not like this solution.
  • my favourite: avoid multiple event loops altogether, so the problem solves itself (so you could just e.g. exit the main function with return).
查看更多
登录 后发表回答