基于Python实现烟花效果的示例代码
作者:m0_54850467 发布时间:2021-02-08 13:25:09
标签: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.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}
'
'speed:{speed}
'
'number of particle: {np}
'
'center:[{cx} , {cy}]
'
'ellipse:a={ea} , b={eb}
'
'particle:
{p}
'
).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)
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://blog.csdn.net/m0_54850467/article/details/125241220


猜你喜欢
- ASP.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式。然而这种简单的应用程序开发模式却给我
- 如下所示:import pymysqlimport timeimport redef get_raw_label(rece): re1 =
- 1 栈的概念栈由一系列对象对象组织的一个集合,这些对象的增加和删除操作都遵循一个“后进先出”(Las
- offsetWidth 包括边框的宽度 clientWidth 不包括<table bord
- 开篇相信广大程序员朋友经常使用MySQL数据库作为书籍持久化的工具,我们最常使用的就是MySQL中的SQL语句,从客户端向MySQL发出一条
- php5.2新增的json功能是非常受欢迎的,但是经过测试发现, json_encode对中文的处理是有问题的, 1.不能处理GB编码,所有
- IE的for...in循环存在严重的缺陷,除了性能低下外,有许多属性不可遍历,著名有这三兄弟:constructor ,toString ,
- 数据丢失(缺失)在现实生活中总是一个问题。 机器学习和数据挖掘等领域由于数据缺失导致的数据质量差,在模型预测的准确性上面临着严重的问题。 在
- 如果你的数据量有几十万条,用户又搜索一些很通俗的词,然后要依次读最后几页重温旧梦。mysql该很悲壮的不停操作硬盘。 所以,可以试着让mys
- 根据 Dotzler 的统计,IE6 的份额正在缩水,这可能是 2009 年本人听到的第一个好消息。于此同时,Gmail 的浏览器支持列表中
- 本文实例讲述了python使用pymongo与MongoDB基本交互操作。分享给大家供大家参考,具体如下:本文内容:pymongo的使用:安
- 前言当你逐渐了解tushare之后,你会发现我们要进行数据分析只靠tushare是不够的,接下来我将介绍如何利用第三方软件将tushare获
- 概述Mysql的Replication(复制)是一个异步的复制过程,从一个 Mysql instance(我们称之为 Master)复制到另
- rabbitmq中文翻译的话,主要还是mq字母上:Message Queue,即消息队列的意思。前面还有个rabbit单词,就是兔子的意思,
- 只做简单地记录,方便一下使用!python关于csv模块的介绍网上有很多资料,这里就不在赘诉。直接给出代码和解释。数据:Symbol,Pri
- 1. 开发1.1. 架构Gorm使用可链接的API,*gorm.DB是链的桥梁,对于每个链API,它将创建一个新的关系。db, err :=
- 引言在已有的网站中,几乎所有的网站都已经实现了 自动登录所谓自动登录,其实就是在你登录后,然后关闭浏览器,接着再启动浏览器重新进入刚刚的网站
- 介绍godep是解决包依赖的管理工具,目前最主流的一种,原理是扫描记录版本控制的信息,并在go命令前加壳来做到依赖管理godep 建议在 g
- .Net新手通常容易把属性(Property)跟特性(Attribute)搞混,其实这是两种不同的东西属性指的类中封装的数据字段;而特性是对
- 当地址栏没有参数"id"时: 代码如下:Request.QueryString["ID"] == n