设置库存时遇到问题

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

如果每个房间只有一个项目,我认为应该删除 game() 函数中的以下行

while True:

因为这将导致无限循环,前三个打印输出是“你得到了……”、“……已添加到库存……”和“[库存内容]”,但下一个打印输出将一遍又一遍地成为“你已经得到了这个”和“[库存内容]”。

一个不错的开始,这里有一些修改作为一个小推动来激发你的想法,你可以完成自己或根据你喜欢的场景进行更改 - 但是你自己发现变化:)我已经测试过这个代码:

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

祝你好运