compiler-errors - 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()函数。

https://stackoverflow.com/questions/7728336/

相关文章:

java - 比较原始类型

java - 编译时出现一个或多个错误的NetBeans项目

compiler-errors - nant 构建文件使用 nant 0.86 而不是 0.91 a

compilation - apt-pkg编译问题

asp.net - 运行时CS0234在VS2010 Web应用程序中访问SOAP Web服务时出错

compiler-construction - lex 使用来自 lex.yy.c 文件的 flex

objective-c - UIAlertView dismissWithClickedButton

java - Java编译时错误: reached end of file while parsin

c++ - 新手Chrome插件开发人员需要NPAPI入门方面的帮助

php - 生成php_perl.dll失败