为什么我在尝试向我的 textRPG 添加陷阱时遇到此错误

Why am i getting this error trying to add trap to my textRPG

我一直在尝试将陷阱添加到我的 TextRPG 我有一些东西我认为可以通过一些调试工作但是我 运行 遇到的第一个错误是。

TypeError: init() 应该 return 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

当我注释掉这段代码时,一切都按预期运行。我不知道该怎么做。 ill post link 到 github 代码库 world.py 从第 146 行开始。

https://github.com/GusRobins60/AdventureGame.git

python中的__init__方法只能用于初始化class变量。您正在 return 从中提取一个字符串,您 不应该这样做

您可以删除 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