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文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0