簡體   English   中英

為什么我在嘗試向 textRPG 添加陷阱時遇到此錯誤

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

我一直在嘗試在我的 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 行開始。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM