如何将ball_boundary_2和ball_boundary_3分别作为一个参数传递 to 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()
答案 0 :(得分:0)
我会考虑传递一个球边界列表而不是单个球边界。 (这是你的问题吗?)
如果这不能回答你的问题,那么缩短答案,这太冗长而且冗长。 (我的眼睛闪过!)
答案 1 :(得分:0)
你可以将x边界和y边界包裹在一个元组中,并将其传递给函数ball-collisions。
ball-collisions((x, y), velocity, sound)