亲爱的,
这是我看到窗外不停地下着小雨,有感而发用python的海龟模块编写的一个小程序,希望你喜欢。
回复我的公众号pythonxiaoyu,可得到此作品所有源代码与素材,赶快行动吧!
这些年来我还创造了很多Python创意程序,大多放在我的博客里,网址是: www.lixingqiu.com
__author__ = '李兴球'
__blog__ = 'www.lixingqiu.com'
import os
import re
import time
from turtle import *
from winsound import *
def xsleep(s):
start = time.time()
while time.time() - start< s:
screen.update()
# 给原生海龟对象增加play方法
def RawTurtle_play(self,song_file,lrc_file=None,fontstyle=("",24,"normal"),loop=False ):
"""在海龟屏幕显示歌词并播放歌曲,本函数只支持无损wav文件。
self:海龟对象或其子类的实例
song_file:歌曲文件
lrc_file:歌词文件,诸如:[00:00.00]月满西楼
[01:00.12]红藕香残 玉簟秋
上面这样的歌词文件。
fontstyle为三元组,表示用write写字时的字体风格。
loop:为假示不循环播放,为True表示循环播放
"""
if loop == True:
PlaySound(song_file, SND_ASYNC|SND_LOOP)# 异步循环播放音效
else:
PlaySound(song_file, SND_ASYNC) # 异步播放音效
if lrc_file==None:return # 无歌词文件则返回
if not os.path.exists(lrc_file):
print("歌词文件没有找到!")
return
f = open(lrc_file) # 打开歌词文件
words_=f.readlines() # 读取歌词文件
f.close() # 关闭文件
# 正则表达式检测歌词文件内容
reg='\[\d\d:\d\d\.\d\d\]'
result = re.findall(reg,"".join(words_)) # 如果有[00:00.33]这样的则会返回非空列表
if not result:
print("歌词文件貌似有问题!")
return
x,y = self.position() # 歌词中央坐标
fgcolor = self.pencolor() # 歌词前景色
bgcolor = self.fillcolor() # 歌词背景色
words_list=[] # 歌词列表
words_index=0 # 歌词索引
words_list=[ line.strip() for line in words_ if len(line)>1]
words_lines=len(words_list)
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():
"""随着音乐显示歌词函数"""
nonlocal words_index # 歌词索引号
nonlocal words_lines # 歌词line数
current_time=time.time()
running_time=(current_time-begin_time)*1000
# 如果逝去的时间大于歌词文件中那个时间点就换歌词
if running_time > get_time_axis(words_index):
self.clear()
display_words_=words_list[words_index].split("]")[1]
self.goto(x,y)
self.color(bgcolor)
# 在左上一个单位印字
self.write(display_words_,align='center',font=fontstyle)
self.goto(x-1,y+1)
self.color(fgcolor)
self.write(display_words_,align='center',font=fontstyle)
words_index=words_index+1
if words_index < words_lines:
self.screen.ontimer(display_subtitle,100)
# 调用显示标题的函数
display_subtitle()
RawTurtle.play = RawTurtle_play
RawTurtle.addy = lambda self,dy:self.sety(self.ycor() + dy)
Sprite = Turtle
screen = Screen()
screen.setup(535,760)
screen.delay(0)
bgs = [f'frames/{i:04d}.png' for i in range(1,61)]
index = 0
stop = False
def alt_background():
global index
if stop:return
screen.bgpic(bgs[index])
index += 1
index %= len(bgs)
screen.ontimer(alt_background,100)
alt_background()
music = '张明敏 - 三月里的小雨.wav'
lrc = '歌词2.txt'
a = Sprite(visible=False)
a.penup()
a.speed(0)
a.color('blue')
a.sety(300)
a.play(music,lrc,loop=True)
xsleep(10)
for _ in range(560):
screen.cv.move(screen._bgpic,0,1)
xsleep(0.01)
stop = True
screen.bgpic('nopic')
bgs = [f'loves/{i:04d}.png' for i in range(1,4)]
stop = False
screen.bgcolor('black')
index = 0
alt_background()
for _ in range(570):
screen.cv.move(screen._bgpic,0,-1)
xsleep(0.01)
ft = ('楷体',14,'normal')
b = Turtle(visible=False)
b.penup()
b.speed(0)
b.sety(-280)
b.color('yellow')
infos = ['本程序主要由Python turtle模块编写而成',
'作者:李兴球 @ 2021/3/5 www.lixingqiu.com',
'关注公众号回复pythonxiaoyu得到此作品的源代码',
'感谢,观看本程序运行结果。']
for info in infos:
b.write(info,align='center',font=ft)
b.addy(-25)
gf = '李兴球Python公众号.gif'
screen.addshape(gf)
g = Turtle(visible=False)
g.penup()
g.speed(0)
g.sety(-170)
g.shape(gf)
g.showturtle()
screen.mainloop()