2016-11-08 67 views
-1

温和的长问题,tl;博士在底部。Python文本冒险 - 与游戏世界互动的物品

很不错的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..." 

(编辑)我道歉,上面的代码不会返回一个错误 - 它根本不起作用。即使钥匙在玩家的库存中,所有显示的都是文字“你进入你废弃的叔叔的牢房......”。 Player类被引用:

class Player: 
    def __init__(self): 
      self.inventory = [items.Fists(), 
           items.CellKey()] 
      self.x = world.start_tile_location[0] 
      self.y = world.start_tile_location[1] 
      self.hp = 100 
      self.money = 0 
      self.victory = False 

我在游戏主代码这一行,所以资本是不是一个问题:

player = Player() 

而且我有使用“player.inventory其他房间“呼叫(追加项目,要删除的项目等),这些工作就好了,像下面的非常相似的代码:

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我的房间显示“监狱牢房被锁定,钥匙必须在某处......”,而玩家没有钥匙。当玩家拥有钥匙时,我希望它显示“你解开牢房!”管他呢。试过“如果库存物品”无济于事。失意。请。帮帮我。

编辑: 对不起,这可能会有所帮助。主要游戏功能:

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()代码类似。

+0

你在哪里/如何调用'modify_player'? –

+0

'Player' *模块*没有库存;一个'Player' *对象*。或者,也许我误解了“一杯藤壶”的含义。 –

+0

那么,你的问题很明显:你期望被调用的方法不是。继续进行'print'跟踪,从'print'(“key used”)调用返回,然后你会发现什么是错误的。 –

回答

0

而不是使用items.CellKey()本身我访问它的名称属性(一个字符串),它的工作。感谢大家的帮助!