iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >利用python实现全屏爱心雨向喜欢的人表白
  • 779
分享到

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

python全屏爱心雨python 画爱心Python编出爱心 2023-05-20 08:05:13 779人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录一 音乐播放二 爱心创建三 爱心下落四 展示表白语五 主函数创建窗口对象和开启多线程总结 以下核心代码参考黑客帝国的界面的雨滴图和网友的爱心素材 一 音乐播放 def

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

一 音乐播放

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实现全屏爱心雨向喜欢的人表白的文章就介绍到这了,更多相关Python实现全屏爱心雨内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 利用python实现全屏爱心雨向喜欢的人表白

本文链接: https://www.lsjlt.com/news/214243.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作