-
Notifications
You must be signed in to change notification settings - Fork 1
/
ball_bounce_sim.py
39 lines (38 loc) · 1.17 KB
/
ball_bounce_sim.py
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
# AIM: Python program to simulate bouncing ball in Pygame
import pygame
import sys
# Initialize Pygame
pygame.init()
# load ball image
ball = pygame.image.load("data/ball.png")
# Set up the display
width, height = 800, 600
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Ball Bouncing Simulation")
pygame.display.set_icon(ball)
# Ball properties
ball = pygame.transform.scale(ball,(80,80))
ball_speed= [5, 5] # [x-speed, y-speed]
ball_position= [width // 2, height // 2]
# Main loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Update ball position
ball_position[0] += ball_speed[0]
ball_position[1] += ball_speed[1]
# Check for collision with walls
if ball_position[0] < 0 or ball_position[0] >width-80:
ball_speed[0] = -ball_speed[0]
if ball_position[1] < 0 or ball_position[1] >height-80:
ball_speed[1] = -ball_speed[1]
#Clear the screen
screen.fill((255, 255, 255))
# Draw the logo
screen.blit(ball, ball_position)
#Update the display
pygame.display.flip()
# Add a small delay to control the frame rate
pygame.time.delay(30)