私は、ビーチボールがスクリーンの周りを跳ね返るアニメーション(Pythonで)をコーディングしました。私は今、2番目のボールをウィンドウに追加し、2人が衝突してお互いに跳ね返りたいときに使います。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()
あなたはpygameの紹介をリファクタリングしました。 http://www.pygame.org/docs/tut/intro/intro.html –
本当にpygameを__init__関数に渡す必要がありますか? – tgray
@tgray:できるだけ例と同じコードを使用するようにしました。 OPが理解しやすくなると思っています –