基于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
0
投稿
猜你喜欢
- 一、密码字典所谓密码字典,主要是配合解密使用,一般情况用来暴力破解密码,是由指定字符排列组合组成的文本文件。如果知道密码设置的规律指定性生成
- 处理页面中的间歇无缝滚动新闻的时候,最常见的方法就是将滚动区内容复制追加一份,然后通过控制和判断滚动块的scrollTop来实现滚动停止效果
- 1.1 简介深层神经网络一般都需要大量的训练数据才能获得比较理想的结果。在数据量有限的情况下,可以通过数据增强(Data Augmentat
- 最近尝试把项目迁移到Python环境下,特别新装了一台干净的Debian系统,准备重新配置环境,上网找了一些运行Python Web的环境方
- 上次还是CSDN里的朋友回答的,我复制了下来。原文如下 =========================== 利用统计文章字数,然后达到一
- Notepad++是一款不错的编辑器,很轻巧,我很喜欢它。再换个主题,加个代码高亮,看上去就更专业了。如果你是Mac用户,应该听说或使用过T
- 本文实例为大家分享了python实现微信消息防撤回的具体代码,供大家参考,具体内容如下使用了sqlite3保存数据,当有人撤回消息时取出数据
- 前言随着网站的内容的增多和用户访问量的增多,网站加载会越来越慢,受限于带宽和服务器同一时间的请求次数的限制,,我们往往需要在此时对我们的网站
- 支持向量机可以用来拟合线性回归。 相同的最大间隔(maximum margin)的概念应用到线性回归拟合。代替最大化分割两类目标是,最大化分
- Python输出整数的方法:先使用str()函数将数字转换成字符串赋值给变量i,再用“if i.count('.') ==
- 一、概述现有一个用户表,需要将表数据写入到excel中。环境说明mysql版本:5.7端口:3306数据库:test表名:users表结构如
- 安装pyserialpip install pyserial查看可用的端口# coding:utf-8import serial.tools
- 函数画图以 z = x 2 + y 2 为例#导入模块import numpy as npimport matplotlib.py
- b 和 i 标签在现在的 Web 标准潜规则中是不推荐使用,甚至是反对使用的,因为认为他们只是“表现”粗体和斜体,而没有任何“语义”。更多的
- 阅读上一篇:[译]Javascript风格要素(一) 我们使用习惯用法可以使我们的意图更加的清晰和简洁。使用==时,当心强制转换考虑下面函数
- 最近在做一些email相关的办公自动化项目,发现一个第三方模块imap-tools不错, 网上没有啥相关介绍,所以记录下来.环境: pyth
- 在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引但在nump
- 我们都知道,Numpy 是 Python 环境下的扩展程序库,支持大量的维度数组和矩阵运算;Pandas 也是 Python 环境下的数据操
- Socket是网络编程的一个抽象概念。通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地
- 一、什么是sql子查询? 子查询是一个嵌套在Select 、Insert 、Update 或Dele