I'm trying to add the beautiful menus from Pygame-menu (
https://github.com/ppizarror/pygame-menu) to my project, but having
trouble. In my attached stripped down sample, I'm trying to show menu1 when
the 1 key is pressed, and show menu2 when the 2 key is pressed. They're
both "main menus" launched from the root of the app, as opposed to one
being a submenu of the other.
My questions:
- how can I find out what item was selected in the menu? Clicking a
selection triggers the item_selected() function, but doesn't send any info
about which item was selected.
- how can I close a menu? Running menu1.disable() in item_selected()
doesn't close the menu... How do I close the menu when something is
selected?
Ideally in this sample I'd like to launch menu1 when the 1 key is pressed,
choose a selection, print info about what was selected, and return to the
root of the app. Then pressing keyboard 2 launches menu2, repeating the
process.
Thanks for any help, and apologies if I'm missing something obvious here.
import pygame, sys
from pygame.locals import *
import pygameMenu # This imports classes and other things
from pygameMenu.locals import * # Import constants (like actions)
'''
Testing pygame-menu. Keyboard 1 launches menu1, keyboard 2 launches menu2.
'''
# set up pygame
pygame.init()
clock = pygame.time.Clock()
# set up the window
surface = pygame.display.set_mode((800, 600) )
pygame.display.set_caption('test')
surface.fill((255,255,255))
font1 = pygame.font.SysFont(None, 48)
text = font1.render('The Main Window', True, (0,255,0), (255,0,0))
textRect = text.get_rect()
textRect.centerx = surface.get_rect().centerx
textRect.centery = surface.get_rect().centery
surface.blit(text, textRect)
# draw the window onto the screen
pygame.display.update()
def menu_bg():
"""
Background color of the main menu, on this function user can plot
images, play sounds, etc.
"""
surface.fill((40, 0, 40))
def item_selected():
print "item selected"
menu1.disable() # should close the menu? But isn't...
print menu1.is_enabled()
#create menu1
menu1 = pygameMenu.Menu(surface,
bgfun=menu_bg,
enabled=False,
font=pygameMenu.fonts.FONT_NEVIS,
menu_alpha=90,
onclose=PYGAME_MENU_CLOSE,
title='Main Menu',
title_offsety=5,
window_height=800,
window_width=600
)
menu1.add_option('choice1', item_selected)
menu1.add_option('choice2', item_selected)
menu1.add_option('choice3', item_selected)
menu1.add_option('Exit', PYGAME_MENU_BACK)
#create menu2
menu2 = pygameMenu.Menu(surface,
bgfun=menu_bg,
enabled=False,
font=pygameMenu.fonts.FONT_NEVIS,
menu_alpha=90,
onclose=PYGAME_MENU_CLOSE,
title='Menu 2',
title_offsety=5,
window_height=800,
window_width=600
)
menu2.add_option('menu2 choice1', item_selected)
menu2.add_option('menu2 choice2', item_selected)
menu2.add_option('menu2 choice3', item_selected)
menu2.add_option('menu2 Exit', PYGAME_MENU_BACK)
# run the game loop
while True:
clock.tick(60)
events = pygame.event.get()
# Execute main from principal menu if is enabled
menu1.mainloop(events)
menu2.mainloop(events)
for event in events:
if event.type == KEYDOWN:
# open menu1 if they press the 1 key
if event.key == 49:
print "enabling menu1!"
menu1.enable()
# open menu2 if they press the 2 key
elif event.key == 50:
print "enabling menu2!"
menu2.enable()
print event.key
if event.type == QUIT:
pygame.quit()
sys.exit()