[英]Having trouble setting up inventory
我正在開發一個基於文本的游戲,玩家必須在不同的房間里找到 6 件物品,然后才能遇到老板,否則他們會死。 我在房間的字典中設置了項目,但是當玩家四處移動時,我不知道如何從中拉出。 我目前擁有的內容是讓玩家能夠將東西添加到庫存中,但隨后它就會陷入永久循環。 我對此很陌生,我無法將事物連接在一起。 這是帶有評論的全部內容。
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 = []
def game():
inventory = []
# simulate picking up items
while True:
item = input()
if item in inventory: # use in operator to check membership
print("you already have got this")
print(" ".join(inventory))
else:
print("You got ", item)
print("its been added to inventory")
inventory.append(item)
print(" ".join(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
一個好的開始,這里有一些修改,作為一個小小的推動來激發你的想法,你可以完成自己或根據你喜歡的場景進行更改 - 但發現你自己的更改:) 我已經測試了這段代碼:
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 the 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 = []
def game(item):
# simulate picking up items
if item in inventory: # use in operator to check membership
print("you already have got this")
print(" ".join(inventory))
else:
print("You got ", item)
print("its been added to inventory")
inventory.append(item)
print(" ".join(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))
if 'item' in rooms[current_room]:
game(rooms[current_room]['item'])
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 meeting boss or gets all items and wins
祝你好運
如果每個房間只有一個物品,我認為應該去掉 game() function 中的以下行
while True:
因為這將導致無限循環,前三個打印輸出是“你得到......”和“......添加到庫存......”和“[庫存內容]”但下一個打印輸出將是“你已經一遍又一遍地得到這個”和“[庫存內容]”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.