質問

私は、画面の周りバウンスするビーチボールのために(Pythonで)アニメーションをコード化しています。私は今のウィンドウに第2のボールを追加したい、と彼らはお互いをオフにバウンスするために二人は衝突したとき。

これまでのところ、この時の私の試みは成功していません。任意のアイデアはどのようにこれを行うには?私がこれまで持っているコードは以下の通りです。

import pygame

import sys

if __name__ =='__main__':

    ball_image = 'Beachball.jpg'
    bounce_sound = 'Thump.wav'
    width = 800
    height = 600
    background_colour = 0,0,0
    caption= 'Bouncing Ball animation'
    velocity = [1,1]
    pygame.init ()
    frame = pygame.display.set_mode ((width, height))
    pygame.display.set_caption (caption)
    ball= pygame.image.load (ball_image). convert()
    ball_boundary = ball.get_rect (center=(300,300))
    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.left < 0 or ball_boundary.right > width:
            sound.play()
            velocity[0] = -1 * velocity[0]
        if ball_boundary.top < 0 or ball_boundary.bottom > height:
            sound.play()
            velocity[1] = -1 * velocity[1]

        ball_boundary = ball_boundary.move (velocity)
        frame.fill (background_colour)
        frame.blit (ball, ball_boundary)
        pygame.display.flip()
役に立ちましたか?

解決

ここにあなたのコードの非常に基本的な再構築です。それはまだ多くのことを片付けすることができ、それはあなたが、クラスのインスタンスを使用する方法をお見せする必要があります。

import pygame
import random
import sys

class Ball:
    def __init__(self,X,Y):
        self.velocity = [1,1]
        self.ball_image = pygame.image.load ('Beachball.jpg'). convert()
        self.ball_boundary = self.ball_image.get_rect (center=(X,Y))
        self.sound = pygame.mixer.Sound ('Thump.wav')

if __name__ =='__main__':
    width = 800
    height = 600
    background_colour = 0,0,0
    pygame.init()
    frame = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Bouncing Ball animation")
    num_balls = 1000
    ball_list = []
    for i in range(num_balls):
        ball_list.append( Ball(random.randint(0, width),random.randint(0, height)) )
    while True:
        for event in pygame.event.get():
            print event 
            if event.type == pygame.QUIT:
                sys.exit(0)
        frame.fill (background_colour)
        for ball in ball_list:
            if ball.ball_boundary.left < 0 or ball.ball_boundary.right > width:
                ball.sound.play()
                ball.velocity[0] = -1 * ball.velocity[0]
            if ball.ball_boundary.top < 0 or ball.ball_boundary.bottom > height:
                ball.sound.play()
                ball.velocity[1] = -1 * ball.velocity[1]

            ball.ball_boundary = ball.ball_boundary.move (ball.velocity)
            frame.blit (ball.ball_image, ball.ball_boundary)
        pygame.display.flip()

他のヒント

あなたはおそらく、あなたのビーチボールを表すクラスを作成する必要があります。そして、あなたが好きなように多くのインスタンスをいただきたい、とPythonリストにインスタンスを配置します。

あなたは、各を更新し、レンダリング、各フレームにそのリストを行くと思います。

あなたは(これは円のために簡単です)別のボールとの衝突をテストするための方法を含める必要があります。衝突が検出された場合、関係するボールが互いに離れるバウンスをシミュレートする必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top