我希望从游戏开始时从我的字典中删除一个房间,而雪地靴 = 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")
这是为了将大厅从房间中移除方向,因此不可能从中移动,直到......
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
雪地靴=是的,这是这个块应该做的,但它似乎不起作用,我是接近还是完全偏离轨道?
编辑 -- 我的主要游戏循环 --
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")
据我了解,您想添加一些条件来决定是否允许通过特定出口。您目前有映射每个房间方向的字典,用于保存玩家是否拥有每个物品的变量,以及每个房间中的物品列表和玩家物品栏。请注意,项目变量是多余的;您可以简单地检查库存。
您建议的方法是在获得或丢失所需物品时添加和删除房间的出口。这是可以做到的,但是找到要删除的出口的复杂性是您首先忽略它们所需要的;如果它们被删除,则恢复它们比根据需要过滤它们更难。这里有一种方法:
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])
采用更面向对象的方法可能是有意义的,其中房间实际上包含其项目列表、指向其他房间的链接和要求。您还可以执行诸如向项目添加同义词之类的操作。
让我在这里指出你的错误。您有snowboots
(这是一个对象),但您正在比较"snowboots"
(这是一个字符串)。因此,当您在if
条件下比较"snowboots"
时,它将返回 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.
playerInput
是一个字符串。 看起来您需要将字符串映射到布尔值。
items = {'snowboots' : False,
'lamp' : False,
'treasure' : False}
然后更改
if "snowboots" == False
到if not items['snowboots']
playerInput == True
到items[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
>>>
您将需要遍历所有代码并解析所有类似的代码。