【问题标题】:Having trouble setting up inventory设置库存时遇到问题
【发布时间】:2020-12-06 04:13:04
【问题描述】:

我正在开发一款基于文本的游戏,玩家必须在不同的房间中找到 6 件物品,然后才能遇到老板,否则他们会死亡。我在房间的字典中设置了项目,但是当玩家四处移动时,我不知道如何从中拉出。我目前所拥有的让玩家能够将东西添加到库存中,但随后它就会陷入永久循环。我对此很陌生,我无法将事物连接在一起。这就是 cmets 的全部内容。

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

【问题讨论】:

    标签: python inventory adventure


    【解决方案1】:

    一个好的开始,这里有一些修改可以激发你的想法,你可以完成自己或根据你喜欢的场景进行更改 - 但发现你自己的更改:) 我已经测试了这段代码:

    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
    

    祝你好运

    【讨论】:

      【解决方案2】:

      如果每个房间只有一个物品,我认为game()函数中的下面这行应该去掉

      while True:
      

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

      【讨论】:

      • 这确实有帮助,但 8 个房间中只有 6 个有物品。如何证明这 6 个房间里有物品?
      • 您提供的代码中调用的 game() 函数在哪里?我认为这将有助于我回答您的问题
      • 就是这样,我不知道该放在哪里。当我把它放在调用函数show_status 下时,它只是开始了一个无限循环,用户可以在其中将项目添加到库存中,但我只希望玩家能够在某些房间中选择某些项目。这些项目列在顶部每个房间字典的末尾。
      • 也许把它放在 current_room = rooms[current_room][move] 行之后,并去掉 game() 函数中的 while True。此外,您将需要创建类似字典的内容,其中键是位置,值是这些位置的项目(并且可能使用 None 之类的东西作为没有项目的位置的值),这样您就可以完全表示世界。当您在该位置拿起物品时,请记住将位置的相应值更改为无。
      • 好的!这对我帮助很大!我现在可以移动房间并添加项目,但他们可以添加他们想要的任何项目。我怎样才能使它只能从字典中的项目中获取?
      猜你喜欢
      • 1970-01-01
      • 2014-08-11
      • 1970-01-01
      • 2019-11-01
      • 2021-06-19
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2011-09-20
      相关资源
      最近更新 更多