Untitled
unknown
plain_text
3 years ago
1.1 kB
6
Indexable
import pygame
# Initialize Pygame
pygame.init()
# Set up the screen
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Bouncing Ball")
# Set up the ball
ball_radius = 20
ball_pos = [width // 2, height // 2]
ball_vel = [5, 5]
# Set up the game loop
clock = pygame.time.Clock()
fps = 60
# Start the game loop
running = True
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update the ball position
ball_pos[0] += ball_vel[0]
ball_pos[1] += ball_vel[1]
# Check for collisions with 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
screen.fill((255, 255, 255))
# Draw the ball
pygame.draw.circle(screen, (255, 0, 0), ball_pos, ball_radius)
# Update the screen
pygame.display.flip()
# Limit the frame rate
clock.tick(fps)
# Clean up Pygame
pygame.quit()
Editor is loading...