2010-10-18 10 views
0

Comment passer ball_boundary_2 et ball_boundary_3 séparément en tant que paramètre à la fonction ball_collisions?Python - Balles rebondissantes? Comment passer séparément comme un paramètre?

Voici le code ci-dessous:

## Import PyGame system 

import sys, pygame 

## Initialise PyGame system 

pygame.init() 

## Function to handle ball colissions 

### NOTE: Should alos include sound but we could not get it to work properly. ## 

def ball_collisions(ball_boundary, velocity, sound): 

## If ball_boundary collides with the rectangle of ball_boundary_2 than ... 

    if ball_boundary.colliderect(ball_boundary_2): 

## Change the direction of the balls 

     velocity[0] = -1 * velocity[0] 
     velocity[1] = -1 * velocity[1] 
     velocity_2[0] = -1 * velocity_2[0] 
     velocity_2[1] = -1 * velocity_2[1] 

## Or if there is a colission between any of the other balls do the same   

    elif ball_boundary.colliderect(ball_boundary_3): 
     velocity[0] = -1 * velocity[0] 
     velocity[1] = -1 * velocity[1] 
     velocity_3[0] = -1 * velocity_3[0] 
     velocity_3[1] = -1 * velocity_3[1] 

    elif ball_boundary_2.colliderect(ball_boundary_3): 
     velocity_2[0] = -1 * velocity[0] 
     velocity_2[1] = -1 * velocity[1] 
     velocity_3[0] = -1 * velocity_3[0] 
     velocity_3[1] = -1 * velocity_3[1] 

## Function to handel wall collisions   

def wall_collisions(ball_boundary, velocity, sound, width, height): 

## If ball_boundary collides with the right or left boundaries than ... 

    if ball_boundary.left < 0 or ball_boundary.right > width: 

## Play sound and ...   
     sound.play() 

## Reverse direction   
     velocity[0] = -1 * velocity[0] 

## Do same for top and bottom 

    if ball_boundary.top < 0 or ball_boundary.bottom > height: 

     sound.play() 

     velocity[1] = -1 * velocity[1] 

## Variables to hold important information to be called   

ball_image = 'beachball.jpg' 
bounce_sound = 'Thump.wav' 
width = 800 
height = 600 
background_colour = 0,0,0 
caption = 'Bouncing Ball animation' 
velocity = [1,1] 
velocity_2 = [1,-1] 
velocity_3 = [-1,1] 

## Create window frame and add caption to it 

frame = pygame.display.set_mode((width,height)) 
pygame.display.set_caption(caption) 

## Load image of ball and convert for PyGame to use 

ball = pygame.image.load(ball_image).convert() 

## Set rectangular boundaries for the ball images and set initial locations 

ball_boundary = ball.get_rect(center=(300,300)) 
ball_boundary_2 = ball.get_rect(center=(100,200)) 
ball_boundary_3 = ball.get_rect(center=(400,100)) 

## Readies sound to be played for collisions 

sound = pygame.mixer.Sound(bounce_sound) 

## Event Loop: 

while True: 
    for event in pygame.event.get(): 

## If statment for quiting of program 

     if event.type == pygame.QUIT: sys.exit(0) 

## Call wall_collisions function for each ball   

    wall_collisions(ball_boundary, velocity, sound, width, height) 
    wall_collisions(ball_boundary_2, velocity_2, sound, width, height) 
    wall_collisions(ball_boundary_3, velocity_3, sound, width, height) 

## Call ball_collisions function for each ball  

    ball_collisions(ball_boundary, velocity, sound) 
    ball_collisions(ball_boundary_2, velocity_2, sound) 
    ball_collisions(ball_boundary_3, velocity_3, sound) 

## Calculate the next position of each ball after collision. 

    ball_boundary = ball_boundary.move(velocity) 
    ball_boundary_2 = ball_boundary_2.move(velocity_2) 
    ball_boundary_3 = ball_boundary_3.move(velocity_3) 

## Fill the window background behind balls 

    frame.fill(background_colour) 

## Draw copy (blit) images 
    frame.blit(ball, ball_boundary) 
    frame.blit(ball, ball_boundary_2) 
    frame.blit(ball, ball_boundary_3) 

## Update display with completed image 
    pygame.display.flip() 

if __name__ =='__main__': 
    bounce() 
+0

Pouvez-vous formater le code pour qu'il soit lisible? –

Répondre

0

Je considère le passage d'une liste des limites de balle plutôt que des limites individuelles de balle. (Est-ce votre question?)

Si cela ne répond pas à votre question, alors écourtez la réponse, c'est beaucoup trop long et trop bavard. (Mes yeux se perdent!)

0

Vous pouvez simplement entourer la limite x et la limite y dans un tuple, et passer à la fonction billes-collisions.

ball-collisions((x, y), velocity, sound)