コード例 #1
0
def add_action(target: dict, keyword: str, commands: tuple):
    """ Add action keyword to the {item} or Player
    Args:
        target: Target {item} or Player;
        keyword: Action word.
        commands: Tuple with acceptable commands
    Return:
        updated {item} objects
    """
    # Check that passed target is an {item} or {player}:
    if not (general.is_item(target) or general.is_player(target)):
        print(
            f'{target["name"]} is not an appropriate object. Only Items or Player can have actions'
        )

    # Check that keyword is in command list:
    elif keyword not in commands:
        print(f'{keyword} is not an appropriate command')

    else:
        target['actions'].add(keyword)

        # Print info message:
        print(f'successfully added {keyword} action to {target["name"]}')

    return target
コード例 #2
0
def pickup_item(player: dict, item: dict, place: dict):
    """Move {item} from it current location and place it in Player's Inventory.
    Args:
        player: {player} object;
        item: {item} to be moved;
        place: {place} where {item} is located now
    Return:
        Updated {player}, {item} and {place} objects
    """
    # Check that {player} and {item} objects was passed and that:
    if not (general.is_player(player) and general.is_item(item)
            and general.is_place(place)):
        print(
            f'{player["name"]}, {item["name"]} or {place["name"]} is not an appropriate object.'
        )

    # Check if the {item} is located in {place}:
    elif item.get('location') == place:
        # Remove {item} from its current location:
        place['items'].remove(item)

        # Move the {item} to Inventory:
        item['location'] = player
        player['items'].append(item)

        # Update player's actions:
        player = update_actions(player)

    else:
        print(f"There is no {item['name']} in {place['name']}")

    return player, item, place
コード例 #3
0
def drop_item(player: dict, item: dict, place: dict):
    """Move {item} from Player's Inventory to a specified location.
    Args:
        player: {player} object;
        item: {item} to be moved;
        place: {place} where {item} should be placed
    Return:
        Updated {player}, {item} and {place} objects
    """
    # Check that {player} and {item} objects was passed and that:
    if not (general.is_player(player) and general.is_item(item)
            and general.is_place(place)):
        print(
            f'{player["name"]}, {item["name"]} or {place["name"]} is not an appropriate object.'
        )

    else:
        # Remove {item} from Inventory:
        player['items'].remove(item)

        # Move the {item} to Inventory:
        item['location'] = place
        place['items'].append(item)

        # Update player's actions:
        player = update_actions(player)

        print(f"You have dropped {item['name']} in {place['name']}")

    return player, item, place
コード例 #4
0
def remove_action(target: dict, keyword: str):
    """ Add action keyword to the {item} or Player
    Args:
        target: Target {item} or Player;
        keyword: Action word that needs to be removed.
    Return:
        updated {item} objects
    """
    # Check that passed target is an {item} or {player}:
    if not (general.is_item(target) or general.is_player(target)):
        print(
            f'{target["name"]} is not an appropriate object. Only Items or Player can have actions'
        )

    # Check that keyword is in item actions:
    elif keyword not in target['actions']:
        print(f'{target["name"]} has no {keyword} action')

    else:
        target['actions'].remove(keyword)

        # Print info message:
        print(f'successfully removed {keyword} action from {target["name"]}')