# -*- coding: utf-8 -*-
#泡妞秘籍https://www.lmtaolu.cn/shop/view2.html
#网站建设https://www.ygwzjs.cn/category-1.htm
#365小吃技术论坛:https://365xiaochi.ygxcjs.cn/forum-2-1.html
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}\n'
'speed:{speed}\n'
'number of particle: {np}\n'
'center:[{cx} , {cy}]\n'
'ellipse:a={ea} , b={eb}\n'
'particle:\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])
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)
#泡妞秘籍https://www.lmtaolu.cn/shop/view2.html
#网站建设https://www.ygwzjs.cn/category-1.htm
#365小吃技术论坛:https://365xiaochi.ygxcjs.cn/forum-2-1.html
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://www.lmtaolu.cn/shop/view2.html
# 网站建设https://www.ygwzjs.cn/category-1.htm
# 365小吃技术论坛:https://365xiaochi.ygxcjs.cn/forum-2-1.html
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
python烟花代码/烟花代码编程python满屏/python烟花代码高级【python烟花代码动态免费】 在IT行业中,Python是一种广泛应用的编程语言,以其简洁明了的语法和强大的功能深受程序员喜爱。本主题聚焦于“Python跨年烟花代码”,这是一项利用Python编程实现的视觉特效,通常用于庆祝新年或其他特殊场合,为用户带来欢乐的视觉体验。 在描述中提到的“跨年烟花效果”,我们可以理解为在屏幕上模拟真实的烟花绽放情景。这种效果通常通过绘制图形、动画和色彩变化来实现。在Python中,可以借助几个关键库来完成这一任务,比如PIL(Python Imaging Library)用于图像处理,matplotlib用于绘制图形,以及可能用到的pygame或tkinter等库来创建GUI界面和处理动画。 PIL库是Python中的图像处理库,它可以用来创建、打开、修改和保存各种图像文件格式。在“55.png”到“66.png”这些图片文件中,很可能包含了烟花效果的各个阶段截图,这些图片可能是在代码执行过程中生成的中间结果或者最终的动画帧。 python烟花代码/烟花代码编程python
资源推荐
资源详情
资源评论

























收起资源包目录






























共 26 条
- 1
资源评论

- 2401_880384342024-11-05发现一个宝藏资源,资源有很高的参考价值,赶紧学起来~
- 2301_771583932025-01-08资源很不错,内容和描述一致,值得借鉴,赶紧学起来!


YG亲测源码屋
- 粉丝: 475
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 供电系统安全风险管理办法.doc
- 神经收集bp算法数学天然科学专业资料新版.pptx
- 互联网开发及应面试用求职简历模板.docx
- 基于51,52单片机的电子万年历的设计说明.doc
- 2020互联网销售工作总结.pdf
- 信息系统安全题库.doc
- 人教A版数学必修三课件:第一章--1.2.3基本算法语句(共63张PPT).ppt
- 上海证券交易所通信网络技术白皮书.doc
- 二元BCH码译码算法的优化与应用的开题报告.docx
- awtk-mvvm-C语言资源
- 综合布线中的七大子系统演示课件.ppt
- 灌云县侍庄中学2008-2009年网络安全培训计划.doc
- 计算机控制课程设计最小拍无波纹.doc
- 网络与信息安全自查表.docx
- 工作总结-工作总结范例-数据库课程工作总结.doc
- 人工智能与生活讲座1.ppt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
