def main(): # Display menu for player pygame.font.init() screen = pygame.display.set_mode((640, 480), 0, 32) menu = KezMenu( ["New Game", lambda: optionX("new_game")], ["Load Game", lambda: optionX("load_game")], ["Quit!", lambda: optionX("quit")], ) menu.mouse_enabled = False while True: events = pygame.event.get() menu.update(events) screen.fill((150, 150, 150, 255)) menu.draw(screen) pygame.display.flip() if OPTION_SELECTED: break if OPTION_SELECTED == "quit": print("Aww, that's too bad! See you next time!") pygame.quit() sys.exit() else: # Initialize game object game = Game(S_WIDTH, S_HEIGHT, P_WIDTH, P_HEIGHT, TILESIZE, WHITE) # Load last saved game if it exists if OPTION_SELECTED == "load_game": if (game.save_exists()): game.load() # Start game game.run() # When exiting save game game.save(game.get_tiles()) # Close game pygame.quit() sys.exit()
def option3(): global OPTION_SELECTED OPTION_SELECTED = 1 def optionX(p): global OPTION_SELECTED OPTION_SELECTED = p menu = KezMenu( ["First option!", option1], ["sEcond", option2], ["Again!", option3], ["Lambda", lambda: optionX(71)], ["Quit", sys.exit], ) menu.mouse_enabled = False def drawMenu(): screen.fill((150, 150, 150, 255)) menu.draw(screen) pygame.display.flip() pygame.display.set_caption("Example 1: Use the KezMenu freely") while True: events = pygame.event.get()
def __init__(self, game_opts): # initialize the state State.__init__(self, constants.SCENES['menu']) ## the game's command line options self.game_opts = game_opts ## the screen surface self.screen = pygame.display.get_surface() ## flag to control the settings menu's loop self.menu_settings_running = None ## flag to control the main menu's loop self.menu_main_running = True # enable key repeat for the menu pygame.key.set_repeat(MENU_KEY_DEL, MENU_KEY_INT) ## set the main menu's background self.menu_main_bg = graphics.load_image( constants.FILES['graphics']['menu']['main']['bg'][0])[0] ## set the settings menu's background self.menu_settings_bg = graphics.load_image( constants.FILES['graphics']['menu']['share']['bg'][0])[0] ## set the settings menu's background box self.menu_box_bg = graphics.load_image( constants.FILES['graphics']['menu']['settings']['box'][0])[0] ## set the window frame self.window_frame = graphics.load_image( constants.FILES['graphics']['menu']['share']['frame'][0])[0] ## set the mouse cursor self.mouse_cursor = graphics.load_image( constants.FILES['graphics']['menu']['share']['cursor'][0])[0] ## set the sound when a menu option is entered self.select_option_snd = sound_mixer.load_sound( constants.FILES['sounds']['menu']['share']['sel'][0]) ## create the main menu - string, callback function self.menu_main = KezMenu(self.game_opts, ['Play', self._play_option], ['Settings', self._settings_option], ['Credits', self._credits_option], ['Quit', self._quit_option]) # set the position of the main menu self.menu_main.set_position(MENU_MAIN_POS_X, MENU_MAIN_POS_Y) # set the main menu's font self.menu_main.set_font( graphics.load_font(constants.FILES['fonts']['menu']['share'][0], MAIN_FONT_SIZE)) # set the main menu's highlight color self.menu_main.set_highlight_color(MAIN_FOCUS_COLOR) ## create the settings menu - string, callback function self.menu_settings = KezMenu( self.game_opts, ['Fullscreen', self._toggle_fullscreen_option], ['Sounds', self._toggle_sounds_option], ['Music', self._toggle_music_option], ['Back', self._back_option]) # disable the menu graphic for focused options self.menu_settings.toggle_image() # set the settings menu's font self.menu_settings.set_font( graphics.load_font(constants.FILES['fonts']['menu']['share'][0], MENU_FONT_SIZE)) # set the position of the settings menu self.menu_settings.center_at(constants.SCREEN_WIDTH / 2.0, constants.SCREEN_HEIGHT / 2.0) # set the settings menu's highlight color self.menu_settings.set_highlight_color(SETTINGS_FOCUS_COLOR) ## the animated sprite group self.anim_sprites = pygame.sprite.RenderUpdates() # create the animated sprites sprite_num = len(constants.FILES['graphics']['menu']['share']['anim']) sprite_fact = SpriteFactory() for i in range(sprite_num): # create the "right" type of animated sprite using the factory r_sprite = sprite_fact.create_anim_sprite( i, constants.FILES['graphics']['menu']['share']['anim'][i], ANIM_SPRITE_SPEED) self.anim_sprites.add(r_sprite) ## create clock and track time self.clock = pygame.time.Clock()
def __init__(self, game_opts): State.__init__(self, constants.SCENES['menu']) self.game_opts = game_opts self.screen = pygame.display.get_surface() self.menu_settings_running = None self.menu_main_running = True pygame.key.set_repeat(MENU_KEY_DEL, MENU_KEY_INT) self.menu_main_bg = ResourceManager().getImage( constants.FILES['graphics']['menu']['main']['bg'][0]) self.menu_settings_bg = ResourceManager().getImage( constants.FILES['graphics']['menu']['share']['bg'][0]) self.menu_box_bg = ResourceManager().getImage( constants.FILES['graphics']['menu']['settings']['box'][0]) self.window_frame = ResourceManager().getImage( constants.FILES['graphics']['menu']['share']['frame'][0]) self.mouse_cursor = ResourceManager().getImage( constants.FILES['graphics']['menu']['share']['cursor'][0]) self.select_option_snd = sound_mixer.load_sound( constants.FILES['sounds']['menu']['share']['sel'][0]) # create the main menu - string, callback function self.menu_main = KezMenu(self.game_opts, ['Play' , self._play_option], ['Settings' , self._settings_option], ['Credits' , self._credits_option], ['Quit' , self._quit_option]) self.menu_main.set_position(MENU_MAIN_POS_X, MENU_MAIN_POS_Y) self.menu_main.set_font(graphics.load_font( constants.FILES['fonts']['menu']['share'][0], MAIN_FONT_SIZE)) self.menu_main.set_highlight_color(MAIN_FOCUS_COLOR) # create the settings menu - string, callback function self.menu_settings = KezMenu(self.game_opts, ['Fullscreen' , self._toggle_fullscreen_option], ['Sounds' , self._toggle_sounds_option], ['Music' , self._toggle_music_option], ['Back' , self._back_option]) # disable the menu graphic for focused options self.menu_settings.toggle_image() self.menu_settings.set_font(graphics.load_font( constants.FILES['fonts']['menu']['share'][0], MENU_FONT_SIZE)) self.menu_settings.center_at(constants.SCREEN_WIDTH / 2.0, constants.SCREEN_HEIGHT / 2.0) self.menu_settings.set_highlight_color(SETTINGS_FOCUS_COLOR) self.sprites = pygame.sprite.LayeredUpdates() sprites_number = len(constants.FILES['graphics']['menu']['share']['anim']) sprite_area = self.screen.get_rect() sprite_limiter = LimiterFactory().getInstance('Default') for i in range(sprites_number): sprite = MenuSprite(constants.FILES['graphics']['menu']['share']['anim'][i], (sprite_area.center), i, MAX_ALPHA, False, SPRITE_SPEED, sprite_area, 'Random') sprite.limiter = sprite_limiter self.sprites.add(sprite) self.clock = pygame.time.Clock()