用threading+turtle画哆啦A梦,同时播放背景音乐

生日礼物当然不能随便草草做了,效果如图
用threading+turtle画哆啦A梦,同时播放背景音乐_第1张图片
画图同时会用threading播放背景音乐
还是老毛病,资源文件pyinstaller打包不能读取,就是不能播放背景音乐,但是这个打包后是直接闪退,解决方法可参考我的这两篇
https://mp.csdn.net/mdeditor/101147129#
https://mp.csdn.net/mdeditor/101158379#
下面是代码(为了解决颜色空白真的写的好累)
补充一点,KIVY可以成功打包turtle库的程序,把它放到子函数里就行了

import threading
def play_music():
        import pygame
        import time
        #import b
        filepath = r"勾指起誓.mp3";
        pygame.mixer.init()
        # 加载音乐
        pygame.mixer.music.load(filepath)
        pygame.mixer.music.play(start=0.0)
        #播放时长,没有此设置,音乐不会播放,会一次性加载完
        time.sleep(183)
        pygame.mixer.music.play(loops)
#下面是画哆啦的所有代码
def painting():
    import turtle as t
    def my_goto(x,y):
        t.penup()
        t.goto(x,y)
        t.pendown()
    def eyes():
        t.tracer(False)
        a=2.5
        for i in range(120):
            if 0 <=i<30 or 60<=i<90:
                a-=0.05
                t.lt(3)
                t.fd(a)
            else:
                a+=0.05
                t.lt(3)
                t.fd(a)
        t.tracer(True)

    t.title('哆啦A梦')
    t.pensize(3)
    t.penup()
    t.circle(150,40)
    t.pendown()
    t.fillcolor("#00a0de")#头颜色
    t.begin_fill()
    t.circle(150,280)
    t.end_fill()

    t.fillcolor("#e70010")#项圈颜色
    t.begin_fill()
    t.seth(0)
    t.fd(200)
    t.circle(-5,90)
    t.fd(10)
    t.circle(-5,90)
    t.fd(207)
    t.circle(-5,90)
    t.fd(10)
    t.circle(-5,90)
    t.end_fill()

    #脸
    t.fd(183)
    t.fillcolor('#ffffff')#脸颜色
    t.begin_fill()
    t.lt(45)
    t.circle(120,100)
    t.seth(90)
    eyes()#右边眼睛的圈
    t.penup()
    t.seth(180)
    t.fd(60)
    t.pendown()
    t.seth(90)
    eyes()
    t.penup()
    t.seth(180)
    t.fd(60)
    t.pendown()
    t.seth(215)
    t.circle(120,100)
    t.end_fill()
    
    my_goto(-10,158)
    t.fillcolor('#e70010')#鼻子
    t.begin_fill()
    t.circle(20)
    t.end_fill()
    
    my_goto(5,148)
    t.seth(270)
    t.fd(100)
    t.seth(0)
    t.circle(120,50)
    t.seth(230)
    t.circle(-120,100)
    #胡须
    my_goto(-37,135)
    t.seth(165)
    t.fd(60)
    my_goto(-37,125)
    t.seth(180)
    t.fd(60)
    my_goto(-37,115)
    t.seth(193)
    t.fd(60)
    my_goto(37,135)
    t.seth(15)
    t.fd(60)
    my_goto(37,125)
    t.seth(0)
    t.fd(60)
    my_goto(37,115)
    t.seth(-13)
    t.fd(60)
    my_goto(0,0)
    t.seth(0)

    #位置代码
    my_goto(0,0)
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pendown()
    
    t.pencolor('black')
    t.pensize('1')
    t.pendown()
    #身躯
    t.fillcolor('#00a0de')#先涂右胳膊
    t.begin_fill()
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,270)

    t.seth(230)
    t.fd(80)
    t.end_fill()

    my_goto(0,0)#定位右边手
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pendown()
    
    t.fillcolor('#ffffff')#涂右手
    t.begin_fill()
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,360)
    t.end_fill()

    #修复右胳膊空白
    #三角修复
    my_goto(0,0)#重新定位右边手#保证位置不变
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pencolor('#00a0de')#改笔颜色
    t.pendown()#开始用三角形修复空白
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(-90)#向下旋转90度竖的直角边角度
    t.fd(18)#竖长
    t.seth(180)#旋转180度画横的直角边
    t.fd(20)#横长
    t.seth(60)#斜边与横的夹角
    t.fd(20*(2**(1/2)))#1:1:根号2算出斜边长
    t.end_fill()
    #长方形修复
    my_goto(0,0)#重新定位右边手#保证位置不变
    t.seth(0)
    t.penup()
    t.circle(150,50)
    #t.pencolor('#00a0de')#改笔颜色
    t.pendown()#开始用长方形修复空白
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(-90)#向下旋转90度竖的直角边角度
    t.fd(90)#竖长
    t.seth(180)#旋转180度画横的直角边
    t.fd(4)#横长
    t.seth(90)#斜边与横的夹角
    t.fd(90)#1:1:根号2算出斜边长
    t.end_fill()

    my_goto(0,0)#重新定位右边手#保证位置不变
    t.seth(0)
    t.penup()
    t.pencolor('black')#改笔颜色
    t.circle(150,50)
    t.pendown()
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,270)
    t.seth(230)
    t.fd(80)
    
    #身体下方,左手左胳膊
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(-89)
    t.circle(-1000,10)
    t.seth(180)
    t.fd(70)
    t.seth(90)
    t.circle(30,180)#微调看不出来胳膊的变化
    t.seth(180)
    t.fd(70)
    t.seth(100)
    t.circle(-1000,9)
    t.seth(240)#到达左胳膊
    t.fd(40)
    #t.pencolor('white')
    t.circle(-30,250)#蓝色的手不需要
    t.seth(50)
    t.fd(70)
    t.end_fill()

    #开始用同样的方法修复左边的手
    my_goto(0,0)#定位到右边手
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,270)
    t.seth(230)
    t.fd(80)
    t.seth(-89)
    t.circle(-1000,10)
    t.seth(180)
    t.fd(70)
    t.seth(90)
    t.circle(30,180)#走原路
    t.seth(180)
    t.fd(70)
    t.seth(100)
    t.circle(-1000,9)
    t.seth(240)#到达左胳膊
    t.fd(40)
    #开始涂左手
    t.fillcolor('white')
    t.begin_fill()
    t.circle(-30,360)#白色的手半径一样
    t.end_fill()
    t.seth(50)
    t.fd(70)
    

    
    
    #身体上方空白修复
    t.penup()
    my_goto(-100,20)
    t.pendown()
    t.pencolor('#00a0de')
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(0)
    t.fd(210)
    t.seth(-90)
    t.fd(70)
    t.seth(180)
    t.fd(210)
    t.seth(90)
    t.fd(200)
    t.end_fill()
    
    #小空隙填充
    t.penup()
    my_goto(-100,20)
    


    my_goto(103.74,-197.59)
    t.pencolor('black')
    t.fillcolor('#ffffff')#右脚
    t.begin_fill()
    t.seth(0)
    t.fd(15)
    t.circle(-15,180)
    t.fd(90)
    t.circle(-15,180)
    t.fd(10)
    t.end_fill()

    my_goto(-96.26,-197.59)
    t.seth(180)
    t.fillcolor('#ffffff')#左脚
    t.begin_fill()
    t.fd(15)
    t.circle(15,180)
    t.fd(90)
    t.circle(15,180)
    t.fd(10)
    t.end_fill()

    t.penup()
    my_goto(-103.42,15.09)
    t.seth(0)
    t.fd(38)
    t.seth(230)
    t.pendown()
    t.pencolor('#00a0de')#改笔颜色
    t.fillcolor('#ffffff')#肚子
    t.begin_fill()
    t.circle(90,260)
    t.end_fill()

    my_goto(5,-40)
    t.seth(0)
    t.pencolor('black')#改笔颜色
    t.fillcolor('#ffffff')#百宝袋
    t.begin_fill()
    t.fd(70)
    t.seth(-90)
    t.circle(-70,180)
    t.seth(0)
    t.fd(70)
    t.end_fill()

    
    my_goto(-103.42,15.09)
    t.penup()#去掉铃铛旁边的线
    t.fd(90)
    t.seth(70)
    t.pendown()
    t.fillcolor('#ffd200')#铃铛颜色
    t.begin_fill()
    t.circle(-20)
    t.end_fill()
    t.goto(-13.42,15.09)
    t.seth(250)
    t.circle(20,110)
    t.seth(90)
    t.fd(15)
    t.dot(10)
    my_goto(0,-150)
    t.seth(0)
    my_goto(-20,195)
    t.fillcolor('#000000')#眼睛颜色
    t.begin_fill()
    t.circle(13)
    t.end_fill()
    t.pensize(6)
    my_goto(20,205)
    t.seth(75)
    t.circle(-10,150)
    t.pensize(3)
    my_goto(-17,200)
    t.seth(0)
    t.fillcolor('#ffffff')#眼睛里的光颜色
    t.begin_fill()
    t.circle(5)
    t.end_fill()
    my_goto(0,0)

    t.mainloop()
threads = []
threads.append(threading.Thread(target=play_music))
threads.append(threading.Thread(target=painting))
if __name__ == '__main__':
    for t in threads:
        t.start()

9.25 13:14修改,代码优化,使得音乐可以循环播放

import threading
def play_music():
        import pygame
        import time
        #import b
        filepath = r"勾指起誓.mp3";
        pygame.mixer.init()
        # 加载音乐
        pygame.mixer.music.set_volume(2) 
        pygame.mixer.music.load(filepath)
        pygame.mixer.music.play(start=0.0)
        #播放时长,没有此设置,音乐不会播放,会一次性加载完
        time.sleep(183)
        pygame.mixer.music.fadeout(5)
        #pygame.mixer.music.set_endevent() 
        play_music()#递归重新放音乐
        #pygame.mixer.music.play(loops)
#画哆啦
def painting():
    import turtle as t
    
    def my_goto(x,y):
        t.penup()
        t.goto(x,y)
        t.pendown()
    def eyes():
        t.tracer(False)
        a=2.5
        for i in range(120):
            if 0 <=i<30 or 60<=i<90:
                a-=0.05
                t.lt(3)
                t.fd(a)
            else:
                a+=0.05
                t.lt(3)
                t.fd(a)
        t.tracer(True)

    t.title('哆啦A梦祝你生日快乐')
    t.screensize(15000,15000, "pink")
    t.pensize(3)
    t.penup()
    t.circle(150,40)
    t.pendown()
    t.fillcolor("#00a0de")#头颜色
    t.begin_fill()
    t.circle(150,280)
    t.end_fill()

    t.fillcolor("#e70010")#项圈颜色
    t.begin_fill()
    t.seth(0)
    t.fd(200)
    t.circle(-5,90)
    t.fd(10)
    t.circle(-5,90)
    t.fd(207)
    t.circle(-5,90)
    t.fd(10)
    t.circle(-5,90)
    t.end_fill()

    #脸
    t.fd(183)
    t.fillcolor('#ffffff')#脸颜色
    t.begin_fill()
    t.lt(45)
    t.circle(120,100)
    t.seth(90)
    eyes()#右边眼睛的圈
    t.penup()
    t.seth(180)
    t.fd(60)
    t.pendown()
    t.seth(90)
    eyes()
    t.penup()
    t.seth(180)
    t.fd(60)
    t.pendown()
    t.seth(215)
    t.circle(120,100)
    t.end_fill()
    
    my_goto(-10,158)
    t.fillcolor('#e70010')#鼻子
    t.begin_fill()
    t.circle(20)
    t.end_fill()
    
    my_goto(5,148)
    t.seth(270)
    t.fd(100)
    t.seth(0)
    t.circle(120,50)
    t.seth(230)
    t.circle(-120,100)
    #胡须
    my_goto(-37,135)
    t.seth(165)
    t.fd(60)
    my_goto(-37,125)
    t.seth(180)
    t.fd(60)
    my_goto(-37,115)
    t.seth(193)
    t.fd(60)
    my_goto(37,135)
    t.seth(15)
    t.fd(60)
    my_goto(37,125)
    t.seth(0)
    t.fd(60)
    my_goto(37,115)
    t.seth(-13)
    t.fd(60)
    my_goto(0,0)
    t.seth(0)

    #位置代码
    my_goto(0,0)
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pendown()
    
    t.pencolor('black')
    t.pensize('1')
    t.pendown()
    #身躯
    t.fillcolor('#00a0de')#先涂右胳膊
    t.begin_fill()
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,270)

    t.seth(230)
    t.fd(80)
    t.end_fill()

    my_goto(0,0)#定位右边手
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pendown()
    
    t.fillcolor('#ffffff')#涂右手
    t.begin_fill()
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,360)
    t.end_fill()

    #修复右胳膊空白
    #三角修复
    my_goto(0,0)#重新定位右边手#保证位置不变
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.pencolor('#00a0de')#改笔颜色
    t.pendown()#开始用三角形修复空白
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(-90)#向下旋转90度竖的直角边角度
    t.fd(18)#竖长
    t.seth(180)#旋转180度画横的直角边
    t.fd(20)#横长
    t.seth(60)#斜边与横的夹角
    t.fd(20*(2**(1/2)))#1:1:根号2算出斜边长
    t.end_fill()
    #长方形修复
    my_goto(0,0)#重新定位右边手#保证位置不变
    t.seth(0)
    t.penup()
    t.circle(150,50)
    #t.pencolor('#00a0de')#改笔颜色
    t.pendown()#开始用长方形修复空白
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(-90)#向下旋转90度竖的直角边角度
    t.fd(90)#竖长
    t.seth(180)#旋转180度画横的直角边
    t.fd(4)#横长
    t.seth(90)#斜边与横的夹角
    t.fd(90)#1:1:根号2算出斜边长
    t.end_fill()

    my_goto(0,0)#重新定位右边手#保证位置不变
    t.seth(0)
    t.penup()
    t.pencolor('black')#改笔颜色
    t.circle(150,50)
    t.pendown()
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,270)
    t.seth(230)
    t.fd(80)
    
    #身体下方,左手左胳膊
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(-89)
    t.circle(-1000,10)
    t.seth(180)
    t.fd(70)
    t.seth(90)
    t.circle(30,180)#微调看不出来胳膊的变化
    t.seth(180)
    t.fd(70)
    t.seth(100)
    t.circle(-1000,9)
    t.seth(240)#到达左胳膊
    t.fd(40)
    #t.pencolor('white')
    t.circle(-30,250)#蓝色的手不需要
    t.seth(50)
    t.fd(70)
    t.end_fill()

    #开始用同样的方法修复左边的手
    my_goto(0,0)#定位到右边手
    t.seth(0)
    t.penup()
    t.circle(150,50)
    t.seth(30)#角度
    t.fd(40)#长度
    t.seth(70)#角度
    t.circle(-30,270)
    t.seth(230)
    t.fd(80)
    t.seth(-89)
    t.circle(-1000,10)
    t.seth(180)
    t.fd(70)
    t.seth(90)
    t.circle(30,180)#走原路
    t.seth(180)
    t.fd(70)
    t.seth(100)
    t.circle(-1000,9)
    t.seth(240)#到达左胳膊
    t.fd(40)
    #开始涂左手
    t.fillcolor('white')
    t.begin_fill()
    t.circle(-30,360)#白色的手半径一样
    t.end_fill()
    t.seth(50)
    t.fd(70)
    

    
    
    #身体上方空白修复
    t.penup()
    my_goto(-100,20)
    t.pendown()
    t.pencolor('#00a0de')
    t.fillcolor('#00a0de')
    t.begin_fill()
    t.seth(0)
    t.fd(210)
    t.seth(-90)
    t.fd(70)
    t.seth(180)
    t.fd(210)
    t.seth(90)
    t.fd(200)
    t.end_fill()
    
    #小空隙填充
    t.penup()
    my_goto(-100,20)
    


    my_goto(103.74,-197.59)
    t.pencolor('black')
    t.fillcolor('#ffffff')#右脚
    t.begin_fill()
    t.seth(0)
    t.fd(15)
    t.circle(-15,180)
    t.fd(90)
    t.circle(-15,180)
    t.fd(10)
    t.end_fill()

    my_goto(-96.26,-197.59)
    t.seth(180)
    t.fillcolor('#ffffff')#左脚
    t.begin_fill()
    t.fd(15)
    t.circle(15,180)
    t.fd(90)
    t.circle(15,180)
    t.fd(10)
    t.end_fill()

    t.penup()
    my_goto(-103.42,15.09)
    t.seth(0)
    t.fd(38)
    t.seth(230)
    t.pendown()
    t.pencolor('#00a0de')#改笔颜色
    t.fillcolor('#ffffff')#肚子
    t.begin_fill()
    t.circle(90,260)
    t.end_fill()

    my_goto(5,-40)
    t.seth(0)
    t.pencolor('black')#改笔颜色
    t.fillcolor('#ffffff')#百宝袋
    t.begin_fill()
    t.fd(70)
    t.seth(-90)
    t.circle(-70,180)
    t.seth(0)
    t.fd(70)
    t.end_fill()

    
    my_goto(-103.42,15.09)
    t.penup()#去掉铃铛旁边的线
    t.fd(90)
    t.seth(70)
    t.pendown()
    t.fillcolor('#ffd200')#铃铛颜色
    t.begin_fill()
    t.circle(-20)
    t.end_fill()
    t.goto(-13.42,15.09)
    t.seth(250)
    t.circle(20,110)
    t.seth(90)
    t.fd(15)
    t.dot(10)
    my_goto(0,-150)
    t.seth(0)
    my_goto(-20,195)
    t.fillcolor('#000000')#眼睛颜色
    t.begin_fill()
    t.circle(13)
    t.end_fill()
    t.pensize(6)
    my_goto(20,205)
    t.seth(75)
    t.circle(-10,150)
    t.pensize(3)
    my_goto(-17,200)
    t.seth(0)
    t.fillcolor('#ffffff')#眼睛里的光颜色
    t.begin_fill()
    t.circle(5)
    t.end_fill()
    my_goto(0,0)

    t.mainloop()
threads = []
threads.append(threading.Thread(target=play_music))
threads.append(threading.Thread(target=painting))
if __name__ == '__main__':
    for t in threads:
        t.start()

你可能感兴趣的:(python)