[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Question about Pygame-menu package
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Question about Pygame-menu package
- From: Alec Bennett <wrybread@xxxxxxxxx>
- Date: Sun, 23 Sep 2018 21:10:06 -0700
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Mon, 24 Sep 2018 00:10:35 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=ttpCPBww7MkEMcHGn5Xvqr4Qss4DBcbiD8u8WFNPg9g=; b=j81+ZeZEcgTZM1yFsWJEC8uvln8Zrk6+8yb1tZBOyEBoPC8Y0tHNaGnVA5F6OGTxsA 7AXGny21AYTjX4IfyQKNApchpZwGYMdmrHUuYY2HQxDaYgxiX7+2XhHGOWK7m52vJJEH bXkO9evKRS+3BVO7HQYli1pJYAJO9yMetMObCzSobfXYHQMeka0oZ8tJ+3O6hbkfkw+m +pFroNbhPvy012gIEfhXHKLv9dfmildW1VaSkm/sIXHorqzD0fzI2ZogzcR0gn4u1fdD txyyKU8Ighs0FHWmZjU0mRrt68KPDLjsFoVpNAwUrJ2GYQTPj8E87mafBYQZjAuqIBy2 v60A==
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
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()