59 lines
1.3 KiB
Python
59 lines
1.3 KiB
Python
import pygame
|
|
|
|
# 初始化pygame
|
|
pygame.init()
|
|
|
|
# 定义颜色
|
|
BLACK = (0, 0, 0)
|
|
WHITE = (255, 255, 255)
|
|
YELLOW = (255, 255, 0)
|
|
|
|
# 设置窗口大小和标题
|
|
size = (700, 500)
|
|
screen = pygame.display.set_mode(size)
|
|
pygame.display.set_caption("Pygame光波")
|
|
|
|
# 循环标志
|
|
done = False
|
|
|
|
# 时钟对象
|
|
clock = pygame.time.Clock()
|
|
|
|
# 圆心位置列表和半径列表
|
|
circles = []
|
|
|
|
# 游戏循环
|
|
while not done:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
done = True
|
|
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
|
|
# 当左键被按下时,将圆心位置和半径保存到列表中
|
|
circle_pos = event.pos
|
|
circle_radius = 1
|
|
circles.append((circle_pos, circle_radius))
|
|
|
|
# 填充背景色
|
|
screen.fill(BLACK)
|
|
|
|
# 遍历圆心位置列表,绘制每一个光波
|
|
for pos, radius in circles:
|
|
pygame.draw.circle(screen, YELLOW, pos, radius, 5)
|
|
radius += 5
|
|
|
|
# 如果圆的半径已经大于一定值,从列表中删除该光波
|
|
if radius > 200:
|
|
circles.remove((pos, radius))
|
|
|
|
# 更新圆的半径
|
|
else:
|
|
circles[circles.index((pos, radius))] = (pos, radius)
|
|
|
|
# 更新屏幕
|
|
pygame.display.flip()
|
|
|
|
# 控制帧率
|
|
clock.tick(60)
|
|
|
|
# 退出游戏
|
|
pygame.quit() |