Python制作粒子烟花,提前开始跨年?

前言

跨年倒计时20天?我已经开始整烟花了,虽然不是很好看吧,但是也能将就看看 😥

这个的背景图,音乐,还有文字都是可以自己修改的哦

效果展示

Python制作粒子烟花,提前开始跨年?

; 导入库

import random
import pygame as py
import tkinter as tk
from time import time, sleep
from tkinter import filedialog
from PIL import Image, ImageTk
from math import sin, cos, radians
from random import choice, uniform, randint

实现代码

生成随机颜色

源码.点击领取即可


def randomcolor():

    colArr = ['1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
    color = ""
    for i in range(6):
        color += colArr[random.randint(0,14)]
    return "#"+color

GRAVITY = 0.06

colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen','indigo', 'cornflowerblue', 'pink']

属性

Generic class for particles
particles are emitted almost randomly on the sky, forming a round of circle (a star) before falling and getting removed
from canvas
Attributes(属性):
    - id: 粒子的id
    - x, y: 粒子的坐标
    - vx, vy: 粒子在对应坐标的变化速度
    - total:一颗烟花里的粒子总数
    - age: 粒子在画布上停留的时间
    - color: 自我移植
    - cv: 画布
    - lifespan: 粒子在画布上停留的时间

粒子运动的速度

这个里面的新年快乐是可以自己更改的哦

python学习交流Q群:309488165
class part:

    def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx = 0., vy = 0., size=3., color = 'red', lifespan = 2, **kwargs):
        self.id = idx

        self.x = x

        self.y = y

        self.initial_speed = explosion_speed

        self.vx = vx

        self.vy = vy

        self.total = total

        self.age = 0

        self.color = color

        self.cv = cv

        self.cid = self.cv.create_oval(x - size, y - size, x + size,y + size, fill=self.color, outline='white',width=0.01)

        self.lifespan = lifespan

xy轴移动位移

如果粒子仅存活不扩张(只是停留时间足够,说明膨胀到最大了),则自由坠落

        elif self.alive():
            columnFont = ('华文行楷',14)
            self.cv.create_text(250, 100, text='新',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(300, 100,  text='年',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(350, 100, text='快',tag="write_tag", fill=choice(colors),font = columnFont)
            self.cv.create_text(400, 100,  text='乐',tag="write_tag", fill=choice(colors),font = columnFont)

            move_x = cos(radians(self.id*360/self.total))

            self.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)
            self.vy += GRAVITY*dt

膨胀效果时间帧

判断膨胀时间是否小于1.2秒

    def expand (self):

        return self.age  1.2

判断粒子是否仍在生命周期内

判断已停留时间是否小于应该停留时间

    def alive(self):

        return self.age  self.lifespan

剩下代码

不想一步步写出来咯,有点麻烦哈哈

代码后面都有注释哈

源码.点击领取即可

def simulate(cv):

python学习交流Q群:309488165
    t = time()

    explode_points = []

    wait_time = randint(10,100)

    numb_explode = randint(8,20)

    for point in range(numb_explode):

        if point4:
            objects = []

            x_cordi = 250 + point*50

            y_cordi = 100

            speed = uniform (0.5, 2.5)

            size = uniform (0.5,3)

            color = choice(colors)

            explosion_speed = uniform(0.6, 3)

            total_particles = randint(10,60)

            for i in range(1,total_particles):

                r = part(cv, idx = i, total = total_particles, explosion_speed = explosion_speed, x = x_cordi, y = y_cordi, vx = speed, vy = speed, color=color, size = size, lifespan = uniform(0.6,1.75))

                objects.append(r)

            explode_points.append(objects)

        else:
            objects = []

            x_cordi = randint(50,550)

            y_cordi = randint(50, 250)

            speed = uniform (0.5, 2.5)

            size = uniform (0.5,3)

            color = choice(colors)

            explosion_speed = uniform(0.3, 2)

            total_particles = randint(10,50)

            for i in range(1,total_particles):

                r = part(cv, idx = i, total = total_particles, explosion_speed = explosion_speed, x = x_cordi, y = y_cordi, vx = speed, vy = speed, color=color, size = size, lifespan = uniform(0.6,1.75))

                objects.append(r)

            explode_points.append(objects)

    total_time = .0

    while total_time < 2:

        sleep(0.03)

        tnew = time()

        t, dt = tnew, tnew - t

        for point in explode_points:

            for item in point:

                item.update(dt)

        cv.update()

        total_time += dt

    root.after(wait_time, simulate, cv)

def close(*ignore):

    """Stops simulation loop and closes the window."""
    global root
    root.quit()

if __name__ == '__main__':
    root = tk.Tk()
    root.title('新年快乐~~')
    cv = tk.Canvas(root, height=600, width=600)

    bgpath = filedialog.askopenfilename(title='请选择背景图片')

    image = Image.open(bgpath)

    image = image.resize((600,600), Image.ANTIALIAS)

    photo = ImageTk.PhotoImage(image)
    cv.create_image(0, 0, image=photo, anchor='nw')

    bgmusic = filedialog.askopenfilename(title='请选择背景音乐')
    py.mixer.init()

    py.mixer.music.load(bgmusic)

    py.mixer.music.play(-1, 0, fade_ms=50)

    py.mixer.music.pause()

    py.mixer.music.unpause()

    cv.pack()

    root.protocol("WM_DELETE_WINDOW", close)
    root.after(200, simulate, cv)

    root.mainloop()

最后

今天的分享到这里就结束了

顺便给大家推荐一些Python视频教程,希望对大家有所帮助:

Python零基础教学合集

对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/

Original: https://blog.csdn.net/yxczsz/article/details/128254221
Author: kaKA-小圆
Title: Python制作粒子烟花,提前开始跨年?

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/769011/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球