跨年迎 2024 用Python实现例子烟花,可自选音乐

#!/usr/bin/env python
# -*- coding:utf-8 -*-

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

#win(窗口的w=宽,h=高)

WIN_W = 900
WIN_H = 600
# 定义时间、显示、频率

t1 = 0.10 #时间流速
show_n = 0
show_frequency = 0.0105 #烟花绽放频率,数值越大频率越高

color_list = [
    [255, 50, 50],
    [50, 255, 50],
    [50, 50, 255],
    [255, 255, 50],
    [255, 50, 255],
    [50, 255, 255],
    [255, 255, 255]
]

pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode((WIN_W, WIN_H),pygame.RESIZABLE, 32)
pygame.display.set_caption("五彩烟花大放送")


# 改成你自己
sound_wav = pygame.mixer.music.load("D:\桌面\有趣的Python\烟花源码\周懂\青花瓷.mp3")
pygame.mixer.music.play()

class Fireworks():
    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 again(self):
        self.is_show = True
        self.x = random.randint(WIN_W // 2 - 350, WIN_W // 2 + 350)
        self.y = random.randint(int(WIN_H / 2), int(WIN_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>WIN_H+p[1]:
                show_n -= 1
                self.is_show = False
                break
        self.vy += 10 * t1
        self.y += self.vy * t1


fk_list = []
fk_list.append(Fireworks(300, 300, -20, n=100, color=[0, 255, 0], v=10))
fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=11))
fk_list.append(Fireworks(300, 300, -20, n=200, color=[0, 0, 255], v=12))
fk_list.append(Fireworks(300, 300, -20, n=500, color=[0, 0, 255], v=12))
fk_list.append(Fireworks(300, 300, -20, n=600, color=[0, 0, 255], v=13))
fk_list.append(Fireworks(300, 300, -20, n=700, color=[255, 0, 0], v=15))
fk_list.append(Fireworks(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():
        #pygame这个不能少,否则退出还在循环
        if event.type == pygame.QUIT:
            exit()

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

    # 放烟花
    for i, fk in enumerate(fk_list):
        if not fk.is_show:
            fk.is_show = False
            if random.random() < show_frequency * (len(fk_list) - show_n):
                show_n += 1
                fk.again()
            continue
        fk.run()
        for p in fk.p_list:
            x, y = fk.x + p[1] * math.cos(p[0]), fk.y + p[1] * math.sin(p[0])
            if random.random() < 0.055:
                screen.set_at((int(x), int(y)),(255,255,255))
            else:
                screen.set_at((int(x), int(y)), (int(fk.color[0]), int(fk.color[1]), int(fk.color[2])))

    pygame.display.update()

    time_passed = clock.tick(50)

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 烟花代码是一种用Python编写的模拟烟花爆炸效果的程序。它通过绘制图形和模拟物理效果来实现烟花的动态效果。 以下是一个简单的Python烟花代码示例: ```python import random import turtle # 创建画布 canvas = turtle.Screen() canvas.bgcolor("black") # 创建烟花粒子类 class Particle(turtle.Turtle): def __init__(self, color): turtle.Turtle.__init__(self) self.shape("circle") self.color(color) self.penup() def launch(self): self.goto(0, -200) self.setheading(random.randint(0, 360)) self.speed(10) self.showturtle() def explode(self): self.hideturtle() for _ in range(20): self.color(random.choice(["red", "orange", "yellow", "green", "blue", "purple"])) self.goto(random.randint(-300, 300), random.randint(-300, 300)) self.showturtle() # 创建烟花管理类 class FireworkManager: def __init__(self): self.particles = [] def launch_firework(self): particle = Particle("white") particle.launch() self.particles.append(particle) def explode_firework(self): for particle in self.particles: particle.explode() # 创建烟花管理器对象 manager = FireworkManager() # 发射烟花 manager.launch_firework() # 等待一段时间后,烟花爆炸 turtle.delay(1000) manager.explode_firework() # 关闭画布 turtle.done() ``` 这段代码使用了Python的turtle库来绘制图形,并通过模拟粒子的运动和爆炸效果来实现烟花的动态效果。代码中定义了一个烟花粒子类Particle,以及一个烟花管理类FireworkManager。通过调用FireworkManager的launch_firework方法来发射烟花,然后等待一段时间后调用explode_firework方法使烟花爆炸。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值