2009-04-21 6 views
1

나는 비치 볼이 화면 주위로 튀어 오르는 애니메이션 (파이썬으로)을 코딩했습니다. 이제 두 번째 볼을 창에 추가하고 두 사람이 충돌하여 서로 튀어 나오게하려고합니다.두 번째 튀는 공을 창에 추가하는 방법은 무엇입니까?

지금까지 나의 시도는 실패했습니다. 어떤 아이디어가 이것을 어떻게? 내가 지금까지 가지고있는 코드는 아래와 같습니다.

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() 

답변

7

다음은 코드의 매우 기본적인 재구성입니다. 그것은 여전히 ​​많이 정돈 될 수 있지만 클래스의 인스턴스를 사용하는 방법을 보여 주어야합니다.

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() 
+1

파이 게임 소개를 리팩토링했습니다. http://www.pygame.org/docs/tut/intro/intro.html –

+0

파이 게임을 __init__ 함수에 전달해야합니까? – tgray

+0

@tgray : 가능한 한 예제와 동일한 코드를 사용했습니다. OP가 쉽게 이해할 수 있다고 생각하면 –

3

비치 볼을 나타내는 클래스를 만들어야합니다. 그런 다음 인스턴스를 원하는만큼 인스턴스화하고 인스턴스를 파이썬 목록에 넣습니다.

그런 다음 각 프레임에서 해당 목록을 살펴보고 각 프레임을 업데이트하고 렌더링합니다.

다른 공과의 충돌을 테스트하는 방법을 포함해야합니다 (이것은 원의 경우 간단 함). 충돌이 감지되면 관련된 볼들이 서로 떨어져서 튀어 나와야합니다.

관련 문제