From 28d650dd3e46343115dd98588a8220c81c1e883c Mon Sep 17 00:00:00 2001 From: Mike Date: Tue, 28 Mar 2023 14:51:17 +0200 Subject: [PATCH] show/hide LeveMenu when pressing escape --- project/main.py | 46 +++++++++++++++----------------- project/ui_elements/LevelMenu.py | 2 -- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/project/main.py b/project/main.py index 97b2a9a..43ac02c 100644 --- a/project/main.py +++ b/project/main.py @@ -53,19 +53,25 @@ if what_to_run == 'menu': level_menu_width = screen_width / 2 level_menu_height = screen_height / 2 level_menu = LevelMenu(parsed_levels_manager.levels, 0, 0, level_menu_width, level_menu_height, 3) + show_menu = False + def load_next_level(selected_level): + global show_menu generated_level.destroy_level() generated_level.load_level(selected_level) + destroy_menu() + show_menu = False + + + def destroy_menu(): + global level_text_label + for level_text_label in level_menu.level_text_labels: + sprite_manager.remove_ui_element(level_text_label) + level_menu.level_select_listener = lambda selected_level: load_next_level(selected_level) - for level_text_label in level_menu.level_text_labels: - sprite_manager.add_ui_element(DrawLayers.UI, level_text_label) - - # for testing - last_click_pos = (0, 0) - while True: clock.tick(frame_rate) @@ -76,34 +82,26 @@ if what_to_run == 'menu': for event in click_events: for layer in sprite_manager.layers: for sprite in sprite_manager.layers[layer]: - last_click_pos = event.world_position - if sprite.get_bounding_box().contains_point(last_click_pos): + if sprite.get_bounding_box().contains_point(event.world_position): sprite.click(event) for event in pygame_events: if event.type == pygame.QUIT: pygame.quit() quit() - # elif event.type == pygame.MOUSEBUTTONDOWN: - # generated_level.load_level(parsed_levels_manager.levels[0]) + if key_manager.is_keymap_down(KeyManager.KEY_ESCAPE): + if show_menu: + show_menu = False + destroy_menu() + else: + show_menu = True + for level_text_label in level_menu.level_text_labels: + sprite_manager.add_ui_element(DrawLayers.UI, level_text_label) screen.fill((0, 0, 0)) - # level_menu.draw(screen, screen_transform) sprite_manager.tick(TickData(apply_frame_rate(1), pygame_events, key_manager, click_events, screen_transform)) sprite_manager.draw(screen, screen_transform) - # print bounding boxes of menu labels for testing - # for text_label in level_menu.level_text_labels: - # bound_box = text_label.get_bounding_box() - # # transformed_pos = transform_world_to_screen((bound_box.x, bound_box.y), screen_transform) - # # rect = pygame.Rect(transformed_pos[0], transformed_pos[1], bound_box.width, bound_box.height) - # rect = pygame.Rect(bound_box.x, bound_box.y, bound_box.width, bound_box.height) - # # if text_label.text == 'level-01': - # pygame.draw.rect(screen, (255, 0, 0), rect, 1) - # - # rect = pygame.Rect(last_click_pos[0], last_click_pos[1], 5, 5) - # pygame.draw.rect(screen, (0, 255, 0), rect, 1) - pygame.display.update() elif what_to_run == 'level': @@ -124,7 +122,7 @@ elif what_to_run == 'level': parsed_levels_manager.load_from_config('data/levels/levels.json') generated_level = LoadedLevel(sprite_manager, spritesheet_manager) - generated_level.load_level(parsed_levels_manager.levels[2]) + generated_level.load_level(parsed_levels_manager.levels[1]) ghost_character = PlayerSprite(spritesheet_manager.get_sheet("ghost_character")) ghost_character.position_scale = PositionScale((90, 50), (1, 1)) sprite_manager.add_ui_element(DrawLayers.OBJECTS, ghost_character) diff --git a/project/ui_elements/LevelMenu.py b/project/ui_elements/LevelMenu.py index 92cdb9c..17c9196 100644 --- a/project/ui_elements/LevelMenu.py +++ b/project/ui_elements/LevelMenu.py @@ -1,5 +1,3 @@ -import copy - import pygame from level.Level import Level