好的,这里是一个示例代码:
import pygame
import random
# 初始化pygame
pygame.init()
# 设置屏幕的大小
screen_width, screen_height = 800, 600
screen = pygame.display.set_mode((screen_width, screen_height))
# 设置窗口标题
pygame.display.set_caption("烟花")
# 设置烟花颜色,这里使用了一些随机颜色
colors = [(255, 0, 0), (255, 165, 0), (255, 255, 0), (0, 255, 0), (0, 0, 255), (75, 0, 130), (238, 130, 238)]
# 定义烟花类
class Firework:
def __init__(self):
# 随机设置烟花的起始位置
self.x = random.randint(0, screen_width)
self.y = screen_height
# 随机设置烟花颜色
self.color = random.choice(colors)
# 随机设置烟花的速度
self.speed = random.randint(5, 10)
# 画出烟花
def draw(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 5)
# 烟花上升
def move(self):
self.y -= self.speed
# 存储所有的烟花
fireworks = []
# 循环播放烟花
while True:
# 每次循环时都重新绘制屏幕
screen.fill((0, 0, 0))
# 随机生成新的烟花
if random.randint(0, 50) == 0:
fireworks.append(Firework())
# 画出所有的烟花
for f in fireworks:
f.draw()
f.move()
# 更新屏幕
pygame.display.flip()
# 处理退出事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
这段代码使用了pygame库来实现满屏放烟花的