基于Python实现烟花效果的示例代码
作者:m0_54850467 时间:2021-02-08 13:25:09
python烟花代码
如下
# -*- coding: utf-8 -*-
import math, random,time
import threading
import tkinter as tk
import re
#import uuid
Fireworks=[]
maxFireworks=8
height,width=600,600
class firework(object):
def __init__(self,color,speed,width,height):
#uid=uuid.uuid1()
self.radius=random.randint(2,4) #粒子半径为2~4像素
self.color=color #粒子颜色
self.speed=speed #speed是1.5-3.5秒
self.status=0 #在烟花未 * 的情况下,status=0; * 后,status>=1;当status>100时,烟花的生命期终止
self.nParticle=random.randint(20,30) #粒子数量
self.center=[random.randint(0,width-1),random.randint(0,height-1)] #烟花随机中心坐标
self.oneParticle=[] #原始粒子坐标(100%状态时)
self.rotTheta=random.uniform(0,2*math.pi) #椭圆平面旋转角
#椭圆参数方程:x=a*cos(theta),y=b*sin(theta)
#ellipsePara=[a,b]
self.ellipsePara=[random.randint(30,40),random.randint(20,30)]
theta=2*math.pi/self.nParticle
for i in range(self.nParticle):
t=random.uniform(-1.0/16,1.0/16) #产生一个 [-1/16,1/16) 的随机数
x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t) #椭圆参数方程
xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta), y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta) #平面旋转方程
self.oneParticle.append([xx,yy])
self.curParticle=self.oneParticle[0:] #当前粒子坐标
self.thread=threading.Thread(target=self.extend) #建立线程对象
def extend(self): #粒子群状态变化函数线程
for i in range(100):
self.status+=1 #更新状态标识
self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle] #更新粒子群坐标
time.sleep(self.speed/50)
def explode(self):
self.thread.setDaemon(True) #把现程设为守护线程
self.thread.start() #启动线程
def __repr__(self):
return ('color:{color}
'
'speed:{speed}
'
'number of particle: {np}
'
'center:[{cx} , {cy}]
'
'ellipse:a={ea} , b={eb}
'
'particle:
{p}
'
).format(color=self.color,speed=self.speed,np=self.nParticle,cx=self.center[0],cy=self.center[1],p=str(self.oneParticle),ea=self.ellipsePara[0],eb=self.ellipsePara[1])
def colorChange(fire):
rgb=re.findall(r'(.{2})',fire.color[1:])
cs=fire.status
f=lambda x,c: hex(int(int(x,16)*(100-c)/30))[2:] #当粒子寿命到70%时,颜色开始线性衰减
if cs>70:
ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs)
else:
ccr,ccg,ccb=rgb[0],rgb[1],rgb[2]
return '#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb)
def appendFirework(n=1): #递归生成烟花对象
if n>maxFireworks or len(Fireworks)>maxFireworks:
pass
elif n==1:
cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:]) # 产生一个0~16777215(0xFFFFFF)的随机数,作为随机颜色
a=firework(cl,random.uniform(1.5,3.5),width,height)
Fireworks.append( {'particle':a,'points':[]} ) #建立粒子显示列表,‘particle'为一个烟花对象,‘points'为每一个粒子显示时的对象变量集
a.explode()
else:
appendFirework()
appendFirework(n-1)
def show(c):
for p in Fireworks: #每次刷新显示,先把已有的所以粒子全部删除
for pp in p['points']:
c.delete(pp)
for p in Fireworks: #根据每个烟花对象,计算其中每个粒子的显示对象
oneP=p['particle']
if oneP.status==100: #状态标识为100,说明烟花寿命结束
Fireworks.remove(p) #移出当前烟花
appendFirework() #新增一个烟花
continue
else:
li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle] #把中心为原点的椭圆平移到随机圆心坐标上
color=colorChange(oneP) #根据烟花当前状态计算当前颜色
for pp in li:
p['points'].append(c.create_oval(pp[0]-oneP.radius, pp[1]-oneP.radius, pp[0]+oneP.radius, pp[1]+oneP.radius, fill=color)) #绘制烟花每个粒子
root.after(50, show,c) #回调,每50ms刷新一次
if __name__=='__main__':
appendFirework(maxFireworks)
root = tk.Tk()
cv = tk.Canvas(root, height=height, width=width)
cv.create_rectangle(0, 0, width, height, fill="black")
cv.pack()
root.after(50, show,cv)
root.mainloop()
图片展示
来源:https://blog.csdn.net/m0_54850467/article/details/125241220
标签:Python,烟花
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python学习之加密模块使用详解
2022-06-01 03:58:03
![](https://img.aspxhome.com/file/2023/4/102734_0s.jpg)
超详细的Python安装第三方库常用方法汇总
2023-08-26 08:54:47
![](https://img.aspxhome.com/file/2023/7/91917_0s.png)
Python使用socket实现组播与发送二进制数据
2021-05-09 13:44:28
![](https://img.aspxhome.com/file/2023/9/65949_0s.png)
python添加模块搜索路径方法
2023-11-20 09:29:45
实例讲解python中的协程
2022-08-10 20:10:38
Python中的作用域规则详解
2022-08-12 23:54:04
python logging日志打印过程解析
2023-11-03 13:04:09
![](https://img.aspxhome.com/file/2023/8/64308_0s.png)
Python编解码问题及文本文件处理方法详解
2021-04-13 07:52:06
![](https://img.aspxhome.com/file/2023/9/72999_0s.png)
如何判断用户在某一页面逗留了多长时间?
2010-01-18 20:48:00
变态输入框——再谈校验包容性(一)
2009-10-10 13:23:00
![](https://img.aspxhome.com/file/UploadPic/200910/10/1249f1d8f61g215-51s.jpg)
Python循环实现n的全排列功能
2022-08-15 21:04:08
SQL Server 中导入导出数据三方法比较
2009-01-21 14:22:00
SQL Server转换为XQuery及反向转换
2009-01-20 13:32:00
composer.lock文件的作用
2023-06-06 13:05:24
js字符串日期yyyy-MM-dd转化为date示例代码
2023-08-06 16:35:58
Python爬虫爬取电影票房数据及图表展示操作示例
2021-07-18 08:34:32
![](https://img.aspxhome.com/file/2023/9/103599_0s.png)
JS表格排序新法
2010-07-09 06:40:13
python中必要的名词解释
2023-10-17 23:30:32
Python异常处理机制结构实例解析
2021-11-30 15:59:27
![](https://img.aspxhome.com/file/2023/7/90287_0s.png)
SQL Server 2005 Express版企业管理器下载
2009-10-06 14:54:00