0% found this document useful (0 votes)
11 views1 page

requirements

This document contains a Python script using Pygame to create a ball bounce simulation. The simulation features multiple balls that move and bounce off the walls of the window, with gravity affecting their vertical velocity. The program includes a main loop that updates the display and handles user events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

requirements

This document contains a Python script using Pygame to create a ball bounce simulation. The simulation features multiple balls that move and bounce off the walls of the window, with gravity affecting their vertical velocity. The program includes a main loop that updates the display and handles user events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#It is also accompanied by eleastic collission with walls of the container.

#It is fun to watch.


import pygame,time,random

pygame.init(?)

#setting screen size of pygame window to 800 by 600 pixels


screen=pygame.display.set_mode((800,600))
background=pygame.image.load('background-img.jpg')

#Adding title
pygame.display.set_caption('Ball Bounce Simulation')

class ball:
ball_image=pygame.image.load('ball.png')
g=1
def __init__(self):
self.velocityX=4
self.velocityY=4
self.X=random.randint(0,768)
self.Y=random.randint(0,350)

def render_ball(self):
screen.blit(ball.ball_image, (self.X,self.Y))
def move_ball(self):
#changing y component of velocity due to downward acceleration
self.velocityY+=ball.g
#changing position based on velocity
self.X+=self.velocityX
self.Y+=self.velocityY
#collission with the walls lead to change in velocity
if self.X<0 or self.X>768:
self.velocityX*=-1
if self.Y<0 and self.velocityY<0:
self.velocityY*=-1
self.Y=0
if self.Y>568 and self.velocityY>0:
self.velocityY*=-1
self.Y=568
#list of balls created as objects
Ball_List=[ball(),ball(), ball(), ball(), ball()]

#The main program loop


running=True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running=False

time.sleep(0.02)
screen.blit(background, (0,0))
for ball_item in Ball_List:
ball_item.render_ball()
ball_item.move_ball()
pygame.display.update()

You might also like