当前位置:首页  电脑知识  其它

利用python实现全屏爱心雨素材

利用python实现全屏爱心雨素材

日期:2023-07-14 20:37: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()

效果如下图:

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

相关推荐