2009-04-25 13 views
1

私はプログラミングで非常に新しくなったのですが、それは私の学校に科目として導入されたばかりなので、助けが必要です。私は3つのボール(矩形の画像)のアニメーションをスクリーンの周りにバウンドさせたり、互いに離したりするタスクを与えられています。私は3つのボールと壁の跳ね返りがすべて下がっているが、どうやってお互いに跳ね返るのか分からない。すべてのヘルプをいただければ幸いです、私の現在のコードは次のとおりです。長方形の移動方向を逆にするにはどうすればよいですか?

import pygame 
import random 
import sys 

if __name__ =='__main__': 

    ball_image1 = 'beachball1.jpg' 
    ball_image2 = 'beachball2.jpg' 
    ball_image3 = 'beachball3.jpg' 
    bounce_sound = 'thump.wav' 
    width = 800 
    top = 600 
    x = 0 
    y = 0 
    background_colour = 0,0,0 
    caption= 'Bouncing Ball animation' 
    velocity1 = [-1,-1] 
    velocity2 = [-1,1] 
    velocity3 = [1,-1] 
    pygame.init() 
    frame = pygame.display.set_mode ((width, top)) 
    pygame.display.set_caption (caption) 
    ball1= pygame.image.load (ball_image1). convert() 
    ball2= pygame.image.load (ball_image2). convert() 
    ball3= pygame.image.load (ball_image3). convert() 
    ball_boundary_1 = ball2.get_rect (center=(random.randint(50, 750),random.randint(50, 550))) 
    ball_boundary_2 = ball2.get_rect (center=(random.randint(50, 750),random.randint(50, 550))) 
    ball_boundary_3 = ball2.get_rect (center=(random.randint(50, 750),random.randint(50, 550))) 
    sound = pygame.mixer.Sound (bounce_sound) 
    while True: 
     for event in pygame.event.get(): 
      print event 
      if event.type == pygame.QUIT: sys.exit(0) 
     if ball_boundary_1.left < 0 or ball_boundary_1.right > width: 
      sound.play() 
      velocity1[0] = -velocity1[0] 
     if ball_boundary_1.top < 0 or ball_boundary_1.bottom > top: 
      sound.play() 
      velocity1[1] = -velocity1[1] 
     if ball_boundary_2.left < 0 or ball_boundary_2.right > width: 
      sound.play() 
      velocity2[0] = -velocity2[0] 
     if ball_boundary_2.top < 0 or ball_boundary_2.bottom > top: 
      sound.play() 
      velocity2[1] = -velocity2[1] 
     if ball_boundary_3.left < 0 or ball_boundary_3.right > width: 
      sound.play() 
      velocity3[0] = -velocity3[0] 
     if ball_boundary_3.top < 0 or ball_boundary_3.bottom > top: 
      sound.play() 
      velocity3[1] = -velocity3[1] 

     ball_boundary_1 = ball_boundary_1.move (velocity1) 
     ball_boundary_2 = ball_boundary_2.move (velocity2) 
     ball_boundary_3 = ball_boundary_3.move (velocity3) 
     frame.fill (background_colour) 
     frame.blit (ball1, ball_boundary_1) 
     frame.blit (ball2, ball_boundary_2) 
     frame.blit (ball3, ball_boundary_3) 
     pygame.display.flip() 

答えて

3

とき2つのオブジェクトの衝突 "彼らは基本的に同じ物理的空間に存在し、したがって、あなたはこれをチェックする必要があります。 2dでは、これはすばらしく、特に長方形の場合は簡単です。基本的に2つのボールが衝突した場合に真の値を返す 'overlap'という関数を書く。たとえば:

for (i = 0; i < numberOfBalls; i++) { 
    for (j = i+1; j < numberOfBalls; j++) { 
     if (overlap (ball_rect[i], ball_rect[j])) { 
      // you will need to write this reverse velocity code differently 
      // to comply with the code above but I recommend that the balls 
      // go in an array with three elements so that you can index 
      // them. (Useful in for loops as seen above) 
      ball_xvelocity[i] *= -1; 
      ball_xvelocity[j] *= -1; 
      ball_yvelocity[i] *= -1; 
      ball_yvelocity[j] *= -1; 
     } 
    } 
} 

私はあなた次第重複機能を残していますが、「た矩形衝突検出を」GOOGLE必要があり、あなたが方法を見つけるだろう。本当に恐ろしいことではなく、プログラミングの概念を研究して学ぶことができるという信念を与えるでしょう。

N.B.具体的には、Pythonコードを提供していません。宿題なので、答えを書くのはあなたの仕事です。申し訳ありません、ただのSOの方針です。お役に立てれば。 :)

1

実際には、あなたが提供するコードにいくつかの手がかりがあります。ボールが壁に跳ね返るかどうかを検出する方法は、ボールの上端が0より小さいかどうかを確認することです。つまり、ボールが上の境界線に触れているので、方向を変える必要があります。

だから、コード:それは床や天井にバウンス場合

if ball_boundary_1.top < 0 or ball_boundary_1.bottom > top: 
     sound.play() 
     velocity1[1] = -velocity1[1] 

は、基本的にはボールの垂直方向の動きに変化します。

これを修正するには、2つのボールがお互いに跳ね返った場合の意味を自分に尋ねる必要があります。それは、境界がいくつかの方法で重なっていることを意味します。たとえば、ボール1の左右どちらかがボール2の水平寸法の内側にあり、ボール1の上部または底部がボール2の垂直寸法の内側にあるかどうかを確認することができます。次に、2つのボールが接触して、ボール。

1

第2ボールは枠の周りにバウンスされたら、私の講師は、この上で私の特定の評価に

を追加すると、それらが衝突した場合、ボールの方向を変更するためにいくつかのコードを追加します。 PyGameは、2つのRectオブジェクト間で衝突が発生したかどうかを検出する機能を提供します。この関数の名前はcolliderectで、次のように呼び出されます。 2つのRectオブジェクトがball_boundary_1とball_boundary_2命名されている場合、あなたはとの衝突をチェックすることもできますので、

if ball_boundary_1.colliderect(ball_boundary_2): 

    # alter the direction of the balls with the usual method 

、これは衝突が発生した「場合は、」チェックするために使用される、Trueを返し、とにいくつかのコードを実行します逆速度。幸運、私は実際にこのビットをやってhavnt、私はちょうどそれをしようとしています

関連する問題