[英]Why am i getting this error trying to add trap to my textRPG
I have been trying to add traps to my TextRPG I have something I think will work with a little bit of debugging but the first bug that I ran into is.我一直在尝试在我的 TextRPG 中添加陷阱我认为可以通过一些调试来工作,但我遇到的第一个错误是。
TypeError: init () should return None, not 'str' TypeError: init () 应该返回 None,而不是 'str'
the error is coming from this.错误来自于此。
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
when i comment out this block of code everything runs as it should.当我注释掉这段代码时,一切都按原样运行。 I'm at a loss as to what to do about it.
我不知道该怎么办。 ill post a link to the github repo the code is in world.py starts on line 146.
我会发布一个指向 github 存储库的链接,代码在 world.py 中,从第 146 行开始。
https://github.com/GusRobins60/AdventureGame.git https://github.com/GusRobins60/AdventureGame.git
The __init__
method in python should only used be used to initialize the class variables. python 中的
__init__
方法应该只用于初始化类变量。 You are returning a string from it, which you should not do .您正在从中返回一个字符串,您不应该这样做。
You can either remove the return statement or set the string to another variable.您可以删除 return 语句或将字符串设置为另一个变量。 Here is an example of what you can probably do:
以下是您可能可以执行的操作的示例:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.