-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #577 from ShishirMagge/shishir/bouncing_ball_simul…
…ation Bouncing Ball Simulation #447
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |