用python实现放烟花
疫情太严重了,有很多小伙伴都不能出门玩耍了。给大家看一个特别的风景吧,Python烟花代码,虽然没有现实中的烟花那么美丽,但是在特殊时期足矣安慰自己了。(https://jq.qq.com/?_wv=1027&k=hDGOjqqw)
先给大家看一下效果图
代码如下
# -*- coding: utf-8 -*-import math, random,time
import threading
import tkinter as tk
import re
#import uuidFireworks=[]
maxFireworks=8
height,width=600,600class 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.nParticlefor 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.statusf=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:passelif 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() #新增一个烟花continueelse: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()
最后烟花齐放好像还是有点那什么感觉的,想看更多内容嘛,那你需要关注我。喜欢为你绽放的烟花嘛,那就点个赞。这篇文章到这里就结束了,再见。
用python实现放烟花相关推荐
- 放烟花的python程序
一.前言 重点:需要打包后的程序可以直接评论区留言或者私聊我,无需编程直接使用. 2023春节期间做的烟花程序,支持一定的个性化设置,已打包上传在我的资源.主要学习其它大神的烟花代码,后续自己做了改进 ...
- 七夕节马上到了,有对象的,无对象的必备神奇用Python做一个烟花秀
今天已经周三了还有三天就是七夕节了,要给女朋友买礼物怕是来不及了! 那怎么办呢?那么就给她准备一个就全城烟花视频吧. 可除了土豪,不是所有人都能在整个城市放烟花的.对于一个普通的不能再普通的我也只能想 ...
- 含源码,用Python实现浪漫烟花
目录 前言 环境准备 代码编写 效果展示 前言 Python实现浪漫的烟花特效 现在很多地方都不能放烟花了,既然看不到, 那作为程序猿的我们还不能自己用代码做一个吗? 今天就带大家用代码做一个烟花特效 ...
- 如何用C++实现动态放烟花(附源码)
点击蓝字 关注我们 来源于网络,侵删 一.前言 C++实现的放烟花程序 用到了EGE图形库,没有的需要自行安装 可调项:背景图和背景音乐.粒子模糊度.亮度以及上升速度的参数. 实现的动态烟花非常好看, ...
- c语言烟花百度云,C语言实现放烟花的程序
这是一个利用C语言编写放烟花的程序(同时也可以播放音乐),供大家参考,具体内容如下 代码如下 #pragma once #include #include //图形界面库头文件 #include // ...
- 快过年了,用JS让你的网页放烟花吧
如何实现的? 我自己是一名从事了多年开发的web前端老程序员,前段时间我花了一个月整理了一份最适合学习的web前端学习干货,各种框架都有整理,送给每一位前端小伙伴,想要获取的可以添加下面的QQ群,即可 ...
- 2022跨年烟花代码(六)HTML5鼠标点击页面放烟花特效
HTML5鼠标点击页面放烟花特效 html代码 <!DOCTYPE html> <html> <head><meta http-equiv="Con ...
- Matlab放烟花 带音效哟 祝大家新年快乐
这一次要更新的是用Matlab放烟花!祝大家新年快乐! 效果如下: 2021已然接近尾声,回顾这一年,总有这样或那样的收获,也有一些遗憾. 但是,一切过往,皆为序章. 这一期来填一下上上次留下的坑,用 ...
- 2022跨年烟花代码(九)HTML5夜景放烟花绽放动画效果
HTML5夜景放烟花绽放动画效果 html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
最新文章
- 玩转CSS3(一)----CSS3实现页面布局
- 2-2hadoop概述
- object references an unsaved transient instance save the transient instance before flushing
- 1709 - Index column size too large. The maximum column size is 767 bytes.
- 开源免费强大的按键精灵软件----AutoHotKey
- ★★iPhone越狱后必装软件和一些心得教程,送给广大的小白们,希望你们能从小白变成“小黑”★★
- Bcm96xx 系列芯片 SDK介绍(一)
- mfc使用matlab绘图,mfc调用matlab绘图
- 第四周:基于图像相似度比较的分镜头
- 欧姆龙PLC以太网与西门子WINCC通讯
- PI3体验之无线网AP模式设定及热点分享
- 2019 高校运维赛 writeup
- 微服架构基础设施环境平台搭建 -(一)基础环境准备
- JQuery获取父,子,兄弟节点
- (U8)系统管理登录时提示“缺少根元素”
- 早期股权分配不是有钱就能搞定的!
- Navicat连接不上本地MySQL数据库
- Historic Exhibition 贪心
- 经度从0-360更改为-180到180
- DevOps是什么鬼?