tags:

views:

51

answers:

1

Hi there, I have a question and I was wondering if anyone could help...

How do I pass ball_boundary_2 and ball_boundary_3 separately as one parameter to ball_collisions function?

Here is the code below:

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

Any help would be appreciated! Thanks

A: 

I'd consider passing a list of ball boundaries rather than individual ball boundaries. (Is that your question?)

If this doesn't answer your question, then shorten the answer, it's far too long and wordy. (My eyes glazeth over!)

Arafangion