ʜᴀᴢ :
# filename: bouncing_ball_pygame.py
# Install pygame if you don't have it: pip install pygame
import sys
import pygame
from random import randint
pygame.init()
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption("Bouncing Ball — Pygame")
# Ball properties
x, y = WIDTH // 2, HEIGHT // 2
vx, vy = 4, 3
radius = 24
color = (randint(50,255), randint(50,255), randint(50,255))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move
x += vx
y += vy
# Bounce on walls
if x - radius = WIDTH:
vx = -vx
color = (randint(50,255), randint(50,255), randint(50,255))
if y - radius = HEIGHT:
vy = -vy
color = (randint(50,255), randint(50,255), randint(50,255))
# Draw
screen.fill((20, 24, 30))
pygame.draw.circle(screen, color, (int(x), int(y)), radius)
# simple shadow
pygame.draw.ellipse(screen, (0,0,0,60), (x-radius//1.5, y+radius-6, radius*1.6, 12))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
2025-08-18 12:41:18