当前位置:首页 » 《随便一记》 » 正文

Python烟花秀

0 人参与  2023年01月28日 12:33  分类 : 《随便一记》  评论

点击全文阅读


e51d0386db8742e7ac13b70329d4be95.png

前言

Python跨年烟花表演,具体源码见:Python跨年烟花代码-Python文档类资源-CSDN下载

烟花的粒子类

class particle:     #烟花的粒子类
    def __init__(self,canvas,num,sums,x,y,x_speed,y_speed,explosion_speed,color,size,max_life):
        self.canvas=canvas    #画布
        self.num=num          #粒子的序号
        self.sums=sums        #粒子的个数
        self.x=x              #粒子的横向坐标
        self.y=y              #粒子的纵向坐标
        self.x_speed=x_speed    #粒子在横向的移动速度
        self.y_speed=y_speed    #粒子在纵向的移动速度
        self.initial_speed=explosion_speed    #粒子的初始速度
        self.color=color       #粒子的颜色
        self.life=0            #粒子当前存活的时间
        self.max_life=max_life   #粒子最大的存活时间
        self.oval=self.canvas.create_oval(x-size,y-size,x+size,y+size,fill=self.color)   #粒子的范围(烟花的大小)
    def expand(self):    #判断粒子是否还在爆炸
        if self.life<=1.5:    #粒子是否到达最大爆炸时间
            return 1
        else:
            return 0
    def alive(self):     #判断粒子是否存活
        if self.life<=self.max_life:     #粒子是否到达最大存活时间
            return 1
        else:
            return 0
    def new(self,dt):    #更新当前烟花的粒子位置
        self.life=self.life+dt   #更新当前存活时间
        if self.alive() and self.expand():   #如果当前存活时间在爆炸时间内
            move_x=m.cos(m.radians(self.num*360/self.sums))*self.initial_speed   #则执行爆炸,更新横向坐标
            move_y=m.sin(m.radians(self.num*360/self.sums))*self.initial_speed   #更新纵向坐标
            self.canvas.move(self.oval,move_x,move_y)    #在画布上更新烟花
            self.x_speed=move_x/(float(dt)*1000)    #烟花绽放的速度
        elif self.alive():    #如果爆炸结束了,但粒子还存在,则开始坠落
            move_x=m.cos(m.radians(self.num*360/self.sums))    #更新横向坐标
            self.canvas.move(self.oval,self.x_speed+move_x,self.y_speed+0.05*dt)  #在画布上更新烟花
            self.y_speed=self.y_speed+0.5*dt     #烟花坠落的速度
        elif self.oval is not None:    #如果在坠落的时间外了,就将烟花从画布上擦除
            canvas.delete(self.oval)
            self.oval=None

烟花秀的实现

def fireworks(canvas):#烟花函数(循环更新当前界面)
    times=ti.time()       #获取当前时间戳
    explode_points=[]     #烟花列表
    wait_time=r.randint(1,10)      #等待时间
    num_explode=r.randint(20,30)     #烟花的个数
    for point in range(num_explode):     #依次更新各个烟花
        firework=[]      #当前烟花的粒子列表
        x=r.randint(50,550)   #当前烟花的粒子在横向的活动范围
        y=r.randint(50,150)   #当前烟花的粒子在纵向的活动范围
        speed=r.uniform(0.5,2)   #粒子的绽放速度
        size=r.uniform(0.5,1.5)    #粒子的大小
        color=r.choice(colors)     #粒子的颜色
        explosion_speed=r.uniform(0.2,5)    #粒子爆炸的速度
        sum_particles=r.randint(30,50)      #粒子的总数
        max_life=r.uniform(0.6,1.75)        #粒子的最大存活时间
        for i in range(1,sum_particles):   #当前烟花的每个粒子的参数
            fire=particle(canvas,num=i,sums=sum_particles,explosion_speed=explosion_speed,x=x,y=y,x_speed=speed,y_speed=speed,color=color,size=size,max_life=max_life)
            firework.append(fire)    #将当前粒子加入到当前的烟花粒子列表中
        explode_points.append(firework)   #将当前烟花加入到烟花列表中
    expand_time=0     #初始爆炸的时间
    while expand_time<2:    #如果爆炸的时间小于2
        ti.sleep(0.001)     #爆炸帧
        newtime=ti.time()   #获取爆炸一次的时间戳
        times,dt=newtime,newtime-times    #更新当前的时间戳
        for point in explode_points:     #更新所有的粒子位置
            for item in point:
                item.new(dt)
        canvas.update()    #更新当前画布
        expand_time=expand_time+dt   #更新当前的爆炸时间
    global root   #root界面是全局变量
    root.after(wait_time,fireworks,canvas) 

 


点击全文阅读


本文链接:http://m.zhangshiyu.com/post/51743.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • [杀我全家,我成神灭你全族你求饶什么?]口碑神作必读篇章_萧自然苏巧巧小说***章节抢先看
  • 穿越古代:从直播带货开始节选试读_[周敞]全集阅读
  • 雨后微光照耀你黎苏夏怀安情感纠葛_雨后微光照耀你黎苏夏怀安情感纠葛
  • 全网独家(离梦难圆,心事难收:结局+番外)离梦难圆,心事难收:结局+番外_秦嘉丽邱民宇列表_笔趣阁(离梦难圆,心事难收:结局+番外)
  • (番外)+(全书)叶墨谨幽璃迟少瑜(幽思燃烬忘川路/叶墨谨幽璃:结局+番外)_叶墨谨幽璃迟少瑜免费列表_笔趣阁(幽思燃烬忘川路/叶墨谨幽璃:结局+番外)
  • 完结文死后得知我竟是豪门大佬白月光在线列表_完结文死后得知我竟是豪门大佬白月光在线(楚冉冉)
  • [重生1990:被开除我直接当厂长]限时免费章节抢先看_[陈平安海棠]小说***章节抢先看
  • 被养子推入海后,我直接入伍了节选试读_谢知林轩妹妹节选隐藏剧情入口‌
  • 「此后余生皆孤寂」精彩章节免费试读_温知夏谢清野角色专属支线试读入口
  • 完结文爱意沉眠余生相忘(又名:爱意沉眠余生相忘)列表_完结文爱意沉眠余生相忘(又名:爱意沉眠余生相忘)(江芙顾司裕宋嫣)
  • 权途美人香在线在线品鉴(秦朗)
  • 完结文我死后,每天都要靠男主续命全列表_完结文我死后,每天都要靠男主续命全(林初月季随)

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1