Python烟花代码实现详解

编写一个烟花代码(Python)

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置屏幕尺寸
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Beautiful Fireworks")

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

# 烟花类
class Firework:
    def __init__(self):
        # 烟花发射位置
        self.x = random.randint(0, width)
        self.y = height
        # 烟花上升速度
        self.speed = random.randint(10, 20)
        # 烟花是否爆炸
        self.exploded = False
        # 生成更鲜艳的颜色,以亮红色系为例,可根据需求调整
        self.main_color = (random.randint(200, 255), random.randint(50, 150), random.randint(50, 150))
        # 爆炸后的粒子列表
        self.particles = []
        # 随机生成爆炸高度,范围在屏幕高度的 1/4 到 3/4 之间
        self.explode_height = random.randint(height // 4, 3 * height // 4)

    def launch(self):
        if not self.exploded:
            self.y -= self.speed
            # 当烟花上升到爆炸高度时爆炸
            if self.y < self.explode_height:
                self.explode()

    def explode(self):
        self.exploded = True
        # 增加粒子数量,让爆炸范围更大
        num_particles = random.randint(50, 100)
        for _ in range(num_particles):
            # 扩大粒子速度范围,进一步增大爆炸范围
            speed_x = random.uniform(-8, 8)
            speed_y = random.uniform(-8, 8)
            # 每个粒子的生命周期
            lifespan = random.randint(10, 30)
            # 为每个粒子微调颜色,增加颜色变化
            color = [
                min(255, max(0, c + random.randint(-20, 20))) for c in self.main_color
            ]
            self.particles.append([[self.x, self.y], [speed_x, speed_y], lifespan, tuple(color)])

    def update_particles(self):
        if self.exploded:
            for particle in self.particles[:]:
                particle[0][0] += particle[1][0]
                particle[0][1] += particle[1][1]
                particle[2] -= 1
                if particle[2] <= 0:
                    self.particles.remove(particle)
            if not self.particles:
                fireworks.remove(self)

    def draw(self):
        if not self.exploded:
            pygame.draw.circle(screen, self.main_color, (int(self.x), int(self.y)), 3)
        else:
            for particle in self.particles:
                alpha = int(255 * (particle[2] / 30))
                particle_color = (particle[3][0], particle[3][1], particle[3][2], alpha)
                surface = pygame.Surface((2, 2), pygame.SRCALPHA)
                surface.fill(particle_color)
                screen.blit(surface, (int(particle[0][0]), int(particle[0][1])))

# 烟花列表
fireworks = []

clock = pygame.time.Clock()

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

    # 提高创建烟花的概率,从 0.02 提高到 0.05
    if random.random() < 0.05:
        fireworks.append(Firework())

    screen.fill(BLACK)

    for firework in fireworks[:]:
        if not firework.exploded:
            firework.launch()
        else:
            firework.update_particles()
        firework.draw()

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

pygame.quit()

实现效果

作者:C_VuI

物联沃分享整理
物联沃-IOTWORD物联网 » Python烟花代码实现详解

发表回复