简体   繁体   English

为什么我的列表没有变化?

[英]Why doesn't my list change?

I've been hacking away on a little game, just for fun, and I've run into a problem. 为了娱乐,我一直在忙着玩一个小游戏,但我遇到了一个问题。 I'll post the code and try my best to explain: 我将发布代码并尽力解释:

def parseCmd(string):
    cmd = string.split(' ')
    if cmd[0] == 'help':
        showHelp()
    elif cmd[0] == 'add':
        addServer()
    elif cmd[0] == 'bag':
        viewInventory(inventory)
    elif len(cmd) == 1 and cmd[0] == 'look':
        describeRoom()
    elif len(cmd) == 1 and cmd[0] == 'take':
        print 'What do you want me to take?'
    elif cmd[0] == 'take':
        pickUp(cmd[1],  items)
    elif cmd[0] == 'exit':
        sys.exit(0)
    else:
        print 'I don\'t know how to '  + cmd[0]

def describeRoom():
    print locations[player_location]

def pickUp(item,  item_list):
    if item in item_list[player_location]:
        item_list[player_location].remove(item)
        inventory.append(item)
        print 'You took the ' + item        
    else:
        print 'I can\'t find any ' + item

inventory = ['id card',  'money',  'keys']
player_location = 'cookieroom'
items = {'cookieroom': ['crowbar',  'hammer']}
locations = {'cookieroom': 'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: %s' % items[player_location], 
                'LFA': 'The infamous LFA, where dreams of office supplies become reality. there is a big guy sleeping in his chair next to a fire extinguisher.\n\nSOUTH: Cookieroom, WEST: WC'}

if __name__ == "__main__":
    while 1:
        t = raw_input('-> ')
        parseCmd(t)

So, as you can see I want the list of items in the items dictionary to change when you pick up an item available in that specific room. 因此,正如您所看到的,当您领取该特定房间中可用的项目时,我希望更改项目字典中的项目列表。 I can pick up the item and it gets added to my inventory but if I issue the command 'look' it shows the list of items in it's original state. 我可以拿起物品并将其添加到我的库存中,但是如果我发出命令“ look”,它会以其原始状态显示物品清单。

I've been googling and stackoverflowing for 1½ day now and I can't find anything that seems to solve this problem. 我一直在谷歌搜索和stackoverflowing 1.5天,我找不到任何似乎可以解决此问题的方法。

If something is unclear, just ask me and I'll try to answer. 如果不清楚,请问我,我会尽力回答。

The locations dictionary, which is from where the describeRoom function picks up its room description, is initialised once when the program starts. 程序启动后,将对describeRoom函数从其获取房间描述的locations字典进行初始化。 At that time, the location of the player is the cookieroom and the objects there are the crowbar and the hammer . 那时,玩家所在的位置是cookieroom ,而物体则是crowbarhammer So, a string is created like so 因此,像这样创建一个字符串

'The cookieroom, where all the hard work gets done. \n\nNORTH: LFA - ITEMS: ["crowbar", "hammer"]'

This string never changes even if you later alter the contents of the items dictionary. 即使您以后更改items字典的内容,该字符串也不会改变。

Your locations dictionary should only contain the non changing part of the room description. 您的locations字典应仅包含房间说明的不变部分。 The changing part (eg the list of items in the room etc.) should be recomputed everytime the users requests the description of the room. 每当用户要求描述房间时,都应重新计算变化部分(例如房间中的物品清单等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM