def update_player_position(self, player, x, y): """ Updates player according to it's position :param player: player object :param x: x axis coordinate :param y: y axis coordinate :rtype: None """ if not self._is_valid_position(x, y): raise PlayerMoveError item = self.__world[y][x] if item == World.treasure: player.treasures += 1 main_logger.info("You've found a treasure!\n") elif item == World.trap: player.health -= 1 main_logger.info("You've got in trap! You've lost one health =/\n") old = self.__player_position self.world[old[1]][old[0]] = World.explored self.__player_position = [x, y] self.world[y][x] = World.player
def print_map_hide_secrets(world_map, secrets, default): """ Prints world map without secrets :param world_map: map :param secrets: object to hide :param default: what will be printed instead of secrets :return: nothing :rtype: None """ main_logger.debug(f'started {world_map}, {secrets}, {default}') for i in world_map: lines = ['|'] for j in i: if j in secrets: lines.append(default) else: lines.append(j) lines.append('|') main_logger.info(''.join(lines)) main_logger.debug(f'ended')
def update_player_position(self, player): """ Updates player according to it's position :param player: player object :rtype: None """ x = player.x y = player.y item = self.__world[y][x] if item == World.treasure: player.treasures += 1 main_logger.info("You've found a treasure!\n") elif item == World.trap: player.health -= 1 main_logger.info("You've got in trap! You've lost one health =/\n") old = self.__player_position self.world[old[1]][old[0]] = World.explored self.__player_position = [x, y] self.world[y][x] = World.player
def print_map(world_map): """ Prints world map :param world_map: world :return: nothing :rtype: None """ for i in world_map: line = ''.join(['|', *i, '|']) main_logger.info(line)
def load_game(): """ Loads game from file :return: dict of in game objects :rtype: dict """ game = {} try: if path.isfile(SAVE): main_logger.info('Can\'t find saved game') else: game = load_pickle() except IOError: main_logger.info('Can\'t read data from file') except OSError: main_logger.info('Can\'t open file') except MemoryError: main_logger.info('Can\'t load game: not enough memory available') return game
def print_map(world_map): """ Prints world map :param world_map: world :return: nothing :rtype: None """ main_logger.debug(f'started {world_map}') for i in world_map: line = ''.join(['|', *i, '|']) main_logger.info(line) main_logger.debug(f'ended')
def warn_player(treasure=False, trap=True): """ Prints warning message :param treasure: message about treasure :param trap: message about trap :return: nothing :rtype: None """ if treasure: main_logger.info( 'Warning: there is a treasure within one square from you!') if trap: main_logger.info( 'Warning: there is a trap within one square from you!')
def main_menu(): """ Shows main menu and allows user to make choice :return: nothing :rtype: None """ new_game = 'New' load_game = 'Load' exit_game = 'Exit' prompt = 'Choose one of options above:' save_not_found = 'There is no saved games' invalid_input = 'Invalid input' menu_items = [new_game, load_game, exit_game, prompt] quit_game = False while not quit_game: main_logger.info('\n'.join(menu_items)) choice = input() if choice not in menu_items: main_logger.info(invalid_input) elif choice == new_game: save = {} play_game(save) elif choice == load_game: try: save = DungeonSaver.load_game() play_game(save) except FileNotFoundError: main_logger.info(save_not_found) except DataLoadError: main_logger.info('Saved game file has been corrupted') else: quit_game = True
def handle_player_move(world, player_position): """ Changes map accordingly to user input :param world: world map :param player_position: position of player :return: new player position :rtype: list """ main_logger.debug(f'started {world}, {player_position}') player_move_fail = 'Player can\'t make this move' user_input_fail = 'Invalid input' user_input_prompt = 'Choose where you want to move:\n\'u\' - up,\n\'d\' - down,\n\'l\' - left,\n\'r\' - right.\n\ Input move:' moves = {'u': (0, 0), 'd': (0, 1), 'l': (1, 0), 'r': (1, 1)} bounds = (0, len(world)) player_moves = (-1, 1) is_valid = False possible_moves = { k: [index, bounds[axis], player_moves[axis]] for (k, (index, axis)) in moves.items() } while not is_valid: main_logger.info(user_input_prompt) move = input() if move not in moves: main_logger.info(user_input_fail) continue is_valid = try_player_move(player_position, *possible_moves[move]) if not is_valid: main_logger.info(player_move_fail) main_logger.debug(f'ended {world}, {player_position}') return player_position
def handle_player_move(world, player): """ Handles player movement accordingly tp user input :param world: world map :param player: player :rtype: None """ player_move_fail = 'Player can\'t make this move' user_input_fail = 'Invalid input' user_input_prompt = 'Choose where you want to move:\n\'u\' - up,\n\'d\' - down,\n\'l\' - left,\n\'r\' - right.\n\ Input move:' moves = { 'u': player.up, 'd': player.down, 'l': player.left, 'r': player.right } is_valid = False while not is_valid: main_logger.info(user_input_prompt) move = input() if move not in moves: main_logger.info(user_input_fail) continue is_valid = moves[move]() if is_valid: world.update_player_position(player) else: main_logger.info(player_move_fail)
def play_game(game_save): """ Starts Dungeon game :param game_save: saved game state(if any) :return: nothing :rtype: None """ move_command = 'Move' save_command = 'Save' end_game_session = 'End' user_options = [move_command, save_command, end_game_session] user_prompt = '\n'.join(user_options) result = '' game_over = False game_world = World(10) size = game_world.size() game_world.fill_the_map(size * size / 10, World.trap) game_world.fill_the_map(size * size / 20, World.treasure) game_player = Player(game_world) hidden = [World.trap, World.treasure] if game_save != {}: game_world.load(game_save) game_player.load(game_save) while not game_over: world = game_world.print_except(hidden) main_logger.info( f'Player info:\n\thealth: {game_player.health} <3\n\ttreasures: {game_player.treasures}\n' ) main_logger.info(world) generate_warnings_if_required(game_world) main_logger.info(user_prompt) choice = input() if choice == move_command: handle_player_move(game_world, game_player) elif choice == save_command: objects = {} game_world.save(objects) game_player.save(objects) DungeonSaver.save_game(objects) elif choice == end_game_session: result = 'Game over' break else: main_logger.info('Invalid input') treasures = game_player.treasures health = game_player.health if treasures >= 3: game_over = True result = 'You captured 3 treasures! You won!' if health <= 0: game_over = True result = 'You have 0 health =/ You lose.' if game_over: main_logger.info(result) world = game_world.print() main_logger.info(world)
def play_game(game_save={}): """ Starts Dungeon game :param game_save: saved game state(if any) :return: nothing :rtype: None """ main_logger.debug(f'started') empty_space = '~' explored_space = '.' trap = 'x' treasure = '$' player = '*' move_command = 'Move' save_command = 'Save' end_game_session = 'End' user_options = [move_command, save_command, end_game_session] user_prompt = '\n'.join(user_options) result = '' game_over = False if game_save == {}: game_world = MapGenerator.generate(10, empty=empty_space, trap=trap, treasure=treasure) player_coordinates = setup_player_position(game_world, empty_space, player) else: game_world = game_save['map'] player_coordinates = game_save['player'] old_player_coordinates = player_coordinates hidden = [trap, treasure] while not game_over: game_world[old_player_coordinates[0]][ old_player_coordinates[1]] = explored_space game_world[player_coordinates[0]][player_coordinates[1]] = player old_player_coordinates = player_coordinates.copy() View.print_map_hide_secrets(game_world, hidden, empty_space) generate_warnings_if_required(game_world, player_coordinates, trap, treasure) main_logger.info(user_prompt) choice = input() if choice == move_command: handle_player_move(game_world, player_coordinates) elif choice == save_command: objects = {'map': game_world, 'player': player_coordinates} DungeonSaver.save_game(objects) elif choice == end_game_session: result = 'Game over' break else: main_logger.info('Invalid input') game_over, result = check_game_over(player_coordinates, game_world, trap, treasure) main_logger.info(result) View.print_map(game_world) main_logger.debug(f'ended {result}')