网络编程
位置:首页>> 网络编程>> Python编程>> python代码实现烟花实例

python代码实现烟花实例

作者:/^Mike^/  发布时间:2022-09-13 18:09:18 

标签: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}\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)

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()

图片展示:

python代码实现烟花实例

来源:https://blog.csdn.net/weixin_50973371/article/details/111120569

0
投稿

猜你喜欢

  • numpy 数组及运算扩展库 numpy 是 Python 支持科学计算的重要扩展库,是数据分析和科学计算领域如 scipy、pa
  • 前言:泛型是静态类型语言的基本特征,允许将类型作为参数传递给另一个类型、函数、或者其他结构。TypeScript 支持泛型作为将类型安全引入
  • 在python中进行两个整数相除的时候,在默认情况下都是只能够得到整数的值,而在需要进行对除所得的结果进行精确地求值时,想在运算后即得到浮点
  • 本文实例讲述了利用PHP函数计算中英文字符串长度的方法。分享给大家供大家参考。具体实现方法如下:一般来说大家知道英文字符占一个字节,而中文字
  • 错误15105,从网上找了一些解决方案,一般都是说文件的权限不足的问题,当然附加的时候必须是有数据库附加权限才可以操作的。解决办法1:给相应
  • 这次哀悼,网页设计方面除了应用CSS灰度配色和滤镜,还用到正计时代码,就象汶川大地震已过去了多少天。下面这段代码,是从网易页面提取出来的,具
  • HP注释规范注释在写代码的过程中非常重要,好的注释能让你的代码读起来更轻松,在写代码的时候一定要注意注释的规范。“php是一门及其容易入门的
  • <?php //设置我们将要使用的文件 $srcurl = "http://localhost/index.php"
  • 从BbsXp提出来的生肖函数Zodiac(birthday)。使用方法:birthday为把要判断的出生时间,如2008-3-24 20:0
  • PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转。PHP Chinese Tool class, support Chinese pi
  • 兼容当前HTML/XHTML文档是否有DTD声明:以下为程序代码:var xtop = document.documentElement.s
  • CSS网页布局应该避免滥用div元素一直是我们倡导的,以合适的HTML标签组织文档是CSS网页布局的基础。页面中div与span元素的使用是
  • 方法1: 代码如下:truncate table TableName 删除表中的所有的数据的同时,将自动增长清零。 如果有外键参考这个表,这
  • 在ASP中,除了ADODB、Scripting 等一些常用组件外,我们还可以用微软的ActiveX方法来轻松捕获哟: <%u
  • Python操作注册表步骤之1.打开注册表对注册表进行操作前,必须打开注册表。在Python中,可以使用以下两个函数:RegOpenKey和
  • openpyxl模块是一个读写Excel 文档的Python库,openpyxl是一个比较综合的工具,能够同时读取和修改Excel文档。op
  • 最近在看python脚本语言,脚本语言是一种解释性的语言,不需要编译,可以直接用,由解释器来负责解释。python语言很强大,而且写起来很简
  • 存储过程的优缺点: 存储过程优点: 1.由于应用程序随着时间推移会不断更改,增删功能,T-SQL过程代码会变得更复杂,StoredProce
  • 第一次碰到这个问题的时候,确实不知道该怎么办,后来请教了一个大神,加上自己的理解,才了解是什么意思,这个东西写python的会经常用到,而且
  • 今天发现百度图片搜索结果的2级页面改版了,在浏览图片的时候很好用:如图:在浏览图片的时候,右侧的缩略图是这样交互的:因此,在整个浏览图片的过
手机版 网络编程 asp之家 www.aspxhome.com