Python实现新春烟花(渐变效果)

过年了~,给大伙放个电子烟花庆祝一下,祝大家新春快乐!

来年:蛇行千里步步高,财聚八方滚滚来!

import pygame
import random
import math
import sys
from pygame.locals import *

# 初始化
pygame.init()
pygame.mixer.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Python新春烟花")
clock = pygame.time.Clock()

# 渐变色生成函数
def generate_gradient(start_color, end_color, steps):
    return [
        (
            start_color[0] + (end_color[0] - start_color[0]) * i // steps,
            start_color[1] + (end_color[1] - start_color[1]) * i // steps,
            start_color[2] + (end_color[2] - start_color[2]) * i // steps
        ) for i in range(steps)
    ]

# 粒子类(带颜色渐变)
class Particle:
    def __init__(self, x, y, color, is_heart=False):
        self.x = x
        self.y = y
        self.base_color = color
        self.age = 0
        self.max_age = random.randint(30, 60)
        self.angle = random.uniform(0, 2 * math.pi)
        self.speed = random.uniform(1, 5)
        self.is_heart = is_heart  # 是否心形轨迹

        # 生成渐变色序列
        self.gradient = generate_gradient(color, (50, 50, 50), self.max_age)

    def update(self):
        self.age += 1
        if self.is_heart:
            # 心形轨迹方程
            t = self.age * 0.1
            self.x += 10 * (16 * math.sin(t) ** 3)
            self.y -= 10 * (13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t))
        else:
            # 普通粒子运动
            self.x += self.speed * math.cos(self.angle)
            self.y += self.speed * math.sin(self.angle)
            self.speed *= 0.95  # 空气阻力

    def get_color(self):
        return self.gradient[min(self.age, len(self.gradient) - 1)]

    def is_dead(self):
        return self.age >= self.max_age

# 烟花类
class Firework:
    def __init__(self, is_heart=False):
        self.x = random.randint(100, WIDTH - 100)
        self.y = HEIGHT
        self.color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))
        self.particles = []
        self.exploded = False
        self.is_heart = is_heart  # 是否心形烟花

    def explode(self):
        for _ in range(100 if not self.is_heart else 200):
            self.particles.append(Particle(self.x, self.y, self.color, self.is_heart))

    def update(self):
        if not self.exploded:
            self.y -= 5
            if self.y < HEIGHT // 2:
                self.explode()
                self.exploded = True
        else:
            for p in self.particles[:]:
                p.update()
                if p.is_dead():
                    self.particles.remove(p)

    def draw(self):
        if not self.exploded:
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), 5)
        for p in self.particles:
            color = p.get_color()
            size = 4 if not self.is_heart else 2
            pygame.draw.circle(screen, color, (int(p.x), int(p.y)), size)

# 主程序
fireworks = []
running = True

while running:
    screen.fill((0, 0, 30))  # 深蓝色背景

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

    # 随机生成新烟花(5%概率)
    if random.random() < 0.05:
        fireworks.append(Firework())  # 始终生成普通烟花

    # 更新并绘制所有烟花
    for fw in fireworks[:]:
        fw.update()
        fw.draw()
        if fw.exploded and not fw.particles:
            fireworks.remove(fw)

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

pygame.quit()

你可能感兴趣的:(pygame,python,开发语言)