Skip to content

Commit

Permalink
Merge pull request #577 from ShishirMagge/shishir/bouncing_ball_simul…
Browse files Browse the repository at this point in the history
…ation

Bouncing Ball Simulation #447
  • Loading branch information
sumanth-0 authored Nov 10, 2024
2 parents e563c8e + 627156b commit 2345e0f
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Bouncing_Ball_Simulation/bouncing_ball_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pygame
import sys

# Initialize Pygame
pygame.init()

# Screen configuration
WIDTH, HEIGHT = 800, 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ball bouncing simulation")

# Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# Ball Configuration
ball_radius = 10
ball_x = ball_radius # Start position on the left edge
ball_y = HEIGHT // 2 # Vertical center
ball_speed_x = 5 # Horizontal speed
ball_speed_y = 8 # Vertical speed

# Main game loop
running = True
while running:
screen.fill(WHITE) # Clear screen with white background

# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Update ball position
ball_x += ball_speed_x

ball_y -= ball_speed_y

# Bounce off the edges
if ball_x - ball_radius <= 0 or ball_x + ball_radius >= WIDTH:
ball_speed_x = -ball_speed_x # Reverse direction

if ball_y + ball_radius >= HEIGHT or ball_y - ball_radius <= 0:
ball_speed_y = -ball_speed_y
# Draw the ball
pygame.draw.circle(screen, RED, (ball_x, ball_y), ball_radius)

# Update the display
pygame.display.flip()

# Delay to control frame rate
pygame.time.delay(20)

# Quit Pygame
pygame.quit()
sys.exit()

0 comments on commit 2345e0f

Please sign in to comment.