有点长的问题,tl;下面是dr。在

这里的python初学者真不错。我有一个令人沮丧的时间试图让项目与世界互动在这个文本冒险。我可以让玩家拿起物品和交易物品,但是我不能让物品与世界互动。我的目标是:一个房间需要一个物品,以便玩家继续。到目前为止,我不需要任何比显示不同文本更复杂的东西,例如“单元格被锁定了”。找到钥匙!”在没有钥匙的情况下,还有“你解锁了牢房!”钥匙在库存里。最终我需要玩家输入。我知道这可以通过一个简单的“如果库存中的项目”语句来实现,但它似乎不起作用。在

我的房间代码:class GoalCell(MapTile):

def __init__(self, x, y):

self.item = items.CellKey()

self.have_key = False

super().__init__(x, y)

def modify_player(self, player):

if self.item in player.inventory:

self.have_key = True

player.inventory.remove(self.item)

print("Key used.") #I tried using this to see if this function was even running. Alas, it is not, and the statement does not show up.

def intro_text(self):

if self.have_key:

return "\nYou unlock the cell with your key. Congratulations, you freed your uncle and won the game!"

else:

return "\nYou enter the cell of your derelict uncle. 'Hey, kid,' he rasps, 'you gotta get me out of here.' The key has to be here somewhere..."

(编辑)抱歉,上面的代码没有返回错误-它只是不起作用。所有显示的都是文本“你进入你被遗弃叔叔的牢房…”即使钥匙在玩家的库存中。正在引用的播放器类:

^{pr2}$

我在主游戏代码中有这一行,所以大小写不是问题:player = Player()

我还有其他房间播放器.库存'调用(附加项、删除项等),这些操作很好,就像下面的代码一样:class FindWeaponTile(MapTile):

def __init__(self, x, y):

self.item = items.BrassKnuckles()

self.item_claimed = False

super().__init__(x, y)

def modify_player(self, player):

if not self.item_claimed:

self.item_claimed = True

player.inventory.append(self.item)

print("Weapon added.")

def intro_text(self):

if self.item_claimed:

return "\nAn empty area of the rec yard. Nowhere to turn but back the way you came."

else:

return "\nYou spot some brass knuckles on the ground!"

该代码在物品被捡起之前/之后显示正确的文本,并且它将该物品毫无问题地附加到玩家的库存中。在

最终,我想用更复杂的“锁和钥匙”谜题来制作游戏,但我不能做这个简单的例子,这让我很困扰。任何和所有的帮助将不胜感激。在

Tl;dr我的房间显示类似“监狱牢房被锁了”。钥匙一定在某处……”而玩家没有钥匙。当玩家拿到钥匙时,我希望它显示“You unlocked the cell!”或者别的什么。尝试“如果物品在库存中”无效。沮丧的。拜托。救命啊。在

编辑:

抱歉,这可能有帮助。主要游戏功能:def play():

print("Welcome to the Delaware County Correctional Facility!")

world.parse_world_dsl()

player = Player()

while player.is_alive() and not player.victory:

room = world.tile_at(player.x, player.y)

print(room.intro_text())

room.modify_player(player)

if player.is_alive() and not player.victory:

choose_action(room, player)

elif not player.is_alive():

print("The inmates got the best of you! Game over!")

编辑:

很抱歉,我编辑了原来的问题,因为我在上面显示的代码没有给我一个错误。我肯定错了。那个错误得到了处理。我的问题是代码没有按照我想要的方式工作。它所返回的只是当玩家没有钥匙时的介绍文本(即使他们有)。它就像我可以使它成为正确工作的FindWeaponTile()代码一样。在