[Python] python烟花代码

 
4659 7
小明Python 2022-10-15 13:41:57 | 显示全部楼层 |阅读模式
python烟花代码


如下
  1. # -*- coding: utf-8 -*-

  2. import math, random,time
  3. import threading
  4. import tkinter as tk
  5. import re
  6. #import uuid

  7. Fireworks=[]
  8. maxFireworks=8
  9. height,width=600,600

  10. class firework(object):
  11.     def __init__(self,color,speed,width,height):
  12.         #uid=uuid.uuid1()
  13.         self.radius=random.randint(2,4)  #粒子半径为2~4像素
  14.         self.color=color   #粒子颜色
  15.         self.speed=speed  #speed是1.5-3.5秒
  16.         self.status=0   #在烟花未爆炸的情况下,status=0;爆炸后,status>=1;当status>100时,烟花的生命期终止
  17.         self.nParticle=random.randint(20,30)  #粒子数量
  18.         self.center=[random.randint(0,width-1),random.randint(0,height-1)]   #烟花随机中心坐标
  19.         self.oneParticle=[]    #原始粒子坐标(100%状态时)
  20.         self.rotTheta=random.uniform(0,2*math.pi)  #椭圆平面旋转角

  21.         #椭圆参数方程:x=a*cos(theta),y=b*sin(theta)
  22.         #ellipsePara=[a,b]

  23.         self.ellipsePara=[random.randint(30,40),random.randint(20,30)]   
  24.         theta=2*math.pi/self.nParticle
  25.         for i in range(self.nParticle):
  26.             t=random.uniform(-1.0/16,1.0/16)  #产生一个 [-1/16,1/16) 的随机数
  27.             x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t)    #椭圆参数方程
  28.             xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta),  y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta)     #平面旋转方程
  29.             self.oneParticle.append([xx,yy])
  30.         
  31.         self.curParticle=self.oneParticle[0:]     #当前粒子坐标
  32.         self.thread=threading.Thread(target=self.extend)   #建立线程对象
  33.         

  34.     def extend(self):         #粒子群状态变化函数线程
  35.         for i in range(100):
  36.             self.status+=1    #更新状态标识
  37.             self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle]   #更新粒子群坐标
  38.             time.sleep(self.speed/50)
  39.    
  40.     def explode(self):
  41.         self.thread.setDaemon(True)    #把现程设为守护线程
  42.         self.thread.start()          #启动线程
  43.             

  44.     def __repr__(self):
  45.         return ('color:{color}\n'  
  46.                 'speed:{speed}\n'
  47.                 'number of particle: {np}\n'
  48.                 'center:[{cx} , {cy}]\n'
  49.                 'ellipse:a={ea} , b={eb}\n'
  50.                 'particle:\n{p}\n'
  51.                 ).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])


  52. def colorChange(fire):
  53.     rgb=re.findall(r'(.{2})',fire.color[1:])
  54.     cs=fire.status
  55.    
  56.     f=lambda x,c: hex(int(int(x,16)*(100-c)/30))[2:]    #当粒子寿命到70%时,颜色开始线性衰减
  57.     if cs>70:
  58.         ccr,ccg,ccb=f(rgb[0],cs),f(rgb[1],cs),f(rgb[2],cs)
  59.     else:
  60.         ccr,ccg,ccb=rgb[0],rgb[1],rgb[2]
  61.         
  62.     return '#{0:0>2}{1:0>2}{2:0>2}'.format(ccr,ccg,ccb)



  63. def appendFirework(n=1):   #递归生成烟花对象
  64.     if n>maxFireworks or len(Fireworks)>maxFireworks:
  65.         pass
  66.     elif n==1:
  67.         cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:])   # 产生一个0~16777215(0xFFFFFF)的随机数,作为随机颜色
  68.         a=firework(cl,random.uniform(1.5,3.5),width,height)
  69.         Fireworks.append( {'particle':a,'points':[]} )   #建立粒子显示列表,‘particle’为一个烟花对象,‘points’为每一个粒子显示时的对象变量集
  70.         a.explode()
  71.     else:
  72.         appendFirework()
  73.         appendFirework(n-1)


  74. def show(c):
  75.     for p in Fireworks:                #每次刷新显示,先把已有的所以粒子全部删除
  76.         for pp in p['points']:
  77.             c.delete(pp)
  78.    
  79.     for p in Fireworks:                #根据每个烟花对象,计算其中每个粒子的显示对象
  80.         oneP=p['particle']
  81.         if oneP.status==100:        #状态标识为100,说明烟花寿命结束
  82.             Fireworks.remove(p)     #移出当前烟花
  83.             appendFirework()           #新增一个烟花
  84.             continue
  85.         else:
  86.             li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle]       #把中心为原点的椭圆平移到随机圆心坐标上
  87.             color=colorChange(oneP)   #根据烟花当前状态计算当前颜色
  88.             for pp in li:
  89.                 p['points'].append(c.create_oval(pp[0]-oneP.radius,  pp[1]-oneP.radius,  pp[0]+oneP.radius,  pp[1]+oneP.radius,  fill=color))  #绘制烟花每个粒子

  90.     root.after(50, show,c)  #回调,每50ms刷新一次

  91. if __name__=='__main__':
  92.     appendFirework(maxFireworks)
  93.    
  94.     root = tk.Tk()
  95.     cv = tk.Canvas(root, height=height, width=width)
  96.     cv.create_rectangle(0, 0, width, height, fill="black")

  97.     cv.pack()

  98.     root.after(50, show,cv)
  99.     root.mainloop()
复制代码
图片展示


司徒寂寞 2022-10-16 12:05:14 来自手机 | 显示全部楼层
能复制粘贴吗
小明Python 2022-10-18 22:19:15 | 显示全部楼层
司徒寂寞 2022-10-22 17:58:40 来自手机 | 显示全部楼层
小明Python 发表于 2022-10-18 22:19

加个微信18382472973可能有点迟
司徒寂寞 2022-10-22 17:58:52 来自手机 | 显示全部楼层
小明Python 发表于 2022-10-18 22:19

加个微信18382472973可能有点迟
匿名  发表于 2023-1-18 19:52:05
后缀改为啥
H.U.C.-鳌 2023-1-18 20:26:48 | 显示全部楼层
抱走了多谢
H.U.C.-鳌 2023-1-18 20:26:58 | 显示全部楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行