├── README.md ├── ball.png ├── background-img.png └── file.py /README.md: -------------------------------------------------------------------------------- 1 | # Bouncing-Ball. 2 | Bouncing-Ball in Python. 3 | -------------------------------------------------------------------------------- /ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdullahthewebbee/Bouncing-Ball-main/HEAD/ball.png -------------------------------------------------------------------------------- /background-img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abdullahthewebbee/Bouncing-Ball-main/HEAD/background-img.png -------------------------------------------------------------------------------- /file.py: -------------------------------------------------------------------------------- 1 | #This program shows the simulation of 8 balls bouncing under gravitational acceleration. 2 | #It is also accompanied by eleastic collission with walls of the container. 3 | #It is fun to watch. 4 | import pygame 5 | import time 6 | import random 7 | 8 | pygame.init() 9 | 10 | #setting screen size of pygame window to 1000 by 800 pixels 11 | screen=pygame.display.set_mode((1000,800)) 12 | background=pygame.image.load('background-img.png') 13 | 14 | #Adding title 15 | pygame.display.set_caption('Bouncing Ball Simulation') 16 | 17 | class ball: 18 | ball_image=pygame.image.load('ball.png') 19 | g=1 20 | def __init__(self): 21 | self.velocityX=6 22 | self.velocityY=6 23 | self.X=random.randint(0,968) 24 | self.Y=random.randint(0,550) 25 | 26 | def render_ball(self): 27 | screen.blit(ball.ball_image, (self.X,self.Y)) 28 | def move_ball(self): 29 | #changing y component of velocity due to downward acceleration 30 | self.velocityY+=ball.g 31 | 32 | #changing position based on velocity 33 | self.X+=self.velocityX 34 | self.Y+=self.velocityY 35 | 36 | #collission with the walls lead to change in velocity 37 | if self.X<0 or self.X>968: 38 | self.velocityX*=-1 39 | 40 | if self.Y<0 and self.velocityY<0: 41 | self.velocityY*=-1 42 | self.Y=0 43 | 44 | if self.Y>768 and self.velocityY>0: 45 | self.velocityY*=-1 46 | self.Y=768 47 | 48 | #list of balls created as objects 49 | Ball_List=[ball(),ball(), ball(), ball(), ball(), ball(), ball(), ball()] 50 | 51 | #The main program loop 52 | running=True 53 | while running: 54 | for event in pygame.event.get(): 55 | if event.type == pygame.QUIT: 56 | running=False 57 | 58 | time.sleep(0.02) 59 | screen.blit(background, (0,0)) 60 | for ball_item in Ball_List: 61 | ball_item.render_ball() 62 | ball_item.move_ball() 63 | pygame.display.update() --------------------------------------------------------------------------------