欢迎访问宙启技术站
智能推送

python烟花效果的代码实例

发布时间:2023-05-15 22:38:56

Python烟花效果的代码实例

烟花效果是一个非常炫酷的特效,可以为网页等多种场景增添动态的视觉效果。在此,我们将基于Python编写一个烟花效果的代码实例,让大家可以快速实现一个炫酷的烟花特效。

代码示例:

import pygame
from random import randint, uniform

# 初始化pygame
pygame.init()

# 定义窗口大小、背景颜色以及烟花最大半径
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
background = (50, 50, 50)
max_radius = 200

# 定义烟花类
class Firework():
    def __init__(self):
        # 烟花的初始位置、颜色和半径
        self.x = randint(0, width)
        self.y = randint(0, int(height/3*2))
        self.color = (randint(0, 255), randint(0, 255), randint(0, 255))
        self.radius = randint(5, 15)

        # 烟花的初始速度、加速度和角度
        self.speed = uniform(2, 10)
        self.acceleration = uniform(-0.2, 0.2)
        self.angle = uniform(0, 2 * 3.1415926)

        # 烟花是否爆炸
        self.exploded = False

        # 烟花粒子的列表
        self.particles = []

    def update(self):
        if not self.exploded:
            # 更新烟花的位置和速度
            self.x += self.speed * pygame.math.sin(self.angle)
            self.y -= self.speed * pygame.math.cos(self.angle)
            self.speed += self.acceleration
            if self.speed <= 0:
                self.exploded = True

        else:
            # 添加烟花粒子
            for i in range(30):
                particle = Particle(self.x, self.y, self.color)
                self.particles.append(particle)

            # 删除已经消失的烟花粒子
            for i in range(len(self.particles)-1, -1, -1):
                if self.particles[i].done:
                    self.particles.pop(i)

            # 检查是否结束烟花
            if len(self.particles) == 0:
                self.exploded = False

        # 绘制烟花或烟花粒子
        if not self.exploded:
            pygame.draw.circle(screen, self.color,
                               (int(self.x), int(self.y)), self.radius)

        else:
            for particle in self.particles:
                particle.update()
                particle.draw()

# 定义烟花粒子类
class Particle():
    def __init__(self, x, y, color):
        # 粒子的初始位置、颜色和半径
        self.x = x
        self.y = y
        self.color = color
        self.radius = randint(5, 10)

        # 粒子的初始速度和加速度
        self.speedx = uniform(-3, 3)
        self.speedy = uniform(-3, 3)
        self.acceleration = uniform(0.03, 0.05)

        # 粒子是否消失
        self.done = False

    def update(self):
        # 更新粒子的位置和速度
        self.x += self.speedx
        self.y += self.speedy
        self.speedy += self.acceleration

        # 判断粒子是否已经消失
        if self.radius <= 0:
            self.done = True
        else:
            self.radius -= 0.1

    def draw(self):
        pygame.draw.circle(screen, self.color,
                           (int(self.x), int(self.y)), int(self.radius))

# 主函数
def main():
    # 烟花的列表
    fireworks = []

    # 游戏主循环
    while True:
        # 处理游戏事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return

        # 绘制游戏背景
        screen.fill(background)

        # 添加新的烟花
        if randint(0, 100) == 0:
            firework = Firework()
            fireworks.append(firework)

        # 更新已有的烟花
        for firework in fireworks:
            firework.update()
            if firework.y <= max_radius:
                fireworks.remove(firework)

        # 更新屏幕
        pygame.display.update()

# 启动主函数
if __name__ == '__main__':
    main()

代码解析:

代码主要分为两个类:Firework和Particle。其中,Firework表示一个烟花,Particle表示一个烟花爆炸后生成的粒子。

在Firework类中,有一个update函数用于更新烟花的位置和状态(是否爆炸)。如果烟花已经爆炸,则会生成一定数量的粒子,并在之后每次更新时更新其位置和状态。如果所有粒子都消失了,则认为烟花已经结束,并将其状态设置为未爆炸。

在Particle类中,同样有一个update函数和一个draw函数。update函数用于更新粒子的位置和状态(是否消失),draw函数用于将粒子绘制在屏幕上。

在主函数中,通过一个循环来不断更新屏幕和烟花的状态。每隔一定时间会创建一个新的烟花,这些烟花会被添加到一个列表中。之后,每次循环会更新已有的烟花,并判断是否需要删除已经爆炸的烟花。最后,通过pygame.display.update()方法将更新后的屏幕绘制出来。

总结:

以上就是关于Python编写烟花效果的代码实例。通过本文的学习,相信大家已经了解了如何实现一个炫酷的烟花特效,希望今后可以运用到实际开发中。