def _quest_on_hover(game_state: GameState, ui_view: GameUiView, boss_npc_type: NpcType, quest_item_id: ItemId): bosses = [npc for npc in game_state.game_world.non_player_characters if npc.npc_type == boss_npc_type] if bosses: position = bosses[0].world_entity.get_center_position() world_area = game_state.game_world.entire_world_area position_ratio = ((position[0] - world_area.x) / world_area.w, (position[1] - world_area.y) / world_area.h) ui_view.set_minimap_highlight(position_ratio) ui_view.set_inventory_highlight(quest_item_id)
def __init__(self, map_file_name: Optional[str], chosen_hero_id: Optional[str], hero_start_level: Optional[int], start_money: Optional[int], save_file_name: Optional[str], fullscreen: bool): cmd_flags = CommandlineFlags(map_file_name, chosen_hero_id, hero_start_level, start_money, save_file_name) pygame.init() print("Available display modes: " + str(pygame.display.list_modes())) self.fullscreen = fullscreen self.pygame_screen = self.setup_screen() images_by_sprite = load_images_by_sprite(ENTITY_SPRITE_INITIALIZERS) images_by_ui_sprite = load_images_by_ui_sprite(UI_ICON_SPRITE_PATHS, UI_ICON_SIZE) big_images_by_ui_sprite = load_images_by_ui_sprite( UI_ICON_SPRITE_PATHS, UI_ICON_BIG_SIZE) self.images_by_portrait_sprite = load_images_by_portrait_sprite( PORTRAIT_ICON_SPRITE_PATHS, PORTRAIT_ICON_SIZE) self.world_view = GameWorldView(self.pygame_screen, CAMERA_SIZE, SCREEN_SIZE, images_by_sprite) self.ui_view = GameUiView(self.pygame_screen, CAMERA_SIZE, SCREEN_SIZE, images_by_ui_sprite, big_images_by_ui_sprite, self.images_by_portrait_sprite, ABILITY_KEY_LABELS) self.ui_view.on_fullscreen_changed(self.fullscreen) self.save_file_handler = SaveFileHandler() init_sound_player() self.clock = pygame.time.Clock() self.scene_factory = SceneFactory(self.pygame_screen, self.images_by_portrait_sprite, self.save_file_handler, self.ui_view, self.world_view, self.toggle_fullscreen, CAMERA_SIZE) self.scene: AbstractScene = StartingProgramScene( self.scene_factory, cmd_flags, self.save_file_handler)
def register_game_state_observers(game_state: GameState, ui_view: GameUiView, include_player_state: bool): game_state.game_world.player_movement_speed_was_updated.register_observer(ui_view.on_player_movement_speed_updated) game_state.game_world.notify_movement_speed_observers() # Must notify the initial state game_state.game_world.player_entity.movement_changed = Observable() game_state.game_world.player_entity.movement_changed.register_observer(play_or_stop_footstep_sounds) game_state.game_world.player_entity.position_changed = Observable() game_state.game_world.player_entity.position_changed.register_observer(ui_view.on_player_position_updated) game_state.game_world.player_entity.position_changed.register_observer( lambda _: ui_view.on_walls_seen([w.get_position() for w in game_state.get_walls_in_sight_of_player()])) game_state.game_world.player_entity.notify_position_observers() # Must notify the initial state if include_player_state: _register_player_state_observers(game_state.player_state, ui_view)
class Main: def __init__(self, map_file_name: Optional[str], chosen_hero_id: Optional[str], hero_start_level: Optional[int], start_money: Optional[int], save_file_name: Optional[str], fullscreen: bool): cmd_flags = CommandlineFlags(map_file_name, chosen_hero_id, hero_start_level, start_money, save_file_name) pygame.init() print("Available display modes: " + str(pygame.display.list_modes())) self.fullscreen = fullscreen self.pygame_screen = self.setup_screen() images_by_sprite = load_images_by_sprite(ENTITY_SPRITE_INITIALIZERS) images_by_ui_sprite = load_images_by_ui_sprite(UI_ICON_SPRITE_PATHS, UI_ICON_SIZE) big_images_by_ui_sprite = load_images_by_ui_sprite( UI_ICON_SPRITE_PATHS, UI_ICON_BIG_SIZE) self.images_by_portrait_sprite = load_images_by_portrait_sprite( PORTRAIT_ICON_SPRITE_PATHS, PORTRAIT_ICON_SIZE) self.world_view = GameWorldView(self.pygame_screen, CAMERA_SIZE, SCREEN_SIZE, images_by_sprite) self.ui_view = GameUiView(self.pygame_screen, CAMERA_SIZE, SCREEN_SIZE, images_by_ui_sprite, big_images_by_ui_sprite, self.images_by_portrait_sprite, ABILITY_KEY_LABELS) self.ui_view.on_fullscreen_changed(self.fullscreen) self.save_file_handler = SaveFileHandler() init_sound_player() self.clock = pygame.time.Clock() self.scene_factory = SceneFactory(self.pygame_screen, self.images_by_portrait_sprite, self.save_file_handler, self.ui_view, self.world_view, self.toggle_fullscreen, CAMERA_SIZE) self.scene: AbstractScene = StartingProgramScene( self.scene_factory, cmd_flags, self.save_file_handler) def main_loop(self): try: self._main_loop() except Exception as e: print("Game crashed with an unexpected error! %s" % e) if hasattr(self.scene, 'game_state'): game_state: GameState = getattr(self.scene, 'game_state', None) print("Saving character to file as backup...") self.save_file_handler.save_to_file(game_state.player_state, None, Millis(0)) else: print("Failed to save character to file as backup!") raise e def _main_loop(self): while True: self.clock.tick() time_passed = Millis(self.clock.get_time()) fps_string = str(int(self.clock.get_fps())) self.ui_view.update_fps_string(fps_string) input_events: List[Any] = pygame.event.get() for event in input_events: if event.type == pygame.QUIT: self.quit_game() elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE and self.fullscreen: self.toggle_fullscreen() self.ui_view.on_fullscreen_changed(self.fullscreen) transition: Optional[ SceneTransition] = self.scene.handle_user_input(input_events) if transition: self.change_scene(transition) continue transition: Optional[SceneTransition] = self.scene.run_one_frame( time_passed) if transition: self.change_scene(transition) continue self.scene.render() pygame.display.update() def toggle_fullscreen(self): self.fullscreen = not self.fullscreen self.pygame_screen = self.setup_screen() def setup_screen(self): flags = pygame.DOUBLEBUF if self.fullscreen: flags = flags | pygame.FULLSCREEN | pygame.HWSURFACE return pygame.display.set_mode(SCREEN_SIZE, flags) @staticmethod def quit_game(): pygame.quit() sys.exit() def change_scene(self, scene_transition: SceneTransition): self.scene = scene_transition.scene self.scene.on_enter()
def _quest_on_blur(ui_view: GameUiView): ui_view.remove_minimap_highlight() ui_view.remove_inventory_highlight()
def on_blur(self, game_state: GameState, ui_view: GameUiView): ui_view.remove_inventory_highlight()
def on_hover(self, game_state: GameState, ui_view: GameUiView): ui_view.set_inventory_highlight(self.item_id)