import pygame, math, time, random, os
from sys import exit

WINDOW_W = 1600
WINDOW_H = 900
one_time = 0.18
show_n = 0
show_frequency = 0.015
color_list = [
    [241, 2, 15],
    [213, 52, 213],
    [107, 9, 204],
    [2, 28, 200],
    [19, 4, 71],
    [244, 244, 242],
    [255, 80, 27],
    [253, 2, 82],
    [251, 66, 154],
    [254, 178, 0]
]

pygame.init()
pygame.mixer.init()
os.environ['SDL_VIDEO_WINDOW_POS'] = '%d,%d' % (500, 230)

screen = pygame.display.set_mode((WINDOW_W, WINDOW_H), pygame.DOUBLEBUF, 32)
pygame.display.set_caption('祝你新年快快乐乐')

sound_wav = pygame.mixer.music.load('firework.mp3')
pygame.mixer.music.play()

class Yanhua():
    is_show = False
    x, y = 0, 0
    vy = 0
    p_list = []
    color = [0, 0, 0]
    v = 0

    def __init__(self, x, y, vy, n=300, color=[0, 255, 0], v=10):
        self.x = x
        self.y = y
        self.vy = vy
        self.color = color
        self.v = v
        for i in range(n):
            self.p_list.append([random.random() * 2 * math.pi, 0, v * math.pow(random.random(), 1 / 3)])

    def chongzhi(self):
        self.is_show = True
        self.x = random.randint(WINDOW_W // 2 - 350, WINDOW_W // 2 + 350)
        self.y = random.randint(int(WINDOW_H / 2), int(WINDOW_H * 3 / 5))
        self.vy = -40 * (random.random() * 0.4 + 0.8) - self.vy * 0.2
        self.color = color_list[random.randint(0, len(color_list) - 1)].copy()
        n = len(self.p_list)
        self.p_list = []
        for i in range(n):
            self.p_list.append([random.random() * 2 * math.pi, 0, self.v * math.pow(random.random(), 1 / 3)])

    def run(self):
        global show_n
        for p in self.p_list:
            p[1] = p[1] + (random.random() * 0.6 + 0.7) * p[2]
            p[2] = p[2] * 0.97
            if p[2] < 1.2:
                self.color[0] *= 0.9999
                self.color[1] *= 0.9999
                self.color[2] *= 0.9999

            if max(self.color) < 10 or self.y > WINDOW_H + p[1]:
                show_n -= 1
                self.is_show = False
                break
        self.vy += 10 * one_time
        self.y += self.vy * one_time

yh_list = []
yh_list.append(Yanhua(300, 300, -20, n=100, color=[0, 255, 0], v=10))
yh_list.append(Yanhua(300, 300, -20, n=200, color=[0, 0, 255], v=11))
yh_list.append(Yanhua(300, 300, -20, n=200, color=[0, 0, 255], v=12))
yh_list.append(Yanhua(300, 300, -20, n=500, color=[0, 0, 255], v=12))
yh_list.append(Yanhua(300, 300, -20, n=600, color=[0, 0, 255], v=13))
yh_list.append(Yanhua(300, 300, -20, n=700, color=[255, 0, 0], v=15))
yh_list.append(Yanhua(300, 300, -20, n=800, color=[255, 255, 0], v=18))

clock = pygame.time.Clock()

while True:
    if not pygame.mixer.music.get_busy():
        pygame.mixer.music.play()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

    screen.fill((0, 0, 0))

    for i, yh in enumerate(yh_list):
        if not yh.is_show:
            yh.is_show = False
            if random.random() < show_frequency * (len(yh_list) - show_n):
                show_n += 1
                yh.chongzhi()
            continue

    pygame.display.update()
    clock.tick(60)
Python Pygame 实现炫酷新年烟花特效

原文地址: http://www.cveoy.top/t/topic/n4j 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录