14. To write a python program simulate bouncing ball in Pygame.
import pygame
# Set up the Pygame window
WIDTH, HEIGHT = 800, 600
FPS = 60
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bouncing Ball")
clock = pygame.time.Clock()
# Set up the ball
BALL_RADIUS = 30
ball_pos = [WIDTH // 2, HEIGHT // 2]
ball_vel = [4, 0]
# Main game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the ball
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
# Bounce off the walls
if ball_pos[0] < BALL_RADIUS or ball_pos[0] > WIDTH - BALL_RADIUS:
ball_vel[0] = -ball_vel[0]
if ball_pos[1] < BALL_RADIUS or ball_pos[1] > HEIGHT - BALL_RADIUS:
ball_vel[1] = -ball_vel[1]
# Clear the screen and draw the ball
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (255, 0, 0), ball_pos, BALL_RADIUS)
# Update the display
pygame.display.flip()
# Wait for the next frame
clock.tick(FPS)
# Clean up
pygame.quit()