python烟花代码如下#-*-coding:utf-8-*-importmath,random,timeimportthreadingimporttkinterastkimportre#importuuidFireworks=[]maxFireworks=8height,width=600,600classfirework(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.nParticleforiinrange(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)#创建线程对象defextend(self):#粒子群状态变化函数threadforiinrange(100):self.status+=1#更新状态标识self.curParticle=[[one[0]*self.status/100,one[1]*self.status/100]foroneinself.oneParticle]#更新粒子群坐标time.sleep(self.speed/50)defexplode(self):self.thread.setDaemon(True)#设置当前进程为守护线程self.thread.start()#启动线程def__repr__(self):return('color:{color}\n''speed:{速度}\n''粒子数:{np}\n''中心:[{cx},{cy}]\n''椭圆:a={ea},b={eb}\n''粒子:\n{p}\n').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])defcolorChange(火):rgb=re.findall(r'(.{2})',fire.color[1:])cs=fire.statusf=lambdax,c:hex(int(int(x,16)*(100-c)/30))[2:]#当粒子寿命达到70%时,颜色开始变如果cs>70,则线性衰减:ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs)否则:ccr,ccg,ccb=rgb[0],rgb[1],rgb[2]返回'#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb)defappendFirework(n=1):#递归生成烟花对象ifn>maxFireworksorlen(Fireworks)>maxFireworks:passelifn==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)defshow(c):forpinFireworks:#每次刷新显示,先删除所有存在的粒子forppinp['points']:c.delete(pp)forpinFireworks:#根据每个烟花对象,计算每个粒子的显示对象oneP=p['particle']ifoneP.status==100:#Thestatusflag为100,表示烟花生命结束Fireworks.remove(p)#移除当前烟花appendFirework()#添加一个烟花continueelse:li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]]forcpinoneP.curParticle]#将以中心为原点的椭圆平移到随机中心坐标color=colorChange(oneP)#根据烟花当前状态State计算当前颜色rforppinli: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()快过年了,学这个出发给女朋友的烟花。
