ppizarror / pygame-menu

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

How to read button presses? #434

Closed Merklins closed 1 year ago

Merklins commented 1 year ago

I still have a question about how to read the user's button clicks, I tried to do it with the code below, but it doesn't work.

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pressed_key = pygame.key.get_pressed()

    if pressed_key[pygame.K_SPACE]:
        print(1)
        GAME_MENU()

    clock.tick(FPS)

    pygame.display.update()
    main_menu.mainloop(screen)
ppizarror commented 1 year ago

Hi, check out https://pygame-menu.readthedocs.io/en/4.3.1/_source/create_menu.html#display-a-menu, in particular, menu can receive your events using mymenu.update(events).

Timer-clock example, https://pygame-menu.readthedocs.io/en/4.3.1/_source/gallery.html#timer-clock-example, illustrates a case with a conditional menu (can be turned on/off. Here you can see menu draw and update.

https://github.com/ppizarror/pygame-menu/blob/650a5d1b7ee1c2c514cf2283743d20deccc6f2b0/pygame_menu/examples/timer_clock.py#L232-L243

ppizarror commented 1 year ago

https://pygame-menu.readthedocs.io/en/4.3.1/_source/gallery.html#game-selector-example does the same. Note this example has two different "while True". One is for the main app, and the other one is for the play_function. I think this example is more similar to your project, see:

https://github.com/ppizarror/pygame-menu/blob/abf4cc1efab4f6797931e3bb261178446f7a73e7/pygame_menu/examples/game_selector.py#L89-L115

Merklins commented 1 year ago

Thank you so much!