3. Python program to bouncing ball in
Pygame:
In this article, you will learn how to program a
bouncing ball using 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.
4. 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()
5. 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)
6. Set speed of the moving object:
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)
7. Load the moving object and set the
rectangle area covering the image
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()
8. Make ball movement continuity:
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=rectboundry.move(speed) if
rect_boundry.left < 0 or rect_boundry.right >
width: speed[0] = -speed[0] if rect_boundry.top
< 0 or rectboundry.bottom > height:
speed[1] = -speed[1]
9. Fill the background color and blit the screen:
The fill() method is used to fill the surface with
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)
10. Make image visible:
We have used the pygame flip() method to
make all images visible.
pygame.display.flip()
11. Complete Code:
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()
12. 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()