当前位置:网站首页>New Year Fireworks code plus copy, are you sure you don't want to have a look
New Year Fireworks code plus copy, are you sure you don't want to have a look
2022-07-07 05:58:00 【Far away x】
Introduction :
There are many beauties in life , Only this human fireworks , Warm the whole world
I sell voice in the world , When you have enough stars , Set off fireworks to show you
The fireworks
Many cities can't set off fireworks now , But if you slowly raise your hand , Lift to the top , Then suddenly open your five fingers , The congratulations ,
You just set off a fireworks for yourself . At most two at a time , But you know , If you slowly reach over , Hold me , that
In my heart , There are thousands of fireworks —— Rilke 《 Celebrate for me 》
Text :
The beauty of fireworks , So short , But so unrestrained , So warm . Even if there is only one second of life . Also be the most perfect , to open up
The most brilliant .
🥢 Officially tap the code :
# -*- 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) # The particle radius is 2~4 Pixels
self.color=color # Particle color
self.speed=speed #speed yes 1.5-3.5 second
self.status=0 # Without the fireworks exploding ,status=0; After the explosion ,status>=1; When status>100 when , The life of fireworks ends
self.nParticle=random.randint(20,30) # Number of particles
self.center=[random.randint(0,width-1),random.randint(0,height-1)] # Fireworks random center coordinates
self.oneParticle=[] # Original particle coordinates (100% In the state of )
self.rotTheta=random.uniform(0,2*math.pi) # Rotation angle of elliptic plane
# Elliptic parametric equation :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) # Produce a [-1/16,1/16) The random number
x,y=self.ellipsePara[0]*math.cos(theta*i+t), self.ellipsePara[1]*math.sin(theta*i+t) # Elliptic parametric equation
xx,yy=x*math.cos(self.rotTheta)-y*math.sin(self.rotTheta), y*math.cos(self.rotTheta)+x*math.sin(self.rotTheta) # Plane rotation equation
self.oneParticle.append([xx,yy])
self.curParticle=self.oneParticle[0:] # Current particle coordinates
self.thread=threading.Thread(target=self.extend) # Create a thread object
def extend(self): # Particle swarm state change function
for i in range(100):
self.status+=1 # Update status identification
self.curParticle=[[one[0]*self.status/100, one[1]*self.status/100] for one in self.oneParticle] # Update particle swarm coordinates
time.sleep(self.speed/50)
def explode(self):
self.thread.setDaemon(True) # Set the current process as a daemon thread
self.thread.start() # Start thread
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:] # When the particle life reaches 70% when , The color begins to decay linearly
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): # Recursively generate fireworks objects
if n>maxFireworks or len(Fireworks)>maxFireworks:
pass
elif n==1:
cl='#{0:0>6}'.format(hex(int(random.randint(0,16777215)))[2:]) # Produce a 0~16777215(0xFFFFFF) The random number , As a random color
a=firework(cl,random.uniform(1.5,3.5),width,height)
Fireworks.append( {'particle':a,'points':[]} ) # Create a particle display list ,‘particle’ For a fireworks object ,‘points’ The object variable set displayed for each particle
a.explode()
else:
appendFirework()
appendFirework(n-1)
def show(c):
for p in Fireworks: # Every time you refresh the display , First delete all the existing particles
for pp in p['points']:
c.delete(pp)
for p in Fireworks: # According to each fireworks object , Calculate the display object of each particle
oneP=p['particle']
if oneP.status==100: # The status is marked as 100, It means that the life of fireworks is over
Fireworks.remove(p) # Remove the current fireworks
appendFirework() # Add a new fireworks
continue
else:
li=[[int(cp[0]*2)+oneP.center[0],int(cp[1]*2)+oneP.center[1]] for cp in oneP.curParticle] # Translate the ellipse whose center is the origin to the random center coordinates
color=colorChange(oneP) # Calculate the current color according to the current state of fireworks
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)) # Draw each particle of fireworks
root.after(50, show,c) # Callback , Every time 50ms Refresh once
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()🥢 Effect display :

ending :
I hope to meet you on the day of fireworks , In this way, we can get light for a few days before and after the meeting , Become a good day . I also
Hope to play cards together , roast , Eat melon seeds , Watching the TV program of the Spring Festival Gala , Every household is booming , Hot and lively
Noisy .
All right. , This is the end of the article ~
Don't forget to set off fireworks for yourself in either way

If you like it, remember A little bit of praise , If you need a complete source code of the project, you can confide in me !
边栏推荐
- Web architecture design process
- 【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
- Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
- Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
- [daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
- Introduction to distributed transactions
- MySQL-CentOS7通过YUM安装MySQL
- 数字IC面试总结(大厂面试经验分享)
- 《HarmonyOS实战—入门到开发,浅析原子化服务》
- On the difference between FPGA and ASIC
猜你喜欢
![C. colonne Swapping [tri + Simulation]](/img/0e/64d17980d3ec0051cdfb5fdb34e119.png)
C. colonne Swapping [tri + Simulation]
上海字节面试问题及薪资福利

Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)

Introduction to the extension implementation of SAP Spartacus checkout process

深度聚类:将深度表示学习和聚类联合优化
![Cf:c. column swapping [sort + simulate]](/img/0e/64d17980d3ec0051cdfb5fdb34e119.png)
Cf:c. column swapping [sort + simulate]

Introduction to distributed transactions

Realize GDB remote debugging function between different network segments

Randomly generate session_ id

AI face editor makes Lena smile
随机推荐
判断文件是否为DICOM文件
2pc of distributed transaction solution
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
得物客服一站式工作台卡顿优化之路
Say sqlyog deceived me!
Digital IC interview summary (interview experience sharing of large manufacturers)
How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
搞懂fastjson 对泛型的反序列化原理
Mac version PHP installed Xdebug environment (M1 version)
往图片添加椒盐噪声或高斯噪声
PTA ladder game exercise set l2-004 search tree judgment
What EDA companies are there in China?
Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
Go 语言的 Context 详解
Interview questions and salary and welfare of Shanghai byte
Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
PTA 天梯赛练习题集 L2-002 链表去重
Personal imitation SSM framework
Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
《HarmonyOS实战—入门到开发,浅析原子化服务》