找回密码
 会员注册
查看: 30|回复: 0

Python新春烟花盛宴

[复制链接]

4

主题

0

回帖

13

积分

新手上路

积分
13
发表于 2024-9-8 14:04:31 | 显示全部楼层 |阅读模式
写在前面哈喽小伙伴们,博主在这里提前祝大家新春快乐呀!我用Python绽放了一场新春烟花盛宴,一起来看看吧!环境需求python3.11.4及以上PyCharmCommunityEdition2023.2.5pyinstaller6.2.0(可选,这个库用于打包,使程序没有python环境也可以运行,如果想发给好朋友的话需要这个库哦~)【注】python环境搭建请见:https://want595.blog.csdn.net/article/details/134586653pyinstaller使用教程见:https://want595.blog.csdn.net/article/details/134106807新春烟花程序设计#导入必要的库importtkinter#导入tkinter库用于GUIimportrandom#导入random库用于生成随机数#函数:将窗口居中显示defcenter_window(window,width,height):screen_width=window.winfo_screenwidth()screen_height=window.winfo_screenheight()x=(screen_width-width)//2y=(screen_height-height)//2window.geometry(f'{width}x{height}+{x}+{y}')#设置窗口的位置和大小#函数:生成随机颜色defcolor():r=random.randint(0,255)g=random.randint(0,255)b=random.randint(0,255)returnf'#{r:02x}{g:02x}{b:02x}'#返回随机颜色值的字符串表示#创建主窗口root=tkinter.Tk()#创建主窗口对象root.title("新春烟花")#设置窗口width=888height=666center_window(root,width,height)#调用center_window函数将窗口居中显示#创建画布canvas=tkinter.Canvas(root,bg='black')#创建画布对象,背景色为黑色canvas.pack(fill=tkinter.BOTH,expand=1)#将画布填满整个窗口并扩展#鼠标点击事件处理函数defon_click(event):x=event.xy=event.yfirework=Firework(canvas,x,y)#创建Firework对象firework.fire()#发射烟花#绑定鼠标点击事件canvas.bind('',on_click)#运行主循环root.mainloop()#启动主循环,等待用户交互事件……完整代码https://want595.blog.csdn.net/article/details/136029420https://want595.blog.csdn.net/article/details/136029420程序分析这段代码是使用Python的Tkinter库创建的一个简单GUI应用程序,模拟了新春烟花的效果。程序的主要功能是在用户点击画布时,在鼠标点击的位置生成并动态展示烟花效果。代码结构与功能概述:1.导入所需的库:  -`tkinter`:用于构建图形用户界面(GUI)的基础库。  -`random`:用于生成随机数以实现不同颜色和粒子运动轨迹的变化。2.定义辅助函数:  -`center_window(window,width,height)`:接受窗口对象、宽度和高度参数,计算窗口在屏幕上的居中坐标,并设置窗口大小和位置。3.定义一个生成随机颜色的方法`color()`,返回一个随机RGB值表示的颜色字符串。4.定义了一个名为`Firework`的类,其包含以下方法:  -`__init__(self,canvas,x,y)`:初始化烟花对象,包括画布引用、初始坐标、随机颜色以及烟花粒子列表。  -`create_particles(self)`:为烟花生成一定数量(99个)的随机速度的粒子,每个粒子用椭圆表示,并存储粒子ID及其速度信息。  -`update(self)`:更新烟花状态,增加粒子半径、减少寿命,并根据粒子速度移动粒子,移除超出画布范围的粒子。  -`fire(self)`:在画布上绘制出烟花主体,并启动定时器调用`launch`方法开始连续更新烟花状态。  -`launch(self)`:连续执行`update`方法,当所有粒子消失或烟花寿命结束时从画布上删除烟花。5.创建主窗口`root`,设置和窗口尺寸,并调用`center_window`将其居中显示。6.在窗口内创建一个黑色背景的画布,并将画布填满整个窗口。7.定义鼠标点击事件处理函数`on_click(event)`,在鼠标左键点击时,根据点击坐标创建一个`Firework`实例并发射烟花。8.将鼠标点击事件绑定到画布上。9.最后运行主循环`root.mainloop()`,持续监听用户交互和GUI事件,直到窗口被关闭。通过这个程序,用户可以在画布上任意位置点击来触发烟花动画,每朵烟花由多个随机颜色和方向的粒子组成,粒子会按照设定的速度在画布上移动,逐渐扩散并最终消失,从而模拟出真实的烟花绽放和消散的效果。运行结果​系列文章序号文章目录直达链接表白系列1无法拒绝的表白界面https://want595.blog.csdn.net/article/details/1347448942满屏飘字表白代码https://want595.blog.csdn.net/article/details/1350373883无限弹窗表白代码https://want595.blog.csdn.net/article/details/1347447114李峋同款可写字版跳动的爱心https://want595.blog.csdn.net/article/details/1347441915流星雨https://want595.blog.csdn.net/article/details/1347474086漂浮爱心https://want595.blog.csdn.net/article/details/1347449297爱心光波https://want595.blog.csdn.net/article/details/1347473658玫瑰花https://want595.blog.csdn.net/article/details/134747447节日系列1新春/跨年烟花秀(2022)https://want595.blog.csdn.net/article/details/128727394烟花秀(2023)https://want595.blog.csdn.net/article/details/135042880粒子烟花https://want595.blog.csdn.net/article/details/1360294202圣诞节圣诞礼物https://want595.blog.csdn.net/article/details/135336583圣诞树(2022)https://want595.blog.csdn.net/article/details/128428985绿色圣诞树(2023)https://want595.blog.csdn.net/article/details/135048607粉色圣诞树(2023)https://want595.blog.csdn.net/article/details/1350430423冬至大雪纷飞https://want595.blog.csdn.net/article/details/1288060174生日生日蛋糕https://want595.blog.csdn.net/article/details/1287397555儿童节五彩气球https://want595.blog.csdn.net/article/details/1287410436国庆节国庆祝福https://want595.blog.csdn.net/article/details/1287409237万圣节万圣礼物https://want595.blog.csdn.net/article/details/1287343958愚人节愚人代码https://want595.blog.csdn.net/article/details/1286969909中秋节浪漫星空https://want595.blog.csdn.net/article/details/12873728410植树节樱花树https://want595.blog.csdn.net/article/details/128700178动漫系列1名侦探柯南系列柯南https://want595.blog.csdn.net/article/details/1347776132喜羊羊与灰太狼系列喜羊羊https://want595.blog.csdn.net/article/details/134778583懒羊羊https://want595.blog.csdn.net/article/details/134847642灰太狼https://want595.blog.csdn.net/article/details/135335303小灰灰https://want595.blog.csdn.net/article/details/135335445小香香https://want595.blog.csdn.net/article/details/1350567833海绵宝宝系列海绵宝宝https://want595.blog.csdn.net/article/details/1348473644哆啦A梦系列哆啦A梦https://want595.blog.csdn.net/article/details/1350378845HelloKitty系列hellokittyhttps://want595.blog.csdn.net/article/details/1353377326Tom&Jerry系列Tom&Jerryhttps://want595.blog.csdn.net/article/details/1353377757草莓熊系列草莓熊https://want595.blog.csdn.net/article/details/1353378328皮卡丘系列迷你皮卡丘https://want595.blog.csdn.net/article/details/135337911高级皮卡丘https://want595.blog.csdn.net/article/details/135337937豪华皮卡丘https://want595.blog.csdn.net/article/details/135337947炫酷系列1  一闪一闪亮星星系列张万森下雪了https://want595.blog.csdn.net/article/details/135336915一闪一闪亮星星https://want595.blog.csdn.net/article/details/1353370492代码雨https://want595.blog.csdn.net/article/details/1350543413七彩花朵https://want595.blog.csdn.net/article/details/13505667043D星空https://want595.blog.csdn.net/article/details/1350565165金榜题名https://want595.blog.csdn.net/article/details/1350561506满天星https://want595.blog.csdn.net/article/details/135056305……写在后面我是一只有趣的兔子,感谢你的喜欢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-10 18:58 , Processed in 0.440054 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表