Python - прыгающие шарики? Как передать отдельно как один параметр? - PullRequest
0 голосов
/ 18 октября 2010

Как передать ball_boundary_2 и ball_boundary_3 отдельно в качестве одного параметра в функцию ball_collisions?

Вот код ниже:

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

Ответы [ 2 ]

0 голосов
/ 06 января 2011

Вы можете просто обернуть x-границу и y-границу в кортеж и передать это функции ball-collisions.

ball-collisions((x, y), velocity, sound)
0 голосов
/ 18 октября 2010

Я бы рассмотрел возможность передачи списка границ шариков, а не отдельных шариков. (Это твой вопрос?)

Если это не отвечает на ваш вопрос, то сокращайте ответ, он слишком длинный и многословный. (Мои глаза застеклены!)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...