首页 > 数据库 > 数据库开发技术 > 春节炫酷烟花秀璀璨绽放

,正值阖家团圆、万家灯火辉煌之际,阿黎逸阳带着烟花秀祝大家新年快乐!牛年大吉
!











实现本文效果的整体思路是:加载库—选择背景图片和音乐—设置动态效果。

要想实现本文的烟花秀,首先要先在python中加载以下库。
# -*- coding: UTF-8 -*-'''代码用途 :庆祝新年作者 :阿黎逸阳博客 : https://blog.csdn.net/qq_32532663/article/details/106176609'''import randomimport pygame as pyimport tkinter as tkfrom time import time, sleepfrom tkinter import filedialogfrom PIL import Image, ImageTkfrom math import sin, cos, radiansfrom random import choice, uniform, randint#导入库

if __name__ == '__main__':root = tk.Tk()root.title('祝大家—牛年大吉') # 设置窗体的标题栏cv = tk.Canvas(root, height=600, width=600)#绘制一个高600,宽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)# 播放 第一个是播放值 -1代表循环播放, 第二个参数代表开始播放的时间py.mixer.music.pause()#暂停py.mixer.music.unpause()#取消暂停cv.pack()#把cv添加进去root.protocol("WM_DELETE_WINDOW", close)root.after(200, simulate, cv)#在0.1秒后再调用stimulate函数,生成一轮烟花绽放效果root.mainloop()#执行root,生成窗口

同时停留阶段在屏幕上绘制想表达的文字(可自行更改)。
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 "#"+colorGRAVITY = 0.06#重力变量colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen','indigo', 'cornflowerblue', 'pink']#颜色列表'''Generic class for particlesparticles are emitted almost randomly on the sky, forming a round of circle (a star) before falling and getting removedfrom canvasAttributes(属性):- id: 粒子的id- x, y: 粒子的坐标- vx, vy: 粒子在对应坐标的变化速度- total:一颗烟花里的粒子总数- age: 粒子在画布上停留的时间- color: 自我移植- cv: 画布- lifespan: 粒子在画布上停留的时间'''class part:#为每一个烟花绽放出来的粒子单独构建一个类的对象 ,每个粒子都会有一些重要的属性,决定它的外观(大小、颜色)、移动速度等def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx = 0., vy = 0., size=2., color = 'red', lifespan = 2, **kwargs):self.id = idx#每个烟花的特定标识符self.x = x#烟花绽放x轴self.y = y#烟花绽放y轴self.initial_speed = explosion_speed#粒子初始速度self.vx = vx#粒子运动x轴速度self.vy = vy#粒子运动y轴速度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)#指定一个限定矩形(Tkinter 会自动在这个矩形内绘制一个椭圆)self.lifespan = lifespan#粒子在画布上停留的时间def update(self, dt):self.age += dt#更新粒子停留时间if self.alive() and self.expand():#如果粒子既存活又处于扩张阶段move_x = cos(radians(self.id*360/self.total))*self.initial_speed#粒子x轴继续膨胀move_y = sin(radians(self.id*360/self.total))*self.initial_speed#粒子y轴继续膨胀self.cv.move(self.cid, move_x, move_y)#根据id把画布上的粒子移动x和y个距离self.vx = move_x/(float(dt)*1000)#粒子x轴的速度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))#x轴的移动位移# we technically don't need to update x, y because move will do the jobself.cv.move(self.cid, self.vx + move_x, self.vy+GRAVITY*dt)self.vy += GRAVITY*dt#更新y轴elif self.cid is not None:#如果粒子生命周期已过,则将其移除cv.delete(self.cid)#在画布上移除该粒子对象self.cv.delete("write_tag")#同时移除字体self.cid = Nonedef expand (self):#定义膨胀效果时间帧return self.age <= 1.2#判断膨胀时间是否小于1.2秒def alive(self):#判断粒子是否仍在生命周期内return self.age <= self.lifespan#判断已停留时间是否小于应该停留时间'''Firework simulation loop:Recursively call to repeatedly emit new fireworks on canvasa list of list (list of stars, each of which is a list of particles)is created and drawn on canvas at every call,via update protocol inside each 'part' object'''def simulate(cv):t = time()#返回自1970年后经过的浮点秒数,精确到小数点后7位explode_points = []#爆炸点列表,烟花列表wait_time = randint(10,100)#等待时间为10到100之间整数numb_explode = randint(8,20)#爆炸烟花个数时6到10之间的随机整数# create list of list of all particles in all simultaneous explosionfor point in range(numb_explode):#为所有模拟烟花绽放的全部粒子创建一列列表if point<=4:objects = []#每个点的爆炸粒子列表粒子列表x_cordi = 250 + point*50#每个爆炸点的x轴y_cordi = 100#每个爆炸点的y轴speed = uniform (0.5, 1.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))#把上述参数代入part函数,但是每个粒子的生存时间是自己独立的objects.append(r)#把r添加进粒子列表explode_points.append(objects)#把粒子列表添加进烟花列表else:objects = []#每个点的爆炸粒子列表粒子列表x_cordi = randint(50,550)#每个爆炸点的x轴y_cordi = randint(50, 150)#每个爆炸点的y轴speed = uniform (0.5, 1.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))#把上述参数代入part函数,但是每个粒子的生存时间是自己独立的objects.append(r)#把r添加进粒子列表explode_points.append(objects)#把粒子列表添加进烟花列表total_time = .0#初始化总时间# keeps undate within a timeframe of 1.8 secondwhile total_time < 2:#当总时间小于1.8秒时运行该循环sleep(0.03)#让画面暂停0.01秒tnew = time()#刷新时间t, dt = tnew, tnew - t#时间等于新时间,和上次时间间隔为tnew-tfor point in explode_points:#遍历烟花列表for item in point:#遍历烟花里的粒子列表item.update(dt)#粒子更新时间cv.update()#刷新画布total_time += dt#为while循环增加时间root.after(wait_time, simulate, cv)#将组件置于其他组件之后,放在最顶层,覆盖下面的,递归调用自己,形成新一轮的爆炸def close(*ignore):#打开模拟循环并关闭窗口"""Stops simulation loop and closes the window."""global rootroot.quit()
注:如想获得本文全量可执行的代码和背景图片,可在公众号中回复”烟花“,即可免费获取。
至此,在Python中绘制烟花图已全部讲解完毕,感兴趣的同学可以自己实现一遍
。


扫一扫关注我
19967879837
投稿微信号、手机
原文作者:阿黎逸阳的代码;如有侵权,请联系管理员删除。