在过去的一周中,我一直在上课时进行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
https://stackoverflow.com/questions/7728336/
相关文章:
java - 编译时出现一个或多个错误的NetBeans项目
compiler-errors - nant 构建文件使用 nant 0.86 而不是 0.91 a
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