简体   繁体   English

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

[英]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.

相关问题 为什么我在尝试运行我的 Discord 机器人时会收到此错误 - Why am I getting this error when I am trying to run my Discord bot 为什么在 Tkinter 中尝试将标签小部件添加到屏幕时出现“错误的窗口路径名”错误? - Why am I getting a "bad window path name" error when trying to add a label widget to the screen in Tkinter? 尝试重新启动井字游戏时,为什么会出现此错误? - Why am i getting this error when trying to restart my tic tac toe game? 为什么我在尝试挑选一个对象时遇到定义__slots__的类的错误? - Why am I getting an error about my class defining __slots__ when trying to pickle an object? (只能将str(而不是“int”)连接到str)这是我得到的错误为什么它说我正在尝试添加字符串和int? - (can only concatenate str (not “int”) to str) this is the error i am getting why is it saying that i am trying to add string and int? 我试图在问题中的每个数字上加一个,但是我收到了没有意义的语法错误? - I am trying to add one to each digit in my problem but I am getting a syntax error that doesn't make sense? 尝试升级我的点子后出现错误 - I am getting an error after trying to upgrade my pip 为什么我在尝试冒泡 plot 类别时会收到值错误? - Why am I getting a value error for trying to bubble plot categories? 为什么在Python 2.7中尝试打印字符串时出现错误? - Why am I getting an error trying to print a string in Python 2.7? 为什么在尝试与字典值进行比较时出现错误? - Why am I getting an error when trying to compare to dictionary values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM