def run(self): """ Main game loop which takes in process player input updates screen """ while self.playing: config.CLOCK.tick(FPS) handle_events() update_game() draw.draw_mouse() self.check_if_player_lost() pygame.display.flip()
def move_char_auto(path, ignore=False): """ Moves current_group (player) according to path and draws character with slight delay to show walking animation Uses difference in current/old coord and new/destination coord to find which direction to move Args: path (list): path to take ignore (boolean): if true, ignore monsters showing up in FOV and continue moving, else stop and prevent movement """ old_coord = (config.PLAYER.x, config.PLAYER.y) if len(path) == 0: if not ignore: # If enemy in FOV stop auto moving # If wall hack on disregard if _check_if_enemy_in_fov(config.GAME_DATA.creature_data["enemy"]): return update_creatures(config.GAME_DATA.creature_data, 0, 0) else: for coord in path: # If key pressed stop auto moving events = pygame.event.get() for event in events: if event.type == pygame.KEYDOWN: return if not ignore: # If enemy in FOV stop auto moving # If wall hack on disregard if _check_if_enemy_in_fov( config.GAME_DATA.creature_data["enemy"]): return # Move to next coord in path dest_x = coord[0] - old_coord[0] dest_y = coord[1] - old_coord[1] update_creatures(config.GAME_DATA.creature_data, dest_x, dest_y) old_coord = coord update_game() draw.draw_mouse() config.CLOCK.tick(20) pygame.display.flip()
def magic_select_menu(): """ Shows spells that are castable and allows for choosing which spell to cast """ select_menu = _draw_castable_spells() select_magic = True while select_magic: game.update_game() draw.draw_mouse() select_menu.draw_buttons(config.SURFACE_MAIN) mouse_x, mouse_y = pygame.mouse.get_pos() hovered_button = select_menu.check_if_button_hovered(mouse_x, mouse_y) if hovered_button and hovered_button.mouse_over_fn: hovered_button.mouse_over_fn() events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: select_magic = False break elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: button = config.BUTTON_PANEL.check_if_button_pressed( mouse_x, mouse_y) if button: select_magic = False break magic_button = select_menu.check_if_button_pressed( mouse_x, mouse_y) if magic_button: magic_targetting_menu(magic_button.left_click_fn) config.CLOCK.tick(FPS) pygame.display.flip()
def magic_targetting_menu(spell_to_cast): """ Selects target for spell and cast spell_to_cast and updates display Args: spell_to_cast (fn pointer): """ magic_cast = True while magic_cast: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: magic_cast = False break elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: mouse_x, mouse_y = pygame.mouse.get_pos() button = config.BUTTON_PANEL.check_if_button_pressed( mouse_x, mouse_y) if button: button.left_click_fn() break game.cast_magic(spell_to_cast, line) magic_cast = False break config.CLOCK.tick(FPS) game.update_game() draw.draw_mouse() m_x, m_y = config.CAMERA.get_mouse_coord() line = magic.line(config.PLAYER.position, (m_x, m_y), config.MAP_INFO.tile_array, config.FOV) draw.draw_magic_path(line) pygame.display.flip()
def stat_menu(): """ Draws stat menu """ menu_width, menu_height = config.CAMERA.camera_width / 3, config.CAMERA.camera_height / 3 stat_surface = pygame.Surface((menu_width, menu_height)) # Could move this to _draw_stat if you want animated character icon in stat menu character_icon = pygame.transform.scale(config.PLAYER.image, (SPRITE_SIZE * 2, SPRITE_SIZE * 2)) stat_open = True while stat_open: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: stat_open = False break elif event.type == pygame.MOUSEBUTTONDOWN: stat_open = False break config.CLOCK.tick(FPS) game.update_game() _draw_stat(config.PLAYER, stat_surface, character_icon) # Centers stat_menu stat_rect = stat_surface.get_rect() stat_rect.center = (config.CAMERA.camera_width // 2, config.CAMERA.camera_height // 2) config.SURFACE_MAIN.blit(stat_surface, stat_rect) draw.draw_mouse() pygame.display.flip()
def inventory_menu(): """ create screens for inventory + equipment menus """ menu_closed = False menu_width, menu_height = config.CAMERA.camera_width / 2, config.CAMERA.camera_height menu_surface = pygame.Surface((menu_width, menu_height - SPRITE_SIZE)) menu_surface.fill(INVENTORY_BEIGE) while not menu_closed: events_list = pygame.event.get() game.update_game() config.SURFACE_MAIN.blit(menu_surface, (menu_width, 0)) equipment = _load_equipment_screen() equipment.draw_buttons(config.SURFACE_MAIN) inventory = _load_inventory_screen() inventory.draw_buttons(config.SURFACE_MAIN) draw.draw_mouse() mouse_x, mouse_y = pygame.mouse.get_pos() hovered_inventory_button = inventory.check_if_button_hovered( mouse_x, mouse_y) if hovered_inventory_button and hovered_inventory_button.mouse_over_fn: hovered_inventory_button.mouse_over_fn() hovered_equipment_button = equipment.check_if_button_pressed( mouse_x, mouse_y) if hovered_equipment_button and hovered_equipment_button.mouse_over_fn: hovered_equipment_button.mouse_over_fn() for event in events_list: if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: inventory_button = config.BUTTON_PANEL.check_if_specific_button_pressed( 'inventory', mouse_x, mouse_y) if inventory_button: menu_closed = True break map_button = config.BUTTON_PANEL.check_if_specific_button_pressed( 'map', mouse_x, mouse_y) if map_button: map_menu() break stat_button = config.BUTTON_PANEL.check_if_specific_button_pressed( 'stats', mouse_x, mouse_y) if stat_button: stat_menu() break item_slot = inventory.check_if_button_pressed( mouse_x, mouse_y) if item_slot and item_slot.left_click_fn: item_slot.left_click_fn() equip_slot = equipment.check_if_button_pressed( mouse_x, mouse_y) if equip_slot and equip_slot.left_click_fn: equip_slot.left_click_fn() elif event.button == 3: clicked_button = inventory.check_if_button_pressed( mouse_x, mouse_y) if clicked_button and clicked_button.right_click_fn: clicked_button.right_click_fn() game.update_creatures(config.GAME_DATA.creature_data, 0, 0) if event.type == pygame.KEYDOWN: if event.key == pygame.K_TAB: game.toggle_minimap() if event.key == pygame.K_i or event.key == pygame.K_ESCAPE: menu_closed = True break config.CLOCK.tick(FPS) pygame.display.update()