def update(self): super().update() player_command = self.__get_player_command() if player_command in self.__move_commands: old_x, old_y = self.__position sum_vectors = lambda a, b: (a[0] + b[0], a[1] + b[1]) self.__position = sum_vectors(self.__position, self.__direction_to_vector[player_command]) current_cell = self.__dungeon_map.move_player(self.__position) if current_cell is DungeonCell.TREASURE: logging.info('You got 1 treasure! Gratz! :)') self.__treasures_number += 1 if self.__treasures_number is DungeonGameConfig.NUMBER_OF_TREASURES_TO_WIN: logging.info('Congratulations! You won!') elif current_cell is DungeonCell.TRAP: logging.info('You lose 1 HP!') self.__hp -= 1 if self.__hp is 0: logging.info('You lost:(') else: logging.info('You found nothing. Keep exploring the map!:)') else: DungeonGameSaveLoad.save_game(self.__position, self.__dungeon_map.dungeon_map) logging.debug('Game if saved.') logging.info('Game is saved.')
def run_game(game_start_mode, map_size): ''' Function runs the Dungeon Game; :param game_start_mode: the way game should start; :param map_size: a size of map that will be used for game. Size should be >= 5. If game_start_mode is LOAD, the\ value of map_size is idnored and will be overwritten after load; :type map_size: int if game_start_mode is NEW_GAME, any other type otherwise; :type game_start_mode: StartMenu. ''' if game_start_mode is StartMenu.LOAD_GAME: logging.debug('Game starts with LOAD GAME mode. Loading from file...') player_position, dungeon_map = DungeonGameSaveLoad.load_game() map_size = len(dungeon_map) else: logging.debug('Game starts with NEW GAME mode. Generating map...') player_position, dungeon_map = generate_map(map_size) logging.debug(f'Map:{dungeon_map}\nPlayer position:{player_position}') should_run = True logging.debug('Game loop starts') game_loop_counter = 0 while should_run: logging.debug(f'Game loop iteration {game_loop_counter}') output_map(dungeon_map, hide_everything_except_player_map) cells_near_player = get_cells_near(player_position, dungeon_map) traps_nearby = [cell for cell in cells_near_player if cell is DungeonCell.TRAP] if traps_nearby: logging.info(f'Warning! There is {len(traps_nearby)} traps nearby!') treasures_nearby = [cell for cell in cells_near_player if cell is DungeonCell.TREASURE] if treasures_nearby: logging.info(f'Wow! There is {len(treasures_nearby)} treasures just near you! Good luck!') player_command = get_player_command(player_position, map_size) if player_command in move_commands: old_x, old_y = player_position player_position = sum_vectors(player_position, direction_to_vector[player_command]) new_x, new_y = player_position if dungeon_map[new_x][new_y] is DungeonCell.TREASURE: logging.info('You won! Gratz! :)') should_run = False elif dungeon_map[new_x][new_y] is DungeonCell.TRAP: logging.info('You lost:( GL next time!') should_run = False else: logging.info('You found nothing. Keep exploring the map!:)') dungeon_map[old_x][old_y] = DungeonCell.EMPTY dungeon_map[new_x][new_y] = DungeonCell.PLAYER else: DungeonGameSaveLoad.save_game(player_position, dungeon_map) logging.debug('Game if saved.') logging.info('Game is saved.') logging.debug('Game loop ended') output_map(dungeon_map, output_everything_map)
def run_game(self): ''' This method implements a single game frame. It will work depends on ''' if self.__current_game_state is GAME_MENU: try: if self.__game_start_mode is LOAD_GAME: player_position, dungeon_map = DungeonGameSaveLoad.load_game() self.__dungeon_map.init_from_load(player_position, dungeon_map) else: map_size = int(input('Map size (>= 5): ')) self.__dungeon_map.generate_new_map(map_size) except NoSavedDataFileError as error: logging.error(f'User have chosen to load the game but we can\'t find game save data on address:\ {error}.') logging.info('Sorry, but there is no game save data. Please, save the game firstly or find the\ save data file.') self.process_game_start() else: self.__character = Character('User', DungeonGameConfig.PLAYER_HP, self.__dungeon_map) self.__current_game_state = GAMEPLAY self.__update_list.add_children(self.__dungeon_map, self.__character) elif self.__current_game_state is GAMEPLAY: logging.info(f'Current character stats: hp - {self.__character.get_hp()}, num of treasures - \ {self.__character.get_treasures_number()}') self.__update_list.update()
def update(self): super().update() player_command = self.__get_player_command() if player_command in self.__move_commands: old_x, old_y = self.__position sum_vectors = lambda a, b: (a[0] + b[0], a[1] + b[1]) self.__position = sum_vectors( self.__position, self.__direction_to_vector[player_command]) current_cell = self.__dungeon_map.move_player(self.__position) if current_cell is DungeonCell.TREASURE: logging.info('You got 1 treasure! Gratz! :)') self.__treasures_number += 1 if self.__treasures_number is DungeonGameConfig.NUMBER_OF_TREASURES_TO_WIN: logging.info('Congratulations! You won!') elif current_cell is DungeonCell.TRAP: logging.info('You lose 1 HP!') self.__hp -= 1 if self.__hp is 0: logging.info('You lost:(') else: logging.info('You found nothing. Keep exploring the map!:)') else: try: DungeonGameSaveLoad.save_game(self.__position, self.__dungeon_map.dungeon_map) except CannotSaveGameError as error: logging.error(error) logging.info( f'Sorry, but we can\'t save the game due to some problems. It\'s likely that we have not\ a permission to write the data to a file. Try making the game folder writable for this program.' ) else: logging.info('Game is saved.')
def run_game(self): ''' This method implements a single game frame. It will work depends on ''' if self.__current_game_state is DungeonGameState.GAME_MENU: if self.__game_start_mode is GameStartMode.LOAD_GAME: player_position, dungeon_map = DungeonGameSaveLoad.load_game() self.__dungeon_map.init_from_load(player_position, dungeon_map) else: map_size = int(input('Map size (>= 5): ')) self.__dungeon_map.generate_new_map(map_size) self.__character = Character('User', DungeonGameConfig.PLAYER_HP, self.__dungeon_map) self.__current_game_state = DungeonGameState.GAMEPLAY self.__update_list.add_children(self.__dungeon_map, self.__character) elif self.__current_game_state is DungeonGameState.GAMEPLAY: logging.info( f'Current character stats: hp - {self.__character.get_hp()}, num of treasures - \ {self.__character.get_treasures_number()}') self.__update_list.update()