Python相声<大数据>双人对话动画

"""这是一个Python动画,显示的是两个人讲相声,相声是我自己编的。"""

import os
from surtle import *
from face_and_functions  import *

def match_condition(line=""):
    """检测一行中是否包括两个冒号,本函数用下列一行也可以定义:
       match_condition = lambda line="":list(line).count(":") == 2
    """
    s = line.split(":")
    if len(s) == 3 :
        return True
    else:
        return False
    
def talk_end():
    """相声结束后显示的文字信息。"""
    screen.bgpic("images/背景.png")
    t = Turtle(visible=False)
    t.penup()
    t.sety(-50)
    t.color("cyan")
    info = "大数据,指互联网不断产生\n的海量数据集合,需要新处理\n模式才能让它转换为真正有\n价值的信息资产。"
    t.write(info,align='center',font=("黑体",22,"italic"))
    t.sety(-130)
    info = "风火轮少儿编程荣誉出品"
    t.color("yellow")
    t.write(info,align='center',font=("宋体",14,'normal'))
    t.screen.onclick(lambda x,y:t.screen.bye())

    
def load(cross_talk_file):
    """加载装有相声文字的文本文件。"""
    words = []
    f = open(cross_talk_file)
    for line in f:
        if not match_condition(line) :continue
        w = line.split(":")        
        sentence = w[0]              #相声文本
        second = int(w[1])           #说话时长
        sound = w[2].strip()         #音效类型
        words.append((sentence,second,sound))
    f.close()
    return words

def cross_talk(cross_talk_file):
    """说相声函数,轮流从列表中显示文字"""   
    boy = Surtle()    
    boy.goto(-150,-60)
    
    girl = Surtle() 
    girl.setheading(180)
    girl.goto(150,10)

    index = 1
    c = [boy,girl]
    if os.path.exists(cross_talk_file):
        words = load(cross_talk_file)
    else:
        screen.title(project_name + ",相声文件不存在,显示默认对话。")
        words = [("小胖,要下雨了!快回家啊。",2,""),("我知道啊,我在等我爷爷呢。",2,""),("好吧,你继续,我在等我妈。",2,""),("这么大了还要妈妈来接....",2,"xiao")]
    for sentence,second ,sound,in words:        
        c[index].speak(sentence,second)       
        if sound!="": playsound(sounddict[sound + "_sound"])
        index = 1 - index
    sleep(1)

    talk_end()
    
def wait_fall_end():
    if face.shakecounter>0: 
        screen.ontimer(wait_fall_end,1000)
    else:
        face.hideturtle() 
        screen.bgpic("images/篮球场背景.png")
        writer = Turtle(visible = False)      #写标题的海龟对象
        writer.penup()                        #抬笔
        writer.color("cyan")                  #设定颜色
        writer.sety(100)                      #设定y坐标
        writer.write(project_name,align="center",font = ("黑体",22,"normal"))
        sleep(3)
        playmusic("audios/相声背景音.wav")
        writer.clear()
        cross_talk("xiangsheng.txt")          #开始说相声

if __name__ =="__main__":

    project_name = "相声《大数据》_作者:李兴球"
    success_sound = "audios/胜利鼓掌吹.wav"
    aa_sound  = "audios/乌鸦叫.wav"
    xiao_sound = "audios/大笑.wav"
    silent_sound = "audios/silent.wav"
    sound_names = ['success_sound','aa_sound','xiao_sound','silent_sound']
    sound_files = [success_sound,aa_sound,xiao_sound,silent_sound]
    sounddict= init_sound(sound_names,sound_files)
    playsound(sounddict["success_sound"])      #笑声来了...

    screen = init_screen(project_name,480,360,"images/背景.png",["images/笑掉牙.gif"]) 
    face = Face(shape = "images/笑掉牙.gif")

    wait_fall_end()

    screen.mainloop()

python相声《大数据》双人动画