How to create bouncing ball animation in pygame

In this article we will explore and write the popular bouncing ball animation with pygame.

First make the logical step in our mind.

What we want?

We want to create a bouncing ball animation?

How it will bounce?

It will bounce and move until find the edge of the display. The the ball will through back by the wall. So the display boundary will work like ground or wall like dropping the basket ball on the field.

How it will bounce? the algorithm?

If we imagine display four wall.. upper , lower, right, left

If the ball has hit any of the four walls of the screen. If the ball hits the right or left wall (x-axis), it reverses its horizontal speed (horizontal_speed *= -1). And also if the ball hits the top or bottom wall (y-axis), it reverses its vertical speed (vertical_speed *= -1).

Now we are ready to program….

In the code we are using python class to make it easy draw many balls with minimal code.




import pygame,sys,random
from pygame.locals import*
pygame.init()
clock=pygame.time.Clock()
display_x=600
display_y=600
screen=pygame.display.set_mode((display_x, display_y))
lightgreen=(144, 238,144)





class BouncingBall():
	def __init__(self):
		self.circle_radius=20
		self.display_position_x=random.randint(self.circle_radius, display_x - self.circle_radius)
		self.display_position_y=random.randint(self.circle_radius, display_y - self.circle_radius)
		self.horizontal_speed=random.randint(1,10)
		self.vertical_speed=random.randint(1,10)
		
	def show(self):
		pygame.draw.circle(screen, (255, 255, 100), (self.display_position_x, self.display_position_y), self.circle_radius)
		
	def update(self):
		self.display_position_x+=self.horizontal_speed
		self.display_position_y+=self.vertical_speed
		if self.display_position_x+ self.circle_radius >= display_x or self.display_position_x- self.circle_radius <= 0:
			self.horizontal_speed *= -1
		if self.display_position_y+ self.circle_radius >= display_y or self.display_position_y- self.circle_radius <= 0:
			self.vertical_speed *=-1

total_ball_number=50
list_of_balls=[BouncingBall() for i in range(total_ball_number)]




game_running=True
while game_running:
	clock.tick(60)
	for event in pygame.event.get():
		if event.type==QUIT:
			pygame.quit()
			sys.exit()
	screen.fill(lightgreen)
	
	for i in range(total_ball_number):
		list_of_balls[i].show()
		list_of_balls[i].update()
	pygame.display.update()	



Run the code and just change the total_ball_number variable . If you set the value 100, it will draw 100 bouncing ball.

You can get 48+ pygame example including this example from github

Simple and readable code Examples are better than tons of explanations. I tried to keep the variable name as much readable as possible. Also you can practice the rainfall animation example with pygame in the same github repository.

Try and customize the code by yourself. And Make it your own style!

Also there is another version of the with 10k bouncing ball with random color!

https://github.com/01one/pygameExamples/blob/main/51.10k_bouncing_ball_with_random_color.py

Published by

mashiurrahman99

Programmer