2016-05-13 5 views
-1

イム私は、複数のファイルと以下のコードを使用してゲームを作成するには水たまりの略で、私はスプライトを持っているファイル、2クラスPlayer()、ボールBall()複数のクラスhandlin - pygameの

あるので、私はしたいですどういうわけか(私はおそらくプレイヤークラスから継承することは知っていませんが、それで何もできませんでした)は、Player()で作成された水たまりのエッジのコントロールを持っています。Player()の水玉の間にボールが跳ね返るようにclass Ball()ボールが> = 560ピクセルの場合

class Player(pg.sprite.Sprite): 

    def __init__(self): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((130,20)) 
     self.image.fill(black) 
     self.rect = self.image.get_rect() 
     self.rect.centerx = width/2 
     self.rect.y = 560 

    def update(self): 
     self.vx = 0 
     keys = pg.key.get_pressed() 

     if keys[pg.K_LEFT]: 
      self.vx = -15 
     if keys[pg.K_RIGHT]: 
      self.vx = 15 
     if self.rect.left <= 0: 
      self.rect.left = 0 
     if self.rect.right >= width: 
      self.rect.right = width 

     self.rect.x += self.vx 


class Ball(pg.sprite.Sprite): 

    def __init__(self): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((10,10)) 
     self.image.fill(black) 
     self.rect = self.image.get_rect() 
     self.rect.centerx = width/2 
     self.rect.centery = height/2 
     self.vx = -4 
     self.vy = 10 

    def update(self): 
     if self.rect.y >= 560:# and... how do i declare the puddle here??? 
      self.vy = -self.vy 
     if self.rect.top <= 0: 
      self.vy = -self.vy 
     if self.rect.right <= 0: 
      self.vx = -self.vx 
     if self.rect.left >= width: 
      self.vx = -self.vx 

     self.rect.x += self.vx 
     self.rect.y += self.vy 

答えて

1

paddle.rect.colliderect(ball.rect)を使用して、ボールとパドルの衝突をテストできます。あなたは後で衝突領域からボールを​​移動する必要があります。

+0

私は別のクラスの中の別のファイルのオブジェクトを持っているので、ここでball.rectを定義することはできません。またはpaddle.rect:/ – Aris

+0

しかし、それは一撃を与える! – Aris

+0

THanks dude !! :) – Aris

関連する問題