0

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.

BaconStereo
  • 19
  • 1
  • 7
  • 1
    Please post a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve) that still produces the error and also the full traceback (error message). – skrx Mar 19 '17 at 21:25
  • @skrx apologies. I understand I maybe could have condensed the code a little more, but the code above is already quite cut down from the original. – BaconStereo Mar 19 '17 at 23:11
  • The problem is that we can't run the program and also can't see where the actual error happens. – skrx Mar 19 '17 at 23:19

1 Answers1

1

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).
sloth
  • 99,095
  • 21
  • 171
  • 219
  • Thanks for the help! But i'm not sure I understand your final suggestion; do you mean I should just carry out the process in `def view()` within the indentation of `if event.type == pygame.MOUSEBUTTONDOWN:`? – BaconStereo Mar 19 '17 at 23:09
  • @BaconStereo the last point is the best suggestion. I'd recommend to use [a state machine like this](https://www.reddit.com/r/pygame/comments/3kghhj/simple_state_machine_example/) by iminurnamez. This allows you to switch between different scenes/states and you need only one event and while loop in the `Game` (scene manager) class which passes the events to the currently active scene. – skrx Mar 20 '17 at 14:39
  • @BaconStereo You can find an example [here](http://stackoverflow.com/a/14727074/142637) – sloth Mar 21 '17 at 07:36
  • @sloth Thanks - very helpful! – BaconStereo Mar 21 '17 at 16:57