Bouncing ball, python

# Bouncing ball


# Program
# Bouncing ball

import sys, pygame
pygame.init()


size = width, height = 700, 250
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)


pygame.display.set_caption("Bouncing ball")


ball = pygame.image.load("ball.jpg")
ballrect = ball.get_rect()


while 1:
pygame.time.delay(2)

for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()

ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]

screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()


   


ball.jpg
   

Output:


   




8 comments:

  1. Replies
    1. https://drranurekha.blogspot.com/2019/11/bouncing-ball-pygame.html

      Delete
  2. Mam I want algorithm for this bouncing ball program

    ReplyDelete
  3. I got it also very easy to understood

    ReplyDelete
  4. i cann't solve the error in 2nd line that "init" error
    please tell me how to solve it?

    ReplyDelete
    Replies
    1. install compatible version of pygame and python and make sure path are set correctly

      Delete
  5. Hello and thank you for the algorithm but what if i want to put for example 8 bouncing balls? Thank you very much :)

    ReplyDelete

Don't be a silent reader...
Leave your comments...

Anu