首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在制作一个基于文本的冒险游戏,操作命令不起作用。

我正在制作一个基于文本的冒险游戏,操作命令不起作用。
EN

Stack Overflow用户
提问于 2021-08-26 02:51:32
回答 1查看 116关注 0票数 0

_emphasized text_Here是目前为止的代码:

代码语言:javascript
复制
# This displays the game's basic instructions.
def show_instructions():
    print("The Haunted Manor")
    print("Collect Pawpaw's Lucky Charms and Break the Ancient Curse")
    print("Avoid the cursed items haunting the Manor")
    print("Move Commands: 'go north', 'go south', 'go east', 'go west'")
    print("Get Items: get 'item name'")
    print("Type 'exit' if you'd like to quit\n")

# This defines the player's status, and displays it.
def player_status():
    print(current_room['text'])
    print()
    print("You are in: {}".format(current_room['name']))
    print("Items in your possession: {}".format(current_items))
    print("You see {}".format(current_room['item']))

# The following is a dictionary of rooms, their connections, items in them, and other information
rooms = {
    'Foyer': {
        'name': 'Foyer',
        'west': 'Dining Room',
        'south': 'Great Hall',
        'east': 'Billiards Room',
        'item': 'nothing',
        'text': 'Welcome to the Foyer'
    },
    # Start Room
    'Dining Room': {
        'name': 'Dining Room',
        'east': 'Foyer',
        'south': 'Kitchen',
        'item': 'Haunted Spork',
        'text': 'You have entered the Dining Room'
    },
    'Billiards Room': {
        'name': 'Billiards Room',
        'west': 'Foyer',
        'item': 'Lucky Rabbit Foot',
        'text': 'You are in the Billiards Room'
    },
    'Kitchen': {
        'name': 'Kitchen',
        'north': 'Dining Room',
        'east': 'Great Hall',
        'south': 'Laundry',
        'item': 'Silver Spoon',
        'text': 'Welcome to the Kitchen'
    },
    'Great Hall': {
        'name': 'Great Hall',
        'north': 'Foyer',
        'west': 'Kitchen',
        'south': 'Master Suite',
        'east': 'Observatory',
        'item': 'Monkey Paw',
        'text': 'The Great Hall welcomes you'
    },
    'Observatory': {
        'name': 'Observatory',
        'west': 'Great Hall',
        'south': 'Armory',
        'item': 'Ancient Curse',
        'text': 'You have entered the Observatory',
    },
    # Boss Room
    'Laundry': {
        'name': 'Laundry',
        'north': 'Kitchen',
        'east': 'Master Suite',
        'item': 'Lucky Hat',
        'text': 'You have entered the Laundry'
    },
    'Master Suite': {
        'name': 'Master Suite',
        'north': 'Great Hall',
        'west': 'Laundry',
        'south': 'Greenhouse',
        'east': 'Armory',
        'item': 'Horseshoe Belt Buckle',
        'text': 'This is the Master Suite'
    },
    'Armory': {
        'name': 'Armory',
        'west': 'Master Suite',
        'north': 'Observatory',
        'item': 'Shooting Star',
        'text': 'Welcome to the Armory'
    },
    'Greenhouse': {
        'name': 'Greenhouse',
        'north': 'Master Suite',
        'item': 'Four-Leaf Clover',
        'text': 'Welcome to the Greenhouse'
    },
}

# The following sets the player's initial location and items.
current_room = rooms['Foyer']
current_items = None

player_action = ''

show_instructions()

while True:
    player_status()
    player_action = input("What do you want to do?\n")
    player_action = player_action.split()

    if player_action[0] == 'exit':
        print("Come Back Soon")
        break

    elif len(player_action) != 2:
        print("That doesn't make any sense\n")

    elif player_action[0] == 'go':
        if player_action[1] in rooms[current_room]:
            current_room = rooms[current_room][player_action[1]]
        else:
            print("You cannot go that way")

    elif player_action[0] == 'get':
        if player_action[1] in rooms[current_room]:
            current_items += [player_action[1]]
            print("You got: ", player_action[1])
            del rooms[current_room]['item']
        else:
            print("What exactly were you looking for?")

我正在接受用户输入,将其分解为一个列表,使用第一个条目作为命令player_action[0] (第124和第130行),并试图使用第二个条目player_action[1]作为目标。

但是,当我试图在程序中使用这些命令时,我会得到以下错误:

跟踪(最近一次调用):文件"C:\Users\josep\OneDrive\Desktop\IT 140\ADVENTURE GAME\main.py",第125行,在if player_action1 in roomscurrent_room: TypeError: unhashable type:'dict‘中

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 02:54:13

试一试

if player_action[1] in rooms[current_room['name']]

current_room是一本字典..。你不能用一本字典作为另一本字典的钥匙(当你创建它的时候,你确实不是.)

current_room的一个键是"name",它实际上是您房间字典中的密钥。

将另一个实例更改为

current_room = rooms[current_room][player_action[1]]

代码语言:javascript
复制
new_room_name = current_room[player_action[1]]
new_room = rooms[new_room_name]
current_room = new_room
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68931924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档