コード例 #1
0
def save_game(current_map):
    """saves game to "save.txt"

    Arguments:
        current_map {list} -- current game map state
    """

    Pickler(open("save.txt", 'wb')).dump(current_map)
    log.info("game saved!")
コード例 #2
0
def print_state(game_map):
    global player_position

    for val in player_actions_callbacks.values():
        player_neighbored_point = val(player_position)
        if game_map[player_neighbored_point[1]][
                player_neighbored_point[0]] == Map_generator.trap:
            log.info("trap is somewhere near")
        elif game_map[player_neighbored_point[1]][
                player_neighbored_point[0]] == Map_generator.treasure:
            log.info("Treasure is somewhere near")
コード例 #3
0
def loop(game_map):
    global player_position
    print_state(game_map)

    while player_input(game_map):
        print_state(game_map)

    cell_type = game_map[player_position[1]][player_position[0]]
    if cell_type == Map_generator.treasure:
        log.info("you found the treasure! Victory")
    else:
        log.info("Trap *traps*\n You *lost* xD")
コード例 #4
0
def load_game():
    """loads game map if file "save.txt" exists 

    Returns:
        list -- saved game map
    """

    if isfile("save.txt"):
        save = load(open("save.txt", 'rb'))
        log.info("game loaded")
        return save
    else:
        log.info("unable to load game")
コード例 #5
0
def player_input(game_map):
    log.debug("player_input function called")
    global player_position

    log.info(
        f"Your action :{[f'{key} - {val},' for key, val in player_actions.items()]}"
    )
    action = int(input())

    result = True
    log.debug(f"player_input: the action is {player_actions[action]}")

    if action in range(1, 5):
        player_position_new = player_actions_callbacks[action](player_position)
        cell_type = game_map[player_position_new[1]][player_position_new[0]]

        if cell_type == Map_generator.treasure or cell_type == Map_generator.trap:
            player_position = player_position_new
            result = False
            log.debug(f"player_input: player found a {cell_type}")
        elif cell_type == Map_generator.wall:
            log.info("you found the wall. Can't move further")
        else:
            game_map[player_position[1]][
                player_position[0]] = Map_generator.empty_cell
            player_position = player_position_new
            game_map[player_position[1]][
                player_position[0]] = Map_generator.player
            log.debug(
                f"_player_input: player new pos({player_position[0]}, {player_position[1]})"
            )
    elif action == 5:
        game_map = Save_load.load_game()
    elif action == 6:
        Save_load.save_game(game_map)
    else:
        print("invalid input \n")

    return result