阅读量:0
要使用Python制作烟花特效,可以使用Pygame库来进行图形绘制和动画效果的实现。以下是一个简单的例子代码,用于绘制烟花特效:
import pygame import random # 初始化pygame pygame.init() # 设置窗口大小 width, height = 800, 600 screen = pygame.display.set_mode((width, height)) # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) # 烟花粒子类 class Particle: def __init__(self, x, y, size): self.x = x self.y = y self.size = size self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) self.speed = random.randint(1, 5) def move(self): self.y -= self.speed def draw(self): pygame.draw.circle(screen, self.color, (self.x, self.y), self.size) # 创建烟花粒子 def create_particles(x, y): particle_count = 20 for _ in range(particle_count): particle = Particle(x, y, random.randint(2, 5)) particles.append(particle) # 主循环 running = True particles = [] clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: mx, my = pygame.mouse.get_pos() create_particles(mx, my) # 清空屏幕 screen.fill(BLACK) # 更新和绘制烟花粒子 for particle in particles: particle.move() particle.draw() if particle.y <= 0: particles.remove(particle) # 刷新屏幕 pygame.display.flip() clock.tick(60) # 退出程序 pygame.quit()
运行上述代码,你将会看到一个简单的窗口,在鼠标点击的位置产生烟花特效。你可以通过调整参数来改变特效的效果,例如改变粒子数量、速度、颜色等。