Python program to bouncing ball in Pygame
In this article, you will learn how to create a bouncing ball using the Python Pygame. Pygame is a collection of Python modules intended for composing computer games. Pygame includes functionality on top of the magnificent SDL library. This permits you to make fully featured games and interactive multi media programs in the Python language. It is free, highly portable, and runs on nearly every platform and operating system.
Import and Initialize Pygame
First, let's import the pygame and sys modules at the top of your python file.
import sys, pygame
pygame.init()
Pygame set screen size, background color and caption
The pygame.display.set_mode() function returns the surface object for the window. This function accepts the width and height of the screen as arguments. To set the caption of the window, call the pygame.display.set_caption() function.
width = 550
height = 300
color = (255, 250, 250)
screen = pygame.display.set_mode(width, height)
pygame.display.set_caption("Pygame bouncing ball")
Load the moving object and set the rectangle area covering the image in Pygame
Here, we have opened the image using the pygame.image.load() method and set the ball rectangle area boundary using the get_rect() method.
ball = pygame.image.load("ball.png")
rect_boundry= ball.get_rect()
Set speed of the moving object in Pygame
Pygame provides the move() method to make an object move smoothly across the screen. The syntax is-
move(forward, right)
All the attributes are optional. But for the bouncing ball, we have set the following-
speed = [1, 1]
rect_boundry= rect_boundry.move(speed)
Make ball movement continuity in Pygame
We have created an infinite loop to make the ball move continuously and reverse the direction of the ball if it hits the edges.
while 1:
rect_boundry= rect_boundry.move(speed)
if rect_boundry.left < 0 or rect_boundry.right > width:
speed[0] = -speed[0]
if rect_boundry.top < 0 or rect_boundry.bottom > height:
speed[1] = -speed[1]
Fill the background color and blit the screen
The fill() method is used to fill the surface with a background color. In the above, we have mentioned how to create a moving object. But, we also need to render them on the window. In pygame, this is called blitting of an object and is implemented with the blit() method.
screen.fill(color)
screen.blit(ball, rect_boundry)
Make image visible in Pygame
We have used the pygame flip() method to make all images visible.
pygame.display.flip()
Complete Code: Bouncing ball program in Python
In the above, we have explained the bouncing ball algorithm in detail with pygame codes. Here, we have merged all the above to get the complete code.
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import sys, pygame
from pygame.locals import *
pygame.init()
speed = [1, 1]
color = (255, 250, 250)
width = 550
height = 300
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame bouncing ball")
ball = pygame.image.load("ball.png")
rect_boundry = ball.get_rect()
while 1:
for event in pygame.event.get():
rect_boundry = rect_boundry.move(speed)
if rect_boundry.left < 0 or rect_boundry.right > width:
speed[0] = -speed[0]
if rect_boundry.top < 0 or rect_boundry.bottom > height:
speed[1] = -speed[1]
screen.fill(color)
screen.blit(ball, rect_boundry)
pygame.display.flip()
if event.type == QUIT:
pygame.quit()
sys.exit()
When, you will run the above code, it returns the following-
Related Articles
How to draw different shapes using Python PygamePython send mail to multiple recipients using SMTP server
How to generate QR Code in Python using PyQRCode
Python programs to check Palindrome strings and numbers
CRUD operations in Python using MYSQL Connector
Fibonacci Series Program in Python
Python File Handler - Create, Read, Write, Access, Lock File
Python convert XML to JSON
Python convert xml to dict
Python convert dict to xml
Python multiline string
glob in Python
Python heap implementation
zip function in Python
Remove last element from list Python
Check if list is empty Python
Remove element from list Python
Python split multiple delimiters
Python loop through list
Python iterate list with index
Python add list to list