轻微的问题,tl; dr在底部。
相当的Python初学者在这里。在这段文字冒险中,我正试图让物品与世界互动,真是令人沮丧。我可以让玩家拾取物品并进行交易,但是我无法让物品与世界互动。我的目标是:一个房间需要物品才能让玩家继续。到目前为止,除了显示不同的文本外,我不需要任何其他复杂的操作,即"单元已锁定。找到键!"没有库存中的钥匙,并且"您已解锁单元!"与库存中的钥匙。最终,我需要玩家输入。我知道可以使用简单的"如果库存中有物品"语句来完成此操作,但是它似乎没有用。
我的房间代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class 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"
You unlock the cell with your key. Congratulations, you freed your uncle and won the game!"
else:
return"
You 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类:
1
2
3
4
5
6
7
8
9class 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
我在主要游戏代码中有此行,因此大写字母不是问题:
1player = Player()
我还有其他一些房间使用" player.inventory"调用(添加项,删除项等),并且工作正常,例如以下非常相似的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class 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"
An empty area of the rec yard. Nowhere to turn but back the way you came."
else:
return"
You spot some brass knuckles on the ground!"
该代码在拾取物品之前/之后显示正确的文本,并且毫无问题地将物品添加到玩家的库存中。
最终,我想用更复杂的"锁与键"谜题制作游戏,而我无法完成这个简单的示例,这令我感到困扰。任何和所有帮助将不胜感激。
Tl; dr我的房间显示类似"牢房已锁定。钥匙必须在某处...",而播放器没有钥匙。当玩家拥有钥匙时,我希望它显示"您已解锁单元!"管他呢。尝试"如果库存中的项目"无济于事。沮丧请。救命。
编辑:
抱歉,这可能会有所帮助。主要游戏功能:
1
2
3
4
5
6
7
8
9
10
11
12def 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")调用返回,您将发现问题所在。
噢,斯科特,你有意思。一大堆藤壶简直就是我在嬉戏。尽管我仍然不了解在其他类(FindWeaponTile)而不是GoalCell类中如何正确使用player.inventory。 [对OOP来说非常非常新]
如果我们没有一个最小,完整和可验证的示例,仍然很难看到问题出在哪里。发布堆栈跟踪会有所帮助。
哦,我的老兄安德鲁·林(Andrew Im)非常抱歉,感谢您要求堆栈跟踪。令我震惊的是,属性错误是我之前尝试的错误,而不是我上面显示的尝试。为清楚起见,对问题进行了编辑。
您必须在print(room.intro_text())之前使用room.modify_player(player)
只是尝试了,不幸的是它没有用。谢谢你的建议!
我没有使用items.CellKey()本身,而是访问了它的name属性(字符串),并且可以正常工作。 谢谢大家的帮助!