ppizarror / pygame-menu

A menu for pygame. Simple, and easy to use
https://pygame-menu.readthedocs.io/
Other
555 stars 141 forks source link

[BUG] Menu closes for no reason #452

Closed leestarb closed 1 year ago

leestarb commented 1 year ago

Environment information Describe your environment information, such as:

The problem I start my menu with the mainloop method. In the menu I choose button to start my game. In my game I use while loop to draw and process my game. When I get pygame.QUIT event I break the loop. The bgfun function is called 1 time, I see black screened window and the process instantly ends with 0 code.

Expected behavior After breaking the loop, the menu must continue running.

Additional context I started experiencing this problem after making my game object oriented. So I use a class to control the menu and a class to process my game. I also start my game from outer scope (I create my menu controller class, which initialises pygame and menu and then runs it with mainloop method with pygame.display.get_surface() surface. I was not experiencing this issue when everything was in the same file, I just had several functions to manage the menu and the gameplay!

So I guess the problem is in that the menu loop dies? Please help!

ppizarror commented 1 year ago

Hello, can you provide a minimal working example to test this bug? Thanks

leestarb commented 1 year ago

Hello, can you provide a minimal working example to test this bug? Thanks

Hello, dear ppizarror.

Here is a simple example of the issue: https://tempfile.io/en/ynBUL0wGEjlVI8g/file (This file will be deleted after 7 days!)

bluefish2020 commented 1 year ago

Calling pygame.event.clear() after your play loop has exited seems to make it behave like you are expecting:

        def start_playing():
            Gameplay().play()
            pygame.event.clear()  # Prevent menu from seeing pygame.QUIT
            self.configure_window()  # This is needed to return menu's size and caption

That lets the menu's internal loop resume without exiting.

I haven't driven the game menus using menu.mainloop() in my project so there might be another approach I am oblivious of.

leestarb commented 1 year ago

Calling pygame.event.clear() after your play loop has exited seems to make it behave like you are expecting:

        def start_playing():
            Gameplay().play()
            pygame.event.clear()  # Prevent menu from seeing pygame.QUIT
            self.configure_window()  # This is needed to return menu's size and caption

That lets the menu's internal loop resume without exiting.

I haven't driven the game menus using menu.mainloop() in my project so there might be another approach I am oblivious of.

Thanks! That is right direction. I'm investigating the event queue. Looks like it is a bug of pygame.

leestarb commented 1 year ago

I started using pygame.event.get(pygame.QUIT) to check for quitting. Instead of this (as I used before) I should iterate over pygame.event.get() and check whether there is event with type pygame.QUIT. This solves the issue.

But also, using pygame.event.get(pygame.QUIT) and adding below pygame.event.clear() or pygame.event.get() works.

Not sure why this happens. That requires more investigation to understand the nature of this. At least, for me, the issue is solved.

Thank you for help, @bluefish2020 ! =)