利用python实现全屏爱心雨向喜欢的人表白

作者:完美,其实并不美 时间:2022-05-29 22:32:14 

以下核心代码参考黑客帝国的界面的雨滴图和网友的爱心素材

一 音乐播放

def alarm():
   # 初始化模
   pygame.init()
   pygame.mixer.init()
   # 加载一个音乐
   file = r'C:\Users\95853\PycharmProjects\contanctoracle\.mp3'
   pygame.mixer.music.load(file)
   pygame.mixer.music.play()
   time.sleep(65)    # 播放65秒
   pygame.mixer.music.stop() # 停止播放

二 爱心创建

def rainmake(canvas, imagefile):
   rainlist = []
   for i in range(20):
       # 根据图片,创建一排心
       rainlist.append(canvas.create_image(100 + 80 * i, INIT_HEIGHT, anchor=NE, image=imagefile))
   return rainlist

三 爱心下落

ef raindown(tk, canvas, imagefile, sec):
   # 线程间等待
   time.sleep(sec)
   rainlist = rainmake(canvas, imagefile)

# 每颗心的纵坐标值
   height = [INIT_HEIGHT] * 20
   while True:
       # 每次移动前稍等一会
       time.sleep(0.2)

# 20颗心一起移动
       for i in range(20):
           # 如果这颗心到底了,则不继续移动,否则height重置就无效了
           if not height[i] == 0:
               # 设置下落步调
               rnd = random.randint(5, 50)
               canvas.move(rainlist[i], 0, rnd)
               height[i] = height[i] + rnd
               tk.update()

for i, h in enumerate(height):
           if h > 600:
               # 当这颗心走到最下方,则删除
               canvas.delete(rainlist[i])
               tk.update()
               # 清空这颗心的height
               height[i] = 0
               print(i, h, height)

# 20颗心全到底,则跳出循环
       if height == [0] * 20:
           print('break:', threading.current_thread().name)
           break

四 展示表白语

def lookloop(tk, canvas, thread):
   aliveflg = False
   alarm()
   while True:
       # 5s检测一次
       time.sleep(5)
       for th in thread:
           if th.is_alive():
               aliveflg = True
           else:
               aliveflg = False
       if aliveflg == False:
           break
   # Over
   text = '雨停了,But.I Love You Too...'
   # canvas.font = "bold 4000px Arial"
   canvas.create_text(1050, 450, text=text, fill='red', anchor='e', font=('宋体', 40,))
   canvas.pack()
   time.sleep(10)
   tk.destroy()

五 主函数创建窗口对象和开启多线程

def main():
   # 创建窗口对象
   tk = Tk()
   tk.title('爱心之雨')

canvas_style = {
       'bg': 'white',
       'height': '1000',
       'width': '1400',
       'cursor': 'circle'
   }
   # 创建画布
   canvas = Canvas(tk, canvas_style)
   canvas.pack()
   imagefile = PhotoImage(file="7777777.gif")

thread = []
   for i in range(60):#60
       thread.append(threading.Thread(target=raindown, args=(tk, canvas, imagefile, i)))
   for t in thread:
       t.start()

# 新开一个线程监控运行中的60个线程
   threading.Thread(target=lookloop, args=(tk, canvas, thread)).start()

效果如下图:

动态爱心下落时,播放音乐,播完后展示一句表白话。

利用python实现全屏爱心雨向喜欢的人表白

总结 

来源:https://huaweicloud.csdn.net/63802f81dacf622b8df865ab.html

标签:python,全屏,爱心雨
0
投稿

猜你喜欢

  • Python使用pandas导入xlsx格式的excel文件内容操作代码

    2022-03-12 04:29:57
  • ASP中页面限权访问的几种方法

    2007-12-13 06:53:00
  • MySQL创建带特殊字符的数据库名称方法示例

    2024-01-26 15:31:30
  • 2008年Logo设计10大趋势

    2008-02-28 13:06:00
  • 百度留言本js 大家可以参考下

    2024-04-16 09:31:55
  • Python使用wxPython实现计算器

    2021-08-31 11:23:54
  • 使用虚拟机在VirtualBox+openEuler上安装部署openGauss数据库

    2024-01-21 22:45:53
  • XMLHttpRequest的浏览器兼容代码写法

    2008-09-02 10:46:00
  • PHP基于yii框架实现生成ICO图标

    2024-06-05 09:44:14
  • Python数据类型及常用方法

    2022-09-22 08:09:19
  • python3.4爬虫demo

    2023-10-24 21:46:24
  • mysql时区查看与设置方法

    2024-01-20 02:01:46
  • 常见Dreamweaver使用过程中的问题及解决办法

    2011-03-17 16:16:00
  • TensorFlow人工智能学习创建数据实现示例详解

    2022-04-20 12:58:27
  • vscode添加GIT和SVN的方法示例

    2023-08-24 17:44:38
  • css学习笔记:div在IE6下无法遮盖select

    2009-04-30 13:21:00
  • SQL Server 数据库实用SQL语句

    2024-01-14 00:10:40
  • set rs=server.CreateObject("adodb.recordset") 的中文详细说明

    2011-03-06 11:21:00
  • 解决python使用list()时总是报错的问题

    2021-11-27 23:34:36
  • python 基于Appium控制多设备并行执行

    2022-12-11 12:00:06
  • asp之家 网络编程 m.aspxhome.com