0

我希望从游戏开始时从我的字典中删除一个房间,而雪地靴 = False。当雪地靴 = True 时,我希望房间可以到达,并且我想拿起雪地靴使它们为 True。

如果这是有道理的。

roomDirections = {
    "hallEnt":{"e":"hallMid"},
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
    "snowRoom":{"n":"hallMid"},
    "giantNature":{"s":"strangeWall", "e":"riverBank", "w":"hallMid"},
    "strangeWall":{"s":"hallOuter", "e":"riverBank", "n":"giantNature"},
    "riverBank":{"e":"lilyOne", "w":"giantNature"},
    "lilyOne":{"e":"lilyTwo", "w":"riverBank", "n":"riverBank", "s":"riverBank"},
    "lilyTwo":{"e":"riverBank", "w":"lilyThree", "n":"riverBank", "s":"riverBank"},
    "lilyThree":{"e":"riverBank", "w":"lilyFour", "n":"riverBank", "s":"riverBank"},
    "lilyFour":{"e":"riverBank", "w":"treasureRoom", "n":"riverBank", "s":"riverBank"},
    "treasureRoom":{"w":"hallEnt"},
}


roomItems = {
    "hallEnt":["snowboots"],
    "snowRoom":["lamp"],
    "treasureRoom":["treasure"],
    }

snowboots = lamp = treasure = False

这些是我的字典和我所谓的变量。

if "snowboots" == False:
            del roomDirections["hallMid"]
        else:
            print ("you cannot go that way")

这是为了从 roomDirections 中删除 hallMid ,因此不可能从中移动,直到...

elif playerInput in roomItems[currentRoom]:
        print("picked up", playerInput)
        invItems.append(playerInput)
        playerInput == True
        for i in range(0, len(roomItems[currentRoom])):
            if playerInput == roomItems[currentRoom][i]:
                del roomItems[currentRoom][i]
                break

雪地靴 = True,这是这个块应该做的,但它似乎没有工作,我是关闭还是完全偏离轨道?

编辑——我的主要游戏循环——

while True:
    playerInput = input("What do you want to do? ")
    playerInput = playerInput.lower()
    if playerInput == "quit":
        break

    elif playerInput == "look":
        print(roomDescriptions[currentRoom])




    elif playerInput in dirs:
        playerInput = playerInput[0]
        if playerInput in roomDirections[currentRoom]:


            currentRoom = roomDirections[currentRoom][playerInput]
            print(roomEntrance [currentRoom])
        else:
            print("You can't go that way")

    elif playerInput == "lookdown":
        if currentRoom in roomItems.keys():
            print ("You see", roomItems[currentRoom])
        else:
            print ("You see nothing on the ground")

    elif playerInput == "inventory" or playerInput == "inv":
        print (invItems)




    elif playerInput in roomItems[currentRoom]:
        print("picked up", playerInput)
        invItems.append(playerInput)       
        for i in range(0, len(roomItems[currentRoom])):
            if playerInput == roomItems[currentRoom][i]:
                del roomItems[currentRoom][i]
                break

    elif playerInput in invItems:
        print("dropped", playerInput)
        roomItems[currentRoom].append (playerInput)
        for i in range (0, len(invItems)):
            if playerInput == invItems[i]:
                del invItems[i]
                break
    else:
        print ("I don't understand")
4

3 回答 3

1

据我了解,您想添加一些条件来决定是否允许通过特定出口。您目前拥有映射每个房间的方向的字典、用于保存玩家是否拥有每个项目的变量以及每个房间中的项目列表和玩家库存。注意项目变量是多余的;您可以简单地检查库存。

您建议的方法是在获得或丢失所需物品时添加和删除房间的出口。这是可以做到的,但是首先要忽略它们,找出要删除的出口的复杂性是唯一的;如果它们被删除,则恢复它们比根据需要过滤它们更难。这是一种方法:

requirements = {'snowRoom': 'snowboots', 'darkCave': 'lamp'}
reasons = {'snowboots': "You'd sink into the snow.",
           'lamp': "It would be too dark to see."}

如果条件不满足,您可以使用这些来忽略方向:

elif playerInput in dirs:
    playerInput = playerInput[0]
    if playerInput in roomDirections[currentRoom]:
        newRoom = roomDirections[currentRoom][playerInput]
        required = requirements.get(newRoom)
        if required and required not in invItems:
            print("You can't go that way. " + reasons[required])
        else:
            currentRoom = newRoom
            print(roomEntrance [currentRoom])
    else:
        print("You can't go that way")

您也可以这样做,这样玩家就无法移除房间中所需的物品:

elif playerInput in invItems:
    if playerInput != requirements[currentRoom]:
        print("dropped", playerInput)
        roomItems[currentRoom].append (playerInput)
        invItems.remove(playerInput)
    else:
        print("You still need " + playerInput + ". " + reasons[required])

采用更面向对象的方法可能更有意义,其中房间实际上包含其项目列表、到其他房间的链接和要求。您还可以执行诸如向项目添加同义词之类的操作。

于 2014-11-19T15:52:42.187 回答
0

让我在这里指出你的错误。你有snowboots(这是一个对象)但你正在比较"snowboots"(这是一个字符串)。因此,当您"snowboots"if条件下进行比较时,它会返回 false,因此它else总是会分开。试试下面的代码,如果它对你有帮助,请告诉我。

snowboots = False
if snowboots == False:              ## notice the absence of double quotes here 
    del roomDirections["hallMid"]
else:
    print ("you cannot go that way")

编辑:为了更清楚起见,我编辑了您的代码以显示它del有效。我已删除

roomDirections = {
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
}

print roomDirections  ## print dictionary before your deleting.

snowboots = lamp = treasure = False

if snowboots == False:              ## notice the absence of double quotes here 
    del roomDirections["hallMid"]
else:
    print ("you cannot go that way")

print roomDirections                ## print dictionary after deleting

输出:

    {'hallMid': {'s': 'snowRoom', 'e': 'giantNature', 'w': 'hallEnt'}}   ## before deleting

     {}     ## after deleting.
于 2014-11-19T04:52:26.517 回答
0

playerInput是一个字符串。看起来您需要将字符串映射到布尔值。

items = {'snowboots' : False,
         'lamp' : False,
         'treasure' : False}

然后改变

if "snowboots" == Falseif not items['snowboots']

playerInput == Trueitems[playerInput] = True

snowboots = lamp = treasure = False

for item in items:
    items[item] = False

看起来您对字符串和名称有概念上的问题,并且似乎在您的代码中将它们混合在一起。"foo"不一样foo

>>> 
>>> foo = 2
>>> "foo" = 2
SyntaxError: can't assign to literal
>>> foo = "foo"
>>> foo
'foo'
>>> "foo" = foo
SyntaxError: can't assign to literal
>>> foo = False
>>> bool("foo")
True
>>> bool(foo)
False
>>>

您将需要检查所有代码并解析所有类似代码。

于 2014-11-19T15:03:59.350 回答