python烟花代码
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.speed=speed #speed是1.5-3.5秒
self.status=0 #在烟花未爆炸的情况下,status=0;爆炸后,status>=1;当status>100时,烟花的⽣命期终⽌
self.nParticle=random.randint(20,30) #粒⼦数量
<=[random.randint(0,width-1),random.randint(0,height-1)] #烟花随机中⼼坐标
#椭圆参数⽅程: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]*s(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t) #椭圆参数⽅程
xx,yy=Theta)-y*math.Theta), Theta)+x*math.Theta) #平⾯旋转⽅程
self.Particle[0:] #当前粒⼦坐标
random pythonself.thread=threading.Thread(d) #建⽴线程对象
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 Particle] #更新粒⼦坐标
time.sleep(self.speed/50)
def explode(self):
self.thread.setDaemon(True) #把现程设为守护线程
self.thread.start() #启动线程
def __repr__(self):
return('color:{color}\n'
'speed:{speed}\n'
'number of particle: {np}\n'
'center:[{cx} , {cy}]\n'
'ellipse:a={ea} , b={eb}\n'
'particle:\n{p}\n'
).format(lor,speed=self.speed,np=self.nParticle,[0],[1],p=Particle),ea=self.ellipsePara[0],eb=s elf.ellipsePara[1])
def colorChange(fire):
rgb=re.findall(r'(.{2})',lor[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’为每⼀个粒⼦显⽰时的对象变量集
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,说明烟花寿命结束
appendFirework() #新增⼀个烟花
continue
else:
li=[[int(cp[0]*2)+[0],int(cp[1]*2)+[1]]for cp in oneP.curParticle] #把中⼼为原点的椭圆平移到随机圆⼼坐标上
color=colorChange(oneP) #根据烟花当前状态计算当前颜⾊
for pp in li:
p['points'].ate_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.pack()
root.after(50, show,cv)
root.mainloop()
图⽚展⽰
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论