import pygame
import sys
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("跳动的小球 - Pygame 示例")
WHITE = (255, 255, 255)
RED = (255, 0, 0)
ball_radius = 30
ball_x = WIDTH // 2
ball_y = ball_radius
ball_speed_y = 5
gravity = 0.5
bounce_damping = 0.8
clock = pygame.time.Clock()
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
ball_speed_y += gravity
ball_y += ball_speed_y
if ball_y + ball_radius > HEIGHT:
ball_speed_y = -ball_speed_y * bounce_damping
ball_y = HEIGHT - ball_radius
screen.fill(WHITE)
pygame.draw.circle(screen, RED, (int(ball_x), int(ball_y)), ball_radius)
pygame.display.flip()
pygame.quit()
sys.exit()