2009-04-21 10 views
1

J'ai codé une animation (en python) pour faire rebondir un ballon de plage autour d'un écran. Je souhaite maintenant ajouter une deuxième balle à la fenêtre, et quand les deux entrent en collision pour qu'ils se rebondissent les uns les autres.Comment ajouter une deuxième balle rebondissante à la fenêtre?

Jusqu'ici, mes tentatives ont été infructueuses. Des idees pour faire cela? Le code que j'ai jusqu'ici est ci-dessous.

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

Répondre

7

Voici une restructuration très basique de votre code. Il pourrait encore être rangé beaucoup, mais il devrait vous montrer comment vous pouvez utiliser les instances de la classe.

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

Vous venez de refactoriser l'introduction de pygame. http://www.pygame.org/docs/tut/intro/intro.html –

+0

Avez-vous vraiment besoin de passer pygame à la fonction __init__? – tgray

+0

@tgray: Je l'ai fait de cette façon d'utiliser le même code que l'exemple autant que possible. En pensant que cela pourrait faciliter la compréhension de l'OP –

3

Vous devriez probablement créer une classe pour représenter votre beachball. Ensuite, vous pouvez en citer autant que vous le souhaitez et placer les instances dans une liste Python.

Vous devez ensuite parcourir cette liste sur chaque image, en la mettant à jour et en la restituant.

Vous devrez inclure une méthode pour tester la collision contre une autre balle (ceci est simple pour les cercles). Si une collision est détectée, les balles impliquées doivent simuler un rebondissement les unes des autres.

Questions connexes