Beispiel #1
0
 def __init__(self, save_file_handler: SaveFileHandler,
              scene_factory: AbstractSceneFactory, flags: InitFlags,
              view: MainMenuView):
     self._selected_option_index = 0
     self._first_shown_option_index = 0
     self.scene_factory = scene_factory
     self.flags = flags
     self._files = save_file_handler.list_save_files()
     self._files.sort(key=lambda file: int(file.split(".")[0]))
     self._saved_characters: List[SavedPlayerState] = [
         save_file_handler.load_player_state_from_json_file(file)
         for file in self._files
     ]
     self._view = view
Beispiel #2
0
    def __init__(self, map_file_name: Optional[str],
                 chosen_hero_id: Optional[str],
                 hero_start_level: Optional[int], start_money: Optional[int]):

        cmd_flags = CommandlineFlags(map_file_name, chosen_hero_id,
                                     hero_start_level, start_money)

        pygame.init()

        print("Available display modes: " + str(pygame.display.list_modes()))

        self.fullscreen = True
        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.save_file_handler = SaveFileHandler()
        init_sound_player()
        self.clock = pygame.time.Clock()

        self.scene: AbstractScene = StartingProgramScene(
            self.main_menu_scene, self.creating_world_scene,
            self.picking_hero_scene, cmd_flags, self.save_file_handler)
Beispiel #3
0
 def __init__(self, save_file_handler: SaveFileHandler,
              picking_hero_scene: Callable[[InitFlags], AbstractScene],
              creating_world_scene: Callable[[InitFlags], AbstractScene],
              flags: InitFlags, view: MainMenuView):
     self._selected_option_index = 0
     self._first_shown_option_index = 0
     self.picking_hero_scene = picking_hero_scene
     self.creating_world_scene = creating_world_scene
     self.flags = flags
     self._files = save_file_handler.list_save_files()
     self._files.sort()
     self._saved_characters: List[SavedPlayerState] = [
         save_file_handler.load_player_state_from_json_file(file)
         for file in self._files
     ]
     self._view = view
Beispiel #4
0
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()