Python少儿创意编程_新年快乐音乐贺卡.py 作者:李兴球

"""Python少儿创意编程_新年快乐音乐贺卡.py
   风火轮少儿编程就是原来的星空培训(萍乡星空少儿编程).
   本程序使用python3.7,需要准备枕头模块(PIL在python3中已更名为pillow模块)
   更新制作日期:2018年11月12日
   作者:李兴球,本源代码及素材下载网址:
   http://www.halifa.net/download/newyear.rar

   这是一个用python制作的多媒体动画,以前做过类似的,这个是更新版。把拆帧的代码单独分出去一个模块叫yeartools.py。它在最下面,没有素材运行不了,会报错,当然你也可以自己准备好素材,需要把screen的colormode设为255。Python的海龟模块可不仅仅是用来画图哦,这里用它来做多媒体动画。
"""
__author__ = "李兴球"
__date__ = "2018年11月12日"

from winsound import PlaySound,SND_ASYNC
from turtle import *
from time import sleep
from random import randint
import time,os
from yeartools import *

#nezha_sentence = "哪吒作为顶天立地的神话英雄,他不仅神力通天,法力高强,武功高强,还是位镇守在一带其信仰地区的大型吉祥物,他英勇善战,驱邪除恶,诛灭牛鬼蛇神,是妖魔鬼怪的克星,象征着福娃类的福神之尊,被供为斩妖除魔、降龙伏虎的少年英雄。"

project_name = "风火轮少儿编程过新年音乐贺卡"
imagefile = "新年快乐.gif"
images_list,width,height = split_gifimage(imagefile)
color_list = ['cyan','yellow','white','yellow','yellow']
position_list = [(-150,50),(-280,0),(-200,-50),(-240,-170),(-240,-200)]
strings = ['风火轮少儿编程','祝大家新年快乐,幸福美满,吉祥如意!','本作品更新日期:2018年11月12日! 作者:李兴球','本作品源代码与素材下载网址:','http://www.scratch8.net/download/newyear.rar']
fonts = [("微软雅黑",32,"normal"),("",20,"normal"),("",12,"normal"),("",12,"normal"),("",16,"normal")]
dx_list  = [45,30,16,16,12]
delay_list  = [0.1,0.1,0,0,0]

screen = Screen()

screen.colormode(255)
screen.setup(width,height+100) 
screen.title(project_name)
screen.bgcolor((0,0,51))
screen.bgpic("背景.png") 

piter=Turtle(visible=False )
piter.pencolor("white")
piter.penup()
piter.speed(0) 
"""打星星代码段"""
for i in range(30):                                 #随机打点
    piter.goto(randint(-300,250),randint(-150,150))
    piter.dot(randint(2,4))

"""写字代码段"""
for i in range(5):                                  #写五line字,内容,大小,风格,颜色都不同
    stepwrite(piter,color_list[i],position_list[i],strings[i],fonts[i],dx_list[i],delay_list[i])
                       

for i in range(300):
    sleep(0.01)
    screen.update()
screen.cv.postscript(file="风火轮少儿编程(请用photoshop打开).ps") #保存为ps图像文件,photoshop能解析.
f = open("风火轮少儿编程.txt",mode= 'w')      #打开文件,以写模式
f.write(__doc__)                              #保存python源代码文件的说明文档
f.close()                                     #关闭文件

piter.clear()
piter.color("white")

"""背景切换代码段"""
index=0
def alt_background():
    global index
    screen.bgpic(images_list[index])
    index=index+1
    index=index % 5
    screen.ontimer(alt_background,100)    
alt_background()

"""在舞台上印歌词代码段"""
piter.goto(0,-230)
song_file="中国娃娃 - 发财发福中国年.wav"    #歌曲文件
lrc_file="发财发福中国年歌词.lrc"            #歌词文件
words_list=[]                                #歌词列表
words_index=0

f=open(lrc_file)
words_=f.readlines()                         
f.close()
words_list=[ line.strip() for line in words_ if len(line)>1]  
words_lines=len(words_list)


PlaySound(song_file, SND_ASYNC) #异步播放音效

def get_time_axis(index):
    """获取时间轴"""
    songtime=words_list[index]
    songtime=songtime.split("]")[0]
    songtime=songtime.split(":")
    songtimef=songtime[0][1:3]
    songtimef=int(songtimef)*60    
    songtimem=float(songtime[1])
    return int((songtimef+songtimem)*1000)
 

words_index=0
begin_time=time.time()
def display_subtitle():
    """随着音乐显示歌词函数"""
    global words_index            #歌词索引号
    global words_lines            #歌词line数
    
    current_time=time.time()
    running_time=(current_time-begin_time)*1000
    screen.title(project_name + "," + str(running_time))
    if get_time_axis(words_index)<running_time:
        piter.clear()
        display_words_=words_list[words_index].split("]")[1]
        piter.goto(0,-230)
        piter.pencolor("black")
        piter.write(display_words_,align='center',font=("",24,"normal"))
        piter.goto(-1,-229)
        piter.pencolor("white")
        piter.write(display_words_,align='center',font=("",24,"normal"))
        
        #print("当前words_index:" + str(words_index) + ",words_:" + display_words_) 
        words_index=words_index+1
    if words_index<words_lines:        
        screen.ontimer(display_subtitle,100)
        
display_subtitle()
screen.mainloop()

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

以下是yeartools.py模块源代码:

from PIL import Image,ImageSequence
import os,time

def split_gifimage(filename):
    """拆帧到文件夹,形成png文件"""
    if not os.path.exists(filename):return
    filename = os.path.split(filename)[-1]
    basename = os.path.splitext(filename)[0]
    if not os.path.exists(basename):os.mkdir(basename)
    
    gif_image = Image.open(filename)  #载入图片
    w,h=gif_image.size
    #ImageSequence.Iterator(图象) 能返回图形中的每一帧。

    index = 1
    frames_list=[]

    for frame in ImageSequence.Iterator(gif_image):  #对于图形中的每一帧
        文件名= basename + os.sep + basename + str(index) + ".png"
        frame.save(文件名)
        frames_list.append(文件名)
        index += 1

    print( "拆分gif完毕!共拆成了" + str(index-1) + "张png图片")
    return frames_list,w,h


def stepwrite(t,color,position,string,fontstyle,dx,delay):
    """ t:海龟对象,color:画笔颜色,position:坐标元组,string:要写的字
        fontstyle:字的风格,三元组,dx:水平方向移动的间隔
    """
    t.color(color)
    t.goto(position)
    for char in string:
        t.write(char,font = fontstyle)
        time.sleep(delay)
        t.setx(t.xcor() + dx)
 


 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李兴球

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值