如何正确使用collidepoint()函数

问题描述 投票:0回答:1

我正在尝试使用'碰撞点'功能来检测玩家是否在游戏中或正在接触平台。根据我的理解,您使用它的方式是执行“ rectangle” .collidepoint(要在内部检查的点的坐标)。当我尝试在程序中使用该功能时,出现以下错误:

 AttributeError: 'Platform' object has no attribute 'collidepoint'

我应该导入一个模块来使用此功能,我是否正确理解这一点?

这里是我制作的碰撞函数代码:

def collide(self, platforms):
    for plat in platforms:
        if plat.collidepoint(self.x, self.y):
            return True
    return False
python pygame collision-detection
1个回答
0
投票

我在这里有点猜测,因为您在这里没有包含太多内容,但是来自错误消息:

 AttributeError: 'Platform' object has no attribute 'collidepoint'

似乎您有一个名为Platform的类,并且正在通过参数'platforms'将它们的集合传递到collide方法中。但是,在您的代码中,您尝试对列表中的元素调用collidepoint()方法。

我的猜测是您的Platform类包含一个self.rect,您打算调用它。在这种情况下,您可能需要将行更改为:

        if plat.rect.collidepoint(self.x, self.y)

但这只是没有代码的猜测。

© www.soinside.com 2019 - 2024. All rights reserved.