[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[pygame] Re: Question about Pygame-menu package
- To: pygame-users@xxxxxxxx
- Subject: [pygame] Re: Question about Pygame-menu package
- From: Alec Bennett <wrybread@xxxxxxxxx>
- Date: Sun, 23 Sep 2018 21:56:52 -0700
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Mon, 24 Sep 2018 00:57:24 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Pvwus/KoUaz/pY1tPI/HUXYifSPWwY5OaO9msurY/a8=; b=Z58uCaIgqudnY6iTxKMuw/1D6Jme0SvNNxxrdhlVq5x0OuBWuZ6bLZ835SgcMnD04R IprULHFR9Yd8f82fqLkiGHvGIRDVhZZyBzKiRxIo4UpXH5pKoPvQwbdd6iLGTTOBbpaQ ijYehV0EvklYqp+qnyIe+p8t6hZbqf9XJyOyTCiDhgw3oBSv3odCuKn1m1wlVgXKS/ZJ ec6mQBqNuHPWB0Lvb1/fcMwsflMGYhGvOWRRY2wbYvvC9TCjGmdfxLL1PiD9SdGDm1q8 g8apTf/mZ01HNRB0w85Af5Z45OXIVOZNeA8sznxCGBScK17mox0ANAxCPThcCWHogKg5 PUXQ==
- In-reply-to: <CACB9+z3AYgd3RgfYz1Kc92ReX7=HTyC1px_JxMdsNOrAHsjsEw@mail.gmail.com>
- References: <CACB9+z3AYgd3RgfYz1Kc92ReX7=HTyC1px_JxMdsNOrAHsjsEw@mail.gmail.com>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
Apologies, there were a few errors in my sample. Attached is a fixed version.
If anyone has any tips about how to close the menu, I'd love to hear. I can close it with the ESC key, but not by selecting an item.
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()
surface = pygame.display.set_mode((800, 600) )
pygame.display.set_caption('test')
# background for menus
def menu_bg():
"""
Background color of the main menu, on this function user can plot
images, play sounds, etc.
"""
surface.fill((40, 0, 40))
# triggered when menu item selected
def item_selected(arg):
print "item selected %s" % arg
menu1.disable() # should close menu1? But isn't...
menu2.disable() # should close menu2? But isn't...
print menu2.is_enabled()
#menu2.reset(100)
#menu2.reset(1)
#create menu1
menu1 = pygameMenu.Menu(surface,
bgfun=menu_bg,
enabled=False,
font=pygameMenu.fonts.FONT_NEVIS,
menu_alpha=90,
#onclose=PYGAME_MENU_CLOSE,
onclose=PYGAME_MENU_RESET, # If this menu closes (press ESC) back to main
title='Main Menu',
title_offsety=5,
window_height=800,
window_width=600
)
menu1.add_option('choice1', item_selected, "item1")
menu1.add_option('choice2', item_selected, "item2")
menu1.add_option('choice3', item_selected, "item3")
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,
#onclose=PYGAME_MENU_RESET,
onclose=PYGAME_MENU_RESET, # If this menu closes (press ESC) back to main
#onreturn=change_color_bg, # Action when pressing return on a element
title='Menu 2',
title_offsety=5,
window_height=800,
window_width=600,
menu_height=400,
menu_width=600
)
menu2.add_option('menu2 choice1', item_selected, "itemmmm1")
menu2.add_option('menu2 pygame_menu_back', PYGAME_MENU_BACK)
menu2.add_option('menu2 pygame_menu_close', PYGAME_MENU_CLOSE)
menu2.add_option('menu2 pygame_menu_exit', PYGAME_MENU_EXIT)
# run the game loop
while True:
clock.tick(60)
# fill the bg and draw some text
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)
pygame.display.update()
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()