def input_player_choice(): m_logger.info('Where do you want to move: ') player_choice = input() m_logger.debug(f'dungeon_game : player inputed {player_choice}') player_choice.casefold() if player_choice == 'save': m_logger.debug('dungeon_game : player want to save game') save_game() else: DIRECTIONS = ['up', 'down', 'left', 'right'] while not player_choice in DIRECTIONS: m_logger.debug('dungeon_game : player typed something wrong') m_logger.info( "I'm sorry, I don't understand where do you want to go.\nTry once more: " ) player_choice = input() m_logger.debug( f'dungeon_game : player inputed {player_choice} as new direction' ) player_choice.casefold() m_logger.debug( f'dungeon_game : success! player inputed right direction') return player_choice
def perform_next_step(direction): global player_position old_position = list(player_position) m_logger.debug(f'game_step : saved previous position') direction_to_move = DIRECTION_TO_MOVE[direction] m_logger.debug(f'game_step : got direction to move by text value of direction') player_position[0] += direction_to_move[0] player_position[1] += direction_to_move[1] m_logger.debug(f'game_step : updated player position') if player_position[0] < 0 or player_position[1] < 0 or player_position[0] >= mg.mapsize or player_position[1] >= mg.mapsize: m_logger.info("You can't move there! You reached border!\n") m_logger.debug(f'game_step : player tried to move out from map') player_position[0] -= direction_to_move[0] player_position[1] -= direction_to_move[1] m_logger.debug(f'game_step : player position reverted') else: m_logger.info(f'Player position changed from ({old_position[1]},{old_position[0]}) to ({player_position[1]},{player_position[0]})') row, col = player_position global is_player_lost if mg.game_map[row][col] == -1: m_logger.debug(f'game_step : player lost') is_player_lost = True global is_player_won if mg.game_map[row][col] == 1: m_logger.debug(f'game_step : player won') is_player_won = True
def notify_player_about_treasures(): neighbour_values = get_neighbour_values() m_logger.debug(f'game_step : got list of neighbour cells') neighbour_values.sort(reverse = True) m_logger.debug(f'game_step : sorted (reverse = True) list of neighbour cells') has_treasures = neighbour_values[0] == 1 if has_treasures: m_logger.info('You are close to win! Treasure is somewhere near you')
def notify_player_about_traps(): neighbour_values = get_neighbour_values() m_logger.debug(f'game_step : got list of neighbour cells') neighbour_values.sort() m_logger.debug(f'game_step : sorted list of neighbour cells') has_traps = neighbour_values[0] == -1 if has_traps: m_logger.info('Be careful! Trap is somewhere near you!')
def set_map_size(): global mapsize m_logger.info("Enter size of game map: ") mapsize = input() while (not mapsize.isdigit()) or int(mapsize) < 5: m_logger.info("Something wrong entered\nTry again: ") mapsize = input() mapsize = int(mapsize)
def print_game_map(): TRAP = '#' TREASURE = '*' PLAYER = '!' EMPTY_CELL = '.' m_logger.debug(f'game_step : game map formatting to print started') game_map = mg.game_map game_map = [[TRAP if cell == -1 else cell for cell in row] for row in game_map ] m_logger.debug(f'game_step : game map traps formatted') game_map = [[TREASURE if cell == 1 else cell for cell in row] for row in game_map ] m_logger.debug(f'game_step : game map treasures formatted') game_map = [[EMPTY_CELL if cell == 0 else cell for cell in row] for row in game_map ] m_logger.debug(f'game_step : game map empty cells formatted') game_map[player_position[0]][player_position[1]] = PLAYER m_logger.debug(f'game_step : game map player position formatted') m_logger.debug(f'game_step : game map formatted') for r in game_map: print(*r) m_logger.debug(f'game_step : game map printed') m_logger.info(f'Player last position: {player_position}')
gs.player_position = loaded[1] m_logger.debug(f'dungeon_game : set data to game_step module') file.close() m_logger.debug(f'dungeon_game : closed file') def new_game(): m_logger.debug(f'dungeon_game : starting game initialization') gs.init_game() m_logger.debug(f'dungeon_game : game initialized') m_logger.info( "Do you want to play a new game or load old one?\nEnter your choice [N/L]:" ) new_or_load = input() m_logger.debug(f'dungeon_game : player inputed {new_or_load}') new_or_load.casefold() while not new_or_load in ['n', 'l']: m_logger.debug(f'dungeon_game : player inputed something wrong') m_logger.info("Try again\nEnter your choice [N/L]: ") new_or_load = input() m_logger.debug(f'dungeon_game : player inputed {new_or_load}') new_or_load.casefold() m_logger.debug(f'dungeon_game : player inputed successfully')
def print_game_result(): if is_player_lost: m_logger.info('So sorry! You have lost this game :(') elif is_player_won: m_logger.info('Congratulations! You have won')