compiler-errors - pygame炮弹碰撞检测

标签 compiler-errors collision-detection pygame

在过去的一周中,我一直在上课时进行Cannon游戏。我们当前的迭代是添加目标和碰撞检测。根据我的理解pygame.draw函数返回Rect对象。我将这些对象附加到列表中,然后将此列表传递给我的炮弹。然后,炮弹使用该列表来检测是否击中了任何东西。但是我收到“如果self.current_ball.Rect.collidelist(self.collision_objects)> -1:
AttributeError:“NoneType”对象没有属性“Rect””错误。

    def draw(self, screen):  
        '''
        Draws the cannonball
        '''      
        self.current_ball = pygame.draw.circle(screen, color["red"],(round(self._x),round(self._y)), 5)
        return self.current_ball

    def move(self):
        '''
        Initiates the cannonball to move along its
        firing arc
        '''
        while self.active:
            prev_y_v = self.y_v
            self.y_v = self.y_v - (9.8 * self.time_passed_seconds)
            self._y = (self._y - (self.time_passed_seconds * ((prev_y_v + self.y_v)/2)))
            self._y = max(self._y, 0)
            self._x += (self.delta_x)
            #self.active = (self._y > 0)
            self.collision_detect()
            if self.collide == True:
                self.active = False

    def collision_detect(self):
        '''
        Detects if the current cannonball has collided with any object within the 
        collision_objects list. 
        '''
        if self.current_ball.Rect.collidelist(self.collision_objects) > -1:
            self.collide = True

我不太确定这段代码是否有问题,或者实际上是冲突对象列表的问题?

最佳答案

Rect对象没有Rect属性。尝试删除.Rect属性,如下所示:

def collision_detect(self):
    '''
    Detects if the current cannonball has collided with any object within the 
    collision_objects list. 
    '''
    if self.current_ball.collidelist(self.collision_objects) > -1:
        self.collide = True

这里可能存在多个问题。据我所知self.current_ball应该是一个Rect对象,并且在您发布的代码中没有任何内容表明它收到“None”。

如果上面的修改不起作用,则可能需要检查其余的类代码,以检查是否正确调用了self.draw()函数。

关于compiler-errors - pygame炮弹碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7728336/

相关文章:

c++ - Pong 的 2D 碰撞检测

python - 按下回车键后如何让字符串出现

python - 使用 python 的 OpenGL 屏幕截图

java - 错误消息 : operator < cannot be applied to boolean, 整数

ruby-on-rails - 如何将实例发送到 Rails 中的部分

c++ - 对 C++ 中已定义函数的 undefined reference ?

ios - SpriteKit - 确定正方形碰撞的一侧

python - Microsoft Compilers for Python 2.7..无法下载,我应该使用哪个更高版本?

javascript - 使用 kineticJS 的碰撞检测(getIntersection 函数不起作用)

python - 如何向鼠标方向旋转 Sprite 并移动它?