我正在制作基于文本的冒险游戏,但动作命令不起作用

Posted

技术标签:

【中文标题】我正在制作基于文本的冒险游戏,但动作命令不起作用【英文标题】:I am making a text based adventure game, and the action commands are not working 【发布时间】:2021-10-26 03:00:20 【问题描述】:

强调的文字这是目前为止的代码:

# 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] 作为目标。

但是,当我尝试使用程序中的命令时,我收到以下错误:

Traceback(最近一次调用最后一次): 文件“C:\Users\josep\OneDrive\Desktop\IT 140\ADVENTURE GAME\main.py”,第 125 行,在 如果房间 [current_room] 中的 player_action[1]: TypeError: unhashable type: 'dict'

我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

试试

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

current_room 是一个字典...您不能将字典用作另一个字典的键(当您创建它时确实不是...)

current_room 的键之一是 "name",这实际上是您房间字典的键

也改变你的其他实例

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

new_room_name = current_room[player_action[1]]
new_room = rooms[new_room_name]
current_room = new_room

【讨论】:

这是该块的代码:elif player_action[0] == 'go': if player_action[1] in rooms[current_room['name']]: current_room = rooms[current_room] [player_action[1]] else: print("You cannot go that way") 不幸的是,我仍然遇到同样的错误。 你还有rooms[current_room]而不是rooms[current_room["name"]] 我错过了。 “go”功能现在可以工作了。我现在遇到了“get”命令的问题。 rooms[current_room] 是个问题...在任何地方您都需要获取 room_name 并以 rooms[room_name] 的身份访问它

以上是关于我正在制作基于文本的冒险游戏,但动作命令不起作用的主要内容,如果未能解决你的问题,请参考以下文章

我的基于文本的游戏中的整数检查系统不起作用

我做了一个功能,在我的井字游戏中的每一个动作之后都会改变玩家,但似乎不起作用

在 C++ 中制作基于文本的 RPG 游戏/模板时出现问题 [关闭]

或管道不工作?

A.N.N.E for Mac 下载 – 好玩的飞行动作冒险游戏

重叠在 Phaser 中使用 JavaScript 不起作用