2011-10-11 20 views
0

先週私はクラスのキャノンゲームに取り組んできました。現在の反復は、ターゲットと衝突検出を追加することです。私の理解から、pygame.draw関数はRectオブジェクトを返します。私はこれらのオブジェクトをリストに追加し、このリストを私の大砲に渡します。 Cannonballはそのリストを使用して、何かヒットしたかどうかを検出します。しかし、self.current_ball.Rect.collidelist(self.collision_objects)> -1: の場合は、 "私は"を受け取るAttributeError: 'NoneType'オブジェクトに属性 'Rect' "エラーがありません。pygameキャノンボールの衝突検出

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 

私はこのコードに何か問題がある場合は、非常にわからない、またはそれは実際にcollision_objectsリストの問題ですか?

答えて

0

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()関数を適切に呼び出すかどうかを確認する必要があります。

+0

私はRectを削除しようとしていましたが、なぜRectオブジェクトをRectに呼び出すのですか?不幸にもそれは私に同じエラーを依然として与える。 GUIの中では、描画されたオブジェクトをcannonball_objectとして保存し、描画後にターゲットと同じオブジェクトを保存します。私は、リスト(先を考える)ではなく、2つのオブジェクトだけを使用しようとし、衝突を使用しました。正直なところ、私はこれによってperpelexedです。 –

+0

エラーの原因が見つかりました。 collision_detectionが実行しようとした後、オブジェクトが格納されました。注文を真っ直ぐにして、プログラムは問題なく走った。 –