我正在为我所在的班级开发基于文本的游戏。游戏的目标是在房间之间导航并从每个房间收集物品,然后将其添加到库存中,以击败最后一个房间。我的大部分代码输出正确,但是,当我尝试将项目添加到我的库存时,它没有做任何事情。库存是否也需要是字典?以及如何将嵌套字典“房间”中的物品添加到我的物品栏中,以便每次玩家获得物品时物品栏都会更新?
我的代码:
def show_instructions():
print('Welcome to the Zombie Apocalypse Text Adventure')
print('------------------------------------------------------------------
---')
print('You are the only human to survive the zombie apocalypse.')
print('You have found shelter in an abandoned hospital.')
print('Each room in the hospital contains an important item.')
print('There is a zombie hiding in the operating room.')
print('------------------------------------------------------------------
---')
print('Collect 6 items to defeat the zombie and win the game.')
print('Move Commands: go North, go South, go East, go West, get item,
Exit')
print('Add to Inventory: Get item')
print('------------------------------------------------------------------
---')
def show_status(current_room, inventory):
print('You are currently in the {}.'.format(current_room))
print('Your inventory contains: {}.'.format(inventory))
print('------------------------------------------------------------------
---')
def main(): # define the main function of the game
rooms = {
'Reception Area': {'West': 'Bathroom', 'East': 'Supply Room',
'South': 'Waiting Room',
'North': 'Patient Room'},
'Bathroom': {'East': 'Reception Area', 'item': 'First Aid Kit'},
'Waiting Room': {'North': 'Reception Area', 'East': 'Exam Room',
'item': 'Book'},
'Exam Room': {'West': 'Waiting Room', 'item': 'Medicine'},
'Supply Room': {'North': 'Closet', 'West': 'Reception Area', 'item':
'Scalpel'},
'Closet': {'South': 'Supply Room', 'item': 'Broom'},
'Patient Room': {'South': 'Reception Area', 'East': 'Operating Room',
'item': 'Syringe'},
'Operating Room': {'West': 'Patient Room', 'item': 'Zombie'}
}
current_room = 'Reception Area' # start player in the Reception Area
show_instructions() # show instructions to player
inventory = {}
while True: # begin gameplay loop
show_status(current_room, inventory) # show player's current status
move = input("Enter 'go North/South/East/West' to move, Get item, or
'Exit': ").split()[-1].capitalize()
# list possible moves for player to enter
if move == 'Exit': # if player's move is Exit
current_room = 'exit'
print('You are exiting the game. Thanks for playing!')
break
elif move in rooms[current_room]:
current_room = rooms[current_room][move]
if current_room == 'Operating Room':
if len(inventory) == 6:
print('Congratulations! You have found all items and won
the game!')
else:
print('You do not have enough items to defeat the
zombie... GAME OVER.')
exit(0) # game finishes
elif current_room == 'Bathroom': # if player reaches Bathroom
print('The item here is {}.'.format(rooms['Bathroom']
['item']))
if move == 'Get item': # if player gets item from room
print('Adding to inventory...') # print message
inventory = inventory.append('First Aid Kit')
print('Your inventory contains: {}'.format(inventory))
elif current_room == 'Waiting Room':
print('The item here is {}.'.format(rooms['Waiting Room']
['item']))
if move == 'Get item': # if player gets item from room
print('Adding to inventory...')
inventory = inventory.append('Book')
print('Your inventory contains: {}'.format(inventory))
elif current_room == 'Exam Room': # if player reaches Exam Room
print('The item here is {}.'.format(rooms['Exam Room']
['item']))
if move == 'Get item': # if player gets item
print('Adding to inventory...')
inventory = inventory.append('Medicine')
print('Your inventory contains: {}'.format(inventory))
elif current_room == 'Supply Room':
print('The item here is {}.'.format(rooms['Supply Room']
['item']))
if move == 'Get item': # if player gets item
print('Adding to inventory...')
inventory = inventory.append('Scalpel')
print('Your inventory contains: {}'.format(inventory))
elif current_room == 'Closet': # if player reaches Closet
print('The item here is {}.'.format(rooms['Closet']['item']))
if move == 'Get item': # if player gets item
print('Adding to inventory...')
inventory = inventory.append('Broom')
print('Your inventory contains: {}'.format(inventory))
elif current_room == 'Patient Room':
print('The item here is {}.'.format(rooms['Patient Room']
['item']))
if move == 'Get item': # if player gets item
print('Adding to inventory...')
inventory = inventory.append('Syringe')
print('Your inventory contains: {}'.format(inventory))
else: # if player enters invalid move
print("Invalid Move. There's no room to the {}".format(move)) # print error message
main() # execute main function