Python昨晚我想你了_创意动画虚像效果文字_淡入文字

Python昨晚我想你了_创意动画虚像效果文字_淡入文字

Python昨晚我想你了虚像淡入文字效果

Python虚像淡入文字效果

要作品提供所有源代码与素材,关注公众号:李兴球Python,回复imissyou,即可得到下载地址.

Python昨晚我想你了虚像淡入文字效果

Python昨晚我想你了虚像淡入文字效果

import os
try:
    from gameturtle import *
except:
    import subprocess
    p = subprocess.Popen(["pip","install","gameturtle"],shell=True)
    from tkinter import messagebox
    t ='风火轮编程提示:'
    p = '''没有找到gameturtle模块,程序无法运行。\n
程序会自动进行安装,如果重新启动程序后还无法运行。\n
请手动安装gameturtle模块,方法:\n用cmd命令打开管理员窗口,\n然后输入pip install gameturtle\n
如果还是不知道操作,请加李兴球微信scratch8提供技术支持。\n
gameturtle模块详细说明,请见《Python海龟宝典》下册原理篇。'''
    messagebox.showwarning(t,p)
    
from winsound import PlaySound,SND_LOOP,SND_ASYNC
    
def xsleep(cv,t):
    start = time.time()
    while time.time() - start < t:
        cv.update()

def sprite_fade_in(sp):
    """sp:角色,本函数让角色淡入,前提是有很多透明度不同的造型"""
    sp.setindex(0)                                   # 设定造型索引号为0
    sp.show()
    for _ in range(sp._shape_amounts-1):
        sp.nextshape()
        sp._canvas.update()        
        time.sleep(0.008)
        
def make_one_sentence(string,canvas):
    """生成一个句子(一系列汉字角色),返回列表"""
    cors = [(50 + x*36,100) for x in range(len(string))]# 每个角色的坐标    
    sprites = []
    for char,xy in zip(string,cors):
        frames = [txt2image(char,fontsize=32,color=(255,0,255,alpha),
                  stroke=(2,(200,200,200,alpha))) for alpha in range(0,256,4)]
        s = Sprite(canvas,frames,visible=False,pos=xy)
        sprites.append(s)
    return sprites

def show_sone_sentence(sprites):
    """sprites:角色们"""
    cv = sprites[0]._canvas
    [sprite_fade_in(sprite) for sprite in sprites]
    xsleep(cv,1)
    [sprite.hide() for sprite in sprites]
    xsleep(cv,1)
     
def alt_background(bgsprite):
    """切换背景图片"""    
    cv = bgsprite._canvas    
    bgsprite.nextshape()
    cv.update()
    cv.after(100,lambda :alt_background(bg))

def move_advertise():
    if left.counter < 200:
        cv = left._canvas
        [cv.move(zi,0,-1) for zi in allzi]
        left.counter += 1
        cv.after(100,move_advertise)            
    
def move_heart_pic():
    cv = left._canvas 
    if left.xcor()<120:        
       left.addx(1)
       right.addx(-1)
       cv.after(100,move_heart_pic)
    else:
       left.counter = 0           # 仅为了不使用全局变量借用的一个属性 
       move_advertise()
    
if __name__ =='__main__':

    root = Tk()
    root.geometry('480x604+0+0')
    root.title('昨晚我想你了,关注公众号:李兴球Python,回复 imissyou 即可获得')
    cv = Canvas(width=480,height=604,bg='light gray')
    cv.pack()

    bgimages = [f"ims/{i:04d}.png" for i in range(1,90)]
    bgframes = [Image.open(im) for im in bgimages]
    bg = Sprite(cv,bgframes)      # 背景图片
    alt_background(bg)

    PlaySound('TRY想你伴奏.wav',SND_LOOP|SND_ASYNC)
    # 左右心合起来
    xsleep(cv,1)
    left = Sprite(cv,Image.open('heart_left.png'),pos=(120-240,302))
    right = Sprite(cv,Image.open('heart_right.png'),pos=(360+240,302))
    move_heart_pic()
    
    sentences =[ '昨晚我梦见你了。','不知道是我想你了。','还是你想我了。',
                 '醒来后很难过。','原来我们已经很久没见面了。',
                 '你还好吗?我想你了.....。']
    
    ft = ('黑体',12,'normal')
    zi1 = cv.create_text(240,472+200,text='本作品由Python编程实现',fill='lime',font=ft)
    zi2 = cv.create_text(240,502+200,text='所有源代码和素材皆免费赠送',fill='yellow',font=ft)
    zi3 = cv.create_text(240,532+200,text='关注公众号:李兴球Python',fill='white',font=ft)
    zi4 = cv.create_text(240,562+200,text='回复 imissyou 即可获得',fill='cyan',font=ft)
    allzi = [zi1,zi2,zi3,zi4]           # 所有要显示的字幕文本

    sprites = [make_one_sentence(s,cv) for s in sentences]
    while 1:[show_sone_sentence(s) for s in sprites]
          
 
李兴球

李兴球的博客是Python创意编程原创博客