如何将库存添加到当前脚本
How to add inventory to the current script
所以多亏了这里的另一个人,我才让运动正常进行,但现在我不确定如何将我放入字典中的项目合并为玩家可以看到该项目在房间里并且可以得到它。或者检查最后一个有老板的房间。如果他们在拿到所有 6 件物品之前到达最后一个房间,他们就会死!这是代码,我将如何去做?
#define inventory and dictionary linking other rooms
rooms = {
'Entry Way': { 'North': 'Stalagmite Cavern'},
'Stalagmite Cavern': {'North': 'Grand Cavern', 'South': 'Entry Way', 'item': 'torch'},
'Grand Cavern': {'North': 'Hallway', 'East': 'Armory', 'West': 'Bedroom', 'South': 'Stalagmite Cavern', 'item': 'cross'},
'Armory': {'North': 'Treasure Trove', 'West': 'Grand Cavern', 'item': 'Stake'},
'Treasure Trove': {'South': 'Armory', 'item': 'silver'},
'Bedroom': {'North': 'Storage', 'East': 'Grand Cavern', 'item': 'elaborate comb'},
'Storage': {'South': 'Bedroom', 'item': 'mirror'},
'Hallway': {'North': 'Cliff Top', 'South': 'Grand Cavern'},
'Cliff Top': {'South': 'Hallway', 'item': 'Orla'}
}
def show_instructions():
#print a main menu and the commands
print("Thousand Year Vampire")
print("Collect 6 items to defeat the vampire or be destroyed by her.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
def show_status():
print(current_room)
print(inventory)
#print the player's current location
#print the current inventory
#print an item if there is one
# setting up inventory
inventory = []
# setting the starting room
starting_room = 'Entry Way'
# set current room to starting room
current_room = starting_room
#show game instructions
show_instructions()
show_status()
while True:
print("\nYou are currently in the {}".format(current_room))
move = input("\n>> ").split()[-1].capitalize()
print('-----------------------------')
# user to exit
if move == 'Exit':
current_room = 'exit'
break
# a correct move
elif move in rooms[current_room]:
current_room = rooms[current_room][move]
print('inventory:', inventory)
# incorrect move
else:
print("You can't go that way. There is nothing to the {}".format(move))
#loop forever until meet boss or gets all items and wins
我想你只需要像这样浏览dic:
def get_item():
for keys in rooms[current_room]:
if(keys=='item'):
inventory.append(rooms[current_room][keys])
所以多亏了这里的另一个人,我才让运动正常进行,但现在我不确定如何将我放入字典中的项目合并为玩家可以看到该项目在房间里并且可以得到它。或者检查最后一个有老板的房间。如果他们在拿到所有 6 件物品之前到达最后一个房间,他们就会死!这是代码,我将如何去做?
#define inventory and dictionary linking other rooms
rooms = {
'Entry Way': { 'North': 'Stalagmite Cavern'},
'Stalagmite Cavern': {'North': 'Grand Cavern', 'South': 'Entry Way', 'item': 'torch'},
'Grand Cavern': {'North': 'Hallway', 'East': 'Armory', 'West': 'Bedroom', 'South': 'Stalagmite Cavern', 'item': 'cross'},
'Armory': {'North': 'Treasure Trove', 'West': 'Grand Cavern', 'item': 'Stake'},
'Treasure Trove': {'South': 'Armory', 'item': 'silver'},
'Bedroom': {'North': 'Storage', 'East': 'Grand Cavern', 'item': 'elaborate comb'},
'Storage': {'South': 'Bedroom', 'item': 'mirror'},
'Hallway': {'North': 'Cliff Top', 'South': 'Grand Cavern'},
'Cliff Top': {'South': 'Hallway', 'item': 'Orla'}
}
def show_instructions():
#print a main menu and the commands
print("Thousand Year Vampire")
print("Collect 6 items to defeat the vampire or be destroyed by her.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")
def show_status():
print(current_room)
print(inventory)
#print the player's current location
#print the current inventory
#print an item if there is one
# setting up inventory
inventory = []
# setting the starting room
starting_room = 'Entry Way'
# set current room to starting room
current_room = starting_room
#show game instructions
show_instructions()
show_status()
while True:
print("\nYou are currently in the {}".format(current_room))
move = input("\n>> ").split()[-1].capitalize()
print('-----------------------------')
# user to exit
if move == 'Exit':
current_room = 'exit'
break
# a correct move
elif move in rooms[current_room]:
current_room = rooms[current_room][move]
print('inventory:', inventory)
# incorrect move
else:
print("You can't go that way. There is nothing to the {}".format(move))
#loop forever until meet boss or gets all items and wins
我想你只需要像这样浏览dic:
def get_item():
for keys in rooms[current_room]:
if(keys=='item'):
inventory.append(rooms[current_room][keys])