溫和的長問題,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()代碼類似。
你在哪裏/如何調用'modify_player'? –
'Player' *模塊*沒有庫存;一個'Player' *對象*。或者,也許我誤解了「一杯藤壺」的含義。 –
那麼,你的問題很明顯:你期望被調用的方法不是。繼續進行'print'跟蹤,從'print'(「key used」)調用返回,然後你會發現什麼是錯誤的。 –