如何使用Python制作新年烟花效果:完整教程与代码示例
                           
天天向上
发布: 2025-01-12 10:34:44

原创
690 人浏览过

使用Python制作新年烟花效果可以是一个非常有趣的项目,特别适合展示节日气氛。我们可以使用 pygame 库来实现这一效果。下面是完整的教程与代码示例,帮助你轻松创建新年烟花效果。

环境准备

首先,确保你已经安装了 pygame 库。如果没有安装,可以使用以下命令安装:

pip install pygame

代码示例

import pygame
import random
import math

# 初始化pygame
pygame.init()

# 设置窗口大小
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("新年烟花效果")

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 烟花类
class Firework:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.particles = []
        self.num_particles = 100
        self.exploded = False
        self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
        self.angle = random.uniform(0, 2 * math.pi)
        self.speed = random.uniform(2, 6)
        self.velocity = (math.cos(self.angle) * self.speed, math.sin(self.angle) * self.speed)

    def update(self):
        if not self.exploded:
            self.x += self.velocity[0]
            self.y += self.velocity[1]
            if random.random() < 0.05:
                self.exploded = True
                for _ in range(self.num_particles):
                    self.particles.append(Particle(self.x, self.y, self.color))
        else:
            for particle in self.particles:
                particle.update()

    def draw(self):
        if not self.exploded:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 3)
        else:
            for particle in self.particles:
                particle.draw()

# 烟花粒子类
class Particle:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.lifetime = random.randint(30, 50)
        self.size = random.randint(2, 4)
        angle = random.uniform(0, 2 * math.pi)
        speed = random.uniform(1, 3)
        self.velocity = (math.cos(angle) * speed, math.sin(angle) * speed)

    def update(self):
        self.x += self.velocity[0]
        self.y += self.velocity[1]
        self.lifetime -= 1

    def draw(self):
        if self.lifetime > 0:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)

# 主函数
def main():
    clock = pygame.time.Clock()
    fireworks = []
    running = True

    while running:
        screen.fill(BLACK)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        if random.random() < 0.05:
            fireworks.append(Firework(random.randint(100, WIDTH - 100), HEIGHT))

        for firework in fireworks:
            firework.update()
            firework.draw()

        pygame.display.flip()
        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    main()

代码解释

  1. 初始化设置
  • 使用 pygame 来初始化窗口和设置显示模式,窗口大小为 800×600 像素。
  1. 烟花类 (Firework)
  • 每个烟花的启动位置、颜色、速度和方向是随机的。
  • 烟花通过 particles 属性生成多个粒子模拟爆炸效果。
  1. 粒子类 (Particle)
  • 粒子通过随机角度和速度发射,模拟烟花的扩散效果。
  • 每个粒子有一定的生命周期,当生命周期结束时,粒子会消失。
  1. 主程序
  • 程序循环中随机生成烟花,并模拟烟花升空与爆炸过程。
  • 每秒钟会更新并绘制所有的烟花和粒子效果。

运行效果

当运行该程序时,你将看到一个黑色背景下,偶尔有烟花升空并爆炸,散发出五光十色的粒子,模拟新年的烟花效果。

小结

通过这个项目,你不仅可以掌握如何使用 pygame 创建动画效果,还能了解如何使用物理学原理模拟烟花的运动与爆炸。如果你希望进行更复杂的烟花效果,可以进一步改进粒子的运动轨迹、生命周期或加入音效。

发表回复 0

Your email address will not be published. Required fields are marked *