我正在为班级创建一个基于文本的冒险游戏,但我的库存系统无法正常工作

问题描述 投票:0回答:1

对于上周我班上的作业,我们必须创建一个简单的、可以移动房间的游戏。我已将该代码调整到我的游戏代码中,即使在我的字典中添加了“Item”,它似乎也能正常工作。一旦我尝试添加到库存并将库存列表引用到字典键,我就会收到“不可散列”错误。我查了一下,但清除错误的唯一方法是使用元组,但元组是不可变的,所以我无法添加到库存中,至少据我了解。非常感谢任何指导或建议!

def player_instructions():
    # main menu and game commands
    print('Move Commands: go North, go South, go West, go East')
    print("Add to inventory: get 'item name'.")
    print('-' * 50)


print('A Paladin Adventure Game.')  # game opening greeting
print('Collect 7 items to win the game,')
print('or be destroyed by the Vampire Lord.')
print('-' * 50)

# dictionary for rooms and movement between
castle_rooms = {'Entry Hall': {'East': 'Great Hall', 'South': 'Solar', 'Item': ''},
                'Great Hall': {'West': 'Entry Hall', 'South': 'Boudoir',
                               'North': 'Kitchen', 'East': 'Cabinet', 'Item': 'Silver Mail'},
                'Solar': {'North': 'Entry Hall', 'East': 'Boudoir', 'Item': 'Stake'},
                'Boudoir': {'North': 'Great Hall', 'West': 'Solar', 'Item': 'Holy Water'},
                'Kitchen': {'East': 'Larder', 'South': 'Great Hall', 'Item': 'Mallet'},
                'Larder': {'West': 'Kitchen', 'Item': 'Potion of Cleansing'},
                'Cabinet': {'West': 'Great Hall', 'North': 'Garderobe', 'South': 'Bedchamber',
                            'Item': 'Blessed Sword'},
                'Garderobe': {'South': 'Cabinet', 'Item': 'Torch'}
                }

current_room = 'Entry Hall'  # define starting room
inventory = []  # define string for inventory


def player_commands(command, direction):  # function for moving rooms
    global current_room  # declare current room as global
    global inventory  # declare inventory as global
    if command == 'go':
        if direction in castle_rooms[current_room]:  # establishing valid direction
            current_room = castle_rooms[current_room][direction]
        elif direction not in castle_rooms[current_room]:  # error message for invalid direction
            print("You can't go that way")
            current_room = current_room

    elif command == 'get':
        if direction in current_room:
            inventory.append(direction)
        current_room = current_room
    else:
        print('Invalid command')  # error message for invalid command
        current_room = current_room
    return current_room, inventory


while current_room != 'Bedchamber':  # while loop for gameplay
    player_instructions()
    print('You are in the {}.'.format(current_room))

    if castle_rooms[current_room]['Item'] not in inventory:
        print('You see {} in the room.'.format(castle_rooms[current_room]['Item']))
    else:
        pass
    user_input = input('What will you do next?').split()
    command = user_input[0]
    direction = user_input[1] if len(user_input) > 1 else ''
    current_room = player_commands(command, direction)

这是到目前为止的完整代码,一切正常,直到我尝试添加单独的玩家库存。我还没有添加游戏结束代码,所以我意识到我有一个无限循环,但我试图在添加结局之前使库存工作,因为库存必须包含所有要赢的物品。

python pycharm
1个回答
0
投票

当您“获得”一个物品时,您希望将其添加到您的库存中并将其从房间中删除。

尝试类似:

    elif command == 'get':
        current_item = castle_rooms[current_room_id]['Item']
        if current_item not in inventory:
            inventory.append(current_item)
            castle_rooms[current_room_id]['Item'] = None
            return current_room_id
© www.soinside.com 2019 - 2024. All rights reserved.