나는 비치 볼이 화면 주위로 튀어 오르는 애니메이션 (파이썬으로)을 코딩했습니다. 이제 두 번째 볼을 창에 추가하고 두 사람이 충돌하여 서로 튀어 나오게하려고합니다.두 번째 튀는 공을 창에 추가하는 방법은 무엇입니까?
지금까지 나의 시도는 실패했습니다. 어떤 아이디어가 이것을 어떻게? 내가 지금까지 가지고있는 코드는 아래와 같습니다.
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()
파이 게임 소개를 리팩토링했습니다. http://www.pygame.org/docs/tut/intro/intro.html –
파이 게임을 __init__ 함수에 전달해야합니까? – tgray
@tgray : 가능한 한 예제와 동일한 코드를 사용했습니다. OP가 쉽게 이해할 수 있다고 생각하면 –