我一直在尝试编写代码,在基于文本的游戏中将项目添加到我的库存中,但到目前为止我还无法弄清楚。这是我当前的代码:
rooms = {
'Entrance': {'west': 'Catacombs A', 'north': 'Main Hall'},
'Catacombs A': {'east': 'Entrance', 'item': 'Lesser Artifact'},
'Main Hall': {'north': 'Great Hall', 'east': 'Catacombs B', 'south': 'Entrance', 'west': 'Necron Tomb',
'item': 'Dog Tags'},
'Necron Tomb': {'east': 'Main Hall', 'item': 'Supplies'},
'Catacombs B': {'west': 'Main Hall', 'north': 'Storage Room', 'item': 'Lesser Artifact'},
'Storage Room': {'south': 'Catacombs B', 'item': 'Supplies'},
'Great Hall': {'east': 'Repair Hub', 'south': 'Main Hall', 'item': 'Dog Tags'},
'Repair Hub': {'west': 'Great Hall', 'item': 'Necron Lord'} # Villain
}
current_room = 'Entrance'
inventory = []
print('You are a member of a squad of Ultramarines that has been tasked with retrieving 6 items from a '
'Necron facility on the orbiting planet before reaching the Necron Lord. You and your brothers breach the entrance, which way do you go first?')
def move_rooms(current_room, directions):
current_room = rooms[current_room]
new_room = current_room[directions]
return new_room
while True:
print('You are in the', current_room)
directions = input('Enter a direction: north, east, south, west, get item, or exit.')
if directions in rooms[current_room]:
current_room = move_rooms(current_room, directions)
if directions == 'exit':
print('You have failed the Emperor and your brothers...')
break
elif directions not in rooms[current_room]:
print('You cannot go that way.')
我尝试创建一个定义函数来通过执行以下操作来获取项目
def get_item(item, inventory):
if item in current_room:
inventory.append(item[current_room])
return inventory
Python 给了我一个错误,并要求定义项目,即使它在我的房间字典中。我对 python 很陌生,所以我很挣扎,任何帮助将不胜感激!
检查您是否为 get_item 函数提供了正确的输入,以及变量 item 是否以列表形式给出。我认为 items 被用作“rooms”字典中的字符串,但它在定义函数中被索引为列表。 (问题以斜体显示)
def get_item(物品, 库存): 如果当前房间中有物品: inventory.append(item[current_room]) 退货库存