用python写一个祝朋友生日快乐的烟花动画
时间: 2024-12-21 18:26:24 浏览: 13
你可以使用Python的pygame库来创建一个简单的烟花动画,庆祝朋友的生日。下面是一个简化的例子,它会随机生成一些烟花爆炸并闪烁:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 设置窗口大小和标题
win_size = (800, 600)
window = pygame.display.set_mode(win_size)
pygame.display.set_caption("Happy Birthday Fireworks")
# 烟花颜色和位置
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
positions = [(random.randint(0, win_size[0]), random.randint(0, win_size[1])) for _ in range(50)]
# 主循环
while True:
# 清空屏幕
window.fill((0, 0, 0))
# 遍历烟花
for i, pos in enumerate(positions):
size = random.randint(10, 50)
color = colors[random.randint(0, len(colors) - 1)]
# 绘制烟花
pygame.draw.circle(window, color, pos, size, 0)
# 渐变消失效果
positions[i] = (pos[0], pos[1] + random.randint(-2, 2)) # 爆炸上升或下降
if positions[i][1] > win_size[1]:
positions.pop(i) # 当烟花离开屏幕时删除
# 更新屏幕
pygame.display.update()
# 检查事件(如关闭窗口)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 延迟一段时间(比如每帧10毫秒)
pygame.time.delay(10)
# 结束游戏消息
print("Happy Birthday! Have a blast!")
阅读全文