TypeError: detect()只接受8个参数(给定)

时间:2022-06-27 20:27:19

i keep getting this type error TypeError: detectCollisions() takes exactly 8 arguments (9 given)

我不断得到这个类型error: detectcollision()正好有8个参数(给定9个)

def detectCollisions(x1,y1,w1,h1,x2,y2,w2,h2,):

    if (x2+w2>=x1>=x2 and y2+h2>=y1>=y2):

        return True

    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1>=y2):

        return True

    elif (x2+w2>=x1>=x2 and y2+h2>=y1+h1>=y2):

        return True

    elif (x2+w2>=x1+w1>=x2 and y2+h2>=y1+h1>=y2):

        return True

    else:

        return False

def update(self,gravity,blocklist):
    if(self.velocity<0):
        self.falling=True
    blockX,blockY=0,0

    collision=False

    for block in blocklist:

        collision = self.detectCollisions(self.x, self.y, self.width, self.height, block.x, block.y, block.width, block.height)

im not sure what is wrong with it, i have already added a def for detect collision

我不知道它有什么问题,我已经添加了一个用于检测碰撞的def

2 个解决方案

#1


2  

I'm pretty sure you missed self in your method declaration

我很确定你在方法声明中漏掉了self。

def detectCollisions(x, y, ....

The first argument should be self, then your custom arguments.

第一个参数应该是self,然后是自定义参数。

When calling it, the self is passed implicitly (so it gets 9 arguments, when you used just 8 in the call)

当调用它时,self被隐式地传递(当调用中只使用8个参数时,它会得到9个参数)

#2


2  

This is a typical problem learning python. The first parameter in a member function is always the "self"-parameter. It is included in the call "behind the scenes".

这是学习python的典型问题。成员函数中的第一个参数总是“self”-parameter。它包含在“幕后”的号召中。

That is why it says 9 arguments when you've only given 8. Here is a class declaration illustrating this, try play around with the code:

这就是为什么它说9个参数,而你只给了8个。这里有一个类声明来说明这一点,请尝试使用代码:

class TestClass():
    def test(self, _param1, _param2):
        print("_param1:" + str(_param1) + "_param2:" + _param2)

_instance = TestClass()
_instance.test("a", "b")
_instance.test("a", "b", "c")

The first call works, the second doesn't and you get that typical error message. This also goes the other way, you always have to add the "self" parameter to the declaration.

第一个调用有效,第二个调用无效,您将得到典型的错误消息。这也是另一种方式,您总是需要向声明添加“self”参数。

#1


2  

I'm pretty sure you missed self in your method declaration

我很确定你在方法声明中漏掉了self。

def detectCollisions(x, y, ....

The first argument should be self, then your custom arguments.

第一个参数应该是self,然后是自定义参数。

When calling it, the self is passed implicitly (so it gets 9 arguments, when you used just 8 in the call)

当调用它时,self被隐式地传递(当调用中只使用8个参数时,它会得到9个参数)

#2


2  

This is a typical problem learning python. The first parameter in a member function is always the "self"-parameter. It is included in the call "behind the scenes".

这是学习python的典型问题。成员函数中的第一个参数总是“self”-parameter。它包含在“幕后”的号召中。

That is why it says 9 arguments when you've only given 8. Here is a class declaration illustrating this, try play around with the code:

这就是为什么它说9个参数,而你只给了8个。这里有一个类声明来说明这一点,请尝试使用代码:

class TestClass():
    def test(self, _param1, _param2):
        print("_param1:" + str(_param1) + "_param2:" + _param2)

_instance = TestClass()
_instance.test("a", "b")
_instance.test("a", "b", "c")

The first call works, the second doesn't and you get that typical error message. This also goes the other way, you always have to add the "self" parameter to the declaration.

第一个调用有效,第二个调用无效,您将得到典型的错误消息。这也是另一种方式,您总是需要向声明添加“self”参数。