我一直试图在我的TextRPG中添加陷阱,我认为有些东西可以进行一些调试,但是遇到的第一个错误是
。TypeError: init ()应该返回None,而不是'str'
错误源于此。
class TrapRoomTile(MapTile):
def __init__(self, x, y):
r = random.randint(1,2)
if r == 1:
self.trap = items.PitFall()
self.tripped_text = "The open hole of a Pit Fall trap obstructs the tunnel."
self.set_text = "The floor in this hallway is unusually clean."
else:
return"""
Looks like more bare stone...
"""
super().__init__(x, y)
def modify_player(self,player):
if not self.trap.is_tripped():
player.hp = player.hp - self.items.damage
print("You stumbled into a trap!")
time.sleep(1)
print("\nTrap does {} damage. You have {} HP remaining.".
format(self.items.damage, player.hp))
def intro_text(self):
text = self.tripped_text if self.items.is_tripped() else self.set_text
time.sleep(0.1)
return text
当我注释掉这段代码时,一切都会按预期运行。我对此无所适从。不适用于发布指向github存储库的链接,代码在world.py中,从第146行开始。
答案 0 :(得分:0)
python中的__init__
方法只应用于初始化类变量。您正在从中返回一个字符串,您不应这样做。
您可以删除return语句或将字符串设置为另一个变量。这是您可能会执行的操作的示例:
class TrapRoomTile(MapTile):
def __init__(self, x, y):
r = random.randint(1,2)
if r == 1:
self.trap = items.PitFall()
self.tripped_text = "The open hole of a Pit Fall trap obstructs the tunnel."
self.set_text = "The floor in this hallway is unusually clean."
else:
self.set_text = "Looks like more bare stone..."
super().__init__(x, y)
def modify_player(self,player):
if not self.trap.is_tripped():
player.hp = player.hp - self.items.damage
print("You stumbled into a trap!")
time.sleep(1)
print("\nTrap does {} damage. You have {} HP remaining.".
format(self.items.damage, player.hp))
def intro_text(self):
text = self.tripped_text if self.trap.is_tripped() else self.set_text
time.sleep(0.1)
return text