Example bouncing ball using PyGame


We will create a simple example of bouncing ball using Pygame. I use Fedora 18 and for installation and using Pygame “1.9.1release” from Fedora repository. Here is small code to make bouncing ball using Pygame:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import sys
import os
import pygame

CURRENT = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, CURRENT)

pygame.init()

size = width, height = 640, 400
speed = [1, 1]
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load(CURRENT + "/ball.png")
ballrect = ball.get_rect()
ballrect.topleft = [0, 0]

screen.blit(ball, ballrect)
   
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    screen.fill(black)
    screen.blit(ball, ballrect)
   
    ballrect = ballrect.move(speed)
   
    if ballrect.left > width or ballrect.right > width:
        speed[0] = -speed[0]
    elif ballrect.left < 0:
        speed[0] = +abs(speed[0])
       
    if ballrect.top > height or ballrect.bottom > height:
        speed[1] = -speed[1]
    elif ballrect.top < 0:
        speed[1] = +abs(speed[1])

    pygame.display.flip()

It will give us results :

Remember to create your “ball.png” and put in the same folder with this scripts.

Update:
Here is the same scripts in decent format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import sys
import os
import pygame

class Game():
    def __init__(self):
        CURRENT = os.path.abspath(os.path.dirname(__file__))
        pygame.init()

        size = self.width, self.height = 640, 400
        self.speed = [1, 1]
        self.black = 0, 0, 0

        self.screen = pygame.display.set_mode(size)
       
        self.ball = pygame.image.load(CURRENT + "/ball.png")
        self.ballrect = self.ball.get_rect()
        self.ballrect.topleft = [0, 0]

        self.screen.blit(self.ball, self.ballrect)


    def start(self):
        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT: sys.exit()

            self.screen.fill(self.black)
            self.screen.blit(self.ball, self.ballrect)

            self.ballrect = self.ballrect.move(self.speed)

            if (self.ballrect.left > self.width or
                self.ballrect.right > self.width):
                self.speed[0] = -self.speed[0]
            elif self.ballrect.left < 0:
                self.speed[0] = +abs(self.speed[0])

            if (self.ballrect.top > self.height or
                self.ballrect.bottom > self.height):
                self.speed[1] = -self.speed[1]
            elif self.ballrect.top < 0:
                self.speed[1] = +abs(self.speed[1])

            pygame.display.flip()

if __name__ == "__main__":
    game = Game()
    game.start()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.