怎样用Python实现烟花秀效果,要用到哪些模块
Admin 2022-05-21 群英技术资讯 883 次浏览
tkinter
、PIL
、time
、random
、math
,如果第三方模块没有装的话,pip install
一下即可,下面看一下代码实现。import tkinter as tk from PIL import Image, ImageTk from time import time, sleep from random import choice, uniform, randint from math import sin, cos, radians
colors = ['red', 'blue', 'yellow', 'white', 'green', 'orange', 'purple', 'seagreen', 'indigo', 'cornflowerblue']
class fireworks: def __init__(self, cv, idx, total, explosion_speed, x=0., y=0., vx=0., vy=0., size=2., color='red', lifespan=2, **kwargs): self.id = idx # 烟花绽放 x 轴 self.x = x # 烟花绽放 x 轴 self.y = y self.initial_speed = explosion_speed # 外放 x 轴速度 self.vx = vx # 外放 y 轴速度 self.vy = vy # 绽放的粒子数 self.total = total # 已停留时间 self.age = 0 # 颜色 self.color = color # 画布 self.cv = cv self.cid = self.cv.create_oval(x - size, y - size, x + size, y + size, fill=self.color) self.lifespan = lifespan # 更新数据 def update(self, dt): self.age += dt # 粒子膨胀 if self.alive() and self.expand(): move_x = cos(radians(self.id * 360 / self.total)) * self.initial_speed move_y = sin(radians(self.id * 360 / self.total)) * self.initial_speed self.cv.move(self.cid, move_x, move_y) self.vx = move_x / (float(dt) * 1000) # 膨胀到最大下落 elif self.alive(): move_x = cos(radians(self.id * 360 / self.total)) self.cv.move(self.cid, self.vx + move_x, self.vy + 0.5 * dt) self.vy += 0.5 * dt # 过期移除 elif self.cid is not None: cv.delete(self.cid) self.cid = None # 定义膨胀效果的时间帧 def expand(self): return self.age <= 1.5 # 检查粒子是否仍在生命周期内 def alive(self): return self.age <= self.lifespan
def ignite(cv): t = time() # 烟花列表 explode_points = [] wait_time = randint(10, 100) # 爆炸的个数 numb_explode = randint(6, 10) for point in range(numb_explode): # 爆炸粒子列表 objects = [] # 爆炸 x 轴 x_cordi = randint(50, 550) # 爆炸 y 轴 y_cordi = randint(50, 150) speed = uniform(0.5, 1.5) size = uniform(0.5, 3) color = choice(colors) # 爆炸的绽放速度 explosion_speed = uniform(0.2, 1) # 爆炸的粒子数半径 total_particles = randint(10, 50) for i in range(1, total_particles): r = fireworks(cv, idx=i, total=total_particles, explosion_speed=explosion_speed, x=x_cordi, y=y_cordi, vx=speed, vy=speed, color=color, size=size, lifespan=uniform(0.6, 1.75)) # 添加进粒子列表里 objects.append(r) # 把粒子列表添加到烟花列表 explode_points.append(objects) total_time = .0 # 在 1.8 秒时间帧内保持更新 while total_time < 1.8: # 让画面暂停 0.01s sleep(0.01) # 刷新时间 tnew = time() t, dt = tnew, tnew - t # 遍历烟花列表 for point in explode_points: # 遍历烟花里的粒子列表 for item in point: # 更新时间 item.update(dt) # 刷新页面 cv.update() total_time += dt root.after(wait_time, ignite, cv)
if __name__ == "__main__": root = tk.Tk() # 绘制一个画布 cv = tk.Canvas(root, height=400, width=600) # 背景图 image = Image.open("bg.jpg") photo = ImageTk.PhotoImage(image) # 在画板上绘制一张图片 cv.create_image(0, 0, image=photo, anchor='nw') cv.pack() root.protocol(close) root.after(100, ignite, cv) # 生成窗口 root.mainloop()
看一下效果:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家介绍了基于Python如何实现评论区抽奖的功能,文章的示例代码讲解详细,对我们学习Python有一定帮助,需要的朋友可以学习一下
这篇文章主要给大家介绍了关于python学习之panda数据分析核心支持库的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
为什么要用requests模块?requests模块的用法是什么?很多学习python的新手,对于requests模块不是很了解,因此,这篇文章就给大家介绍一下关于requests模块的内容。
python staticmethod 返回类,函数的静态方法。该方法不强制要求传递参数,如下声明一个静态方法:以上实例声明了静态方法 f,从而可以实现实例化使用 C().f(),当然也可以不实例化调用该方法 C.f()。
这篇文章主要介绍了numpy 实现返回指定行的指定元素的位置索引操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008