【发布时间】:2021-04-19 17:35:21
【问题描述】:
我正在尝试为班级构建一个简单的基于文本的游戏,并且需要将项目添加到库存中。它应该阅读房间字典以找到该项目,然后如果键入 get Item 则将该项目添加到玩家库存中。在第一个房间时应该这样输出:
You are in Entrance Hall
Inventory [Items]
You Don't see anything useful
What do you wish to do?
我似乎无法弄清楚如何让我的代码这样做。我可能引用了一些错误的东西,但我对编码很陌生,所以我看不出我哪里出了问题。我的代码如下,非常感谢任何帮助。
# A dictionary for the simplified text game that links a room to other rooms.
rooms = {
'Entrance Hall': {'North': 'Great Hall', 'East': 'Gallery', 'West': 'Library'},
'Library': {'East': 'Entrance Hall', 'item': 'Book'},
'Gallery': {'West': 'Entrance Hall', 'item': 'Sword'},
'Great Hall': {'South': 'Entrance Hall', 'East': 'Dining Room', 'West': 'Cellar', 'item': 'Armor'},
'Dining Room': {'North': 'Kitchen', 'West': 'Great Hall', 'item': 'Plates'},
'Kitchen': {'South': 'Dining Room', 'item': 'Knife'},
'Cellar': {'North': 'Catacombs', 'East': 'Great Hall', 'item': 'Ale'},
'Catacombs': {'South': 'Cellar'}
}
instructions = 'A Necromancer has moved into your catacombs to raise an army of the undead! ' \
'You need to defeat him before he turns the dead against you and the town! ' \
'Before you can fight him,you are going to need 5 items to aid you. ' \
'You will need armor to protect your body against his attacks, ' \
'a book to learn how to protect yourself from his spells,' \
' a sword to defeat him, a candle to see in the dark,' \
'and a bottle of Ale to gather your courage and take on this task. ' \
'To move type: go North, go East, go West, go South' \
'to get items type get Item, ex: get Sword\n '
directions = ['go North', 'go South', 'go East', 'go West']
pick_up_items = ['get Ale', 'get Book', 'get Armor', 'get Sword', 'Necromancer', 'get Knife', 'get Candle']
print(instructions)
current_room = 'Entrance Hall'
item_in_room = 'None'
inventory = []
while True:
if inventory == ['Ale', 'Book', 'Armor', 'Sword', 'Candle']:
print('Congratulations! You collected all the items, you use you wits to head into the'
'cellar and defeat the Necromancer!')
elif current_room == 'Catacombs':
print('A bolt of magic streaks towards you and your vision goes black..... Game Over!')
break
# displays the players current location
print('You are in the {}.'.format(current_room))
print('Inventory:', inventory)
if item_in_room: #checks to see if there is an item in the room
print('You see a'.format(item_in_room))
else:
print("You don't see anything useful")
# gets the users input
command = input('\nWhat do you wish to do? ') # this controls the movement
if command in directions:
command = command.split()[1]
if command in rooms[current_room].keys():
current_room = rooms[current_room][command]
else:
# if the player inputs a bad movement
print('You cant go that way!')
# Checks to see if the player types a 'get' command and adds the item in the room to the players inventory.
if command in pick_up_items:
command = command.split()[1]
if command in rooms[current_room].keys():
inventory = current_room['item']
else:
# if the player inputs a item not in the room
print('You cant get that item here!')
【问题讨论】: