import re
import time
from gameturtle import *
from winsound import PlaySound,SND_ASYNC
def rotate_text(self,item,ms=100,du=10,rev=1):
"""旋转文字,item:文字的项目编号,ms调用的时间间隔,
du:每次旋转的角度值,rev:决定是顺时针旋转还是逆时针旋转的参数"""
heading = 0
cv = self._canvas
def rotate():
nonlocal heading
cv.itemconfig(item,angle=heading)
heading += du*rev
heading %= 360
cv.after(ms,rotate)
rotate()
Sprite.rotate_text = rotate_text
def play(self,song_file,lrc_file=None,fontstyle=("",14,"normal"),loop=False ):
"""在海龟屏幕显示歌词并播放歌曲,本函数只支持无损wav文件。
self:海龟对象或其子类的实例
song_file:歌曲文件
lrc_file:歌词文件,诸如:[00:00.00]月满西楼
[01:00.12]红藕香残 玉簟秋
上面这样的歌词文件。
fontstyle为三元组,表示用write写字时的字体风格。
loop:为假示不循环播放,为True表示循环播放
本函数来源于本人编写的Python精灵模块
"""
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)
self._canvas.update()
words_index=words_index+1
if words_index < words_lines:
self._canvas.after(100,display_subtitle)
# 调用显示标题的函数
display_subtitle()
root = Tk()
root.title('童年_庆祝国庆72周年by李兴球')
cv = Canvas(width=480,height=720,bg='#000000')
cv.pack()
mouse = Mouse(cv) # 新建鼠标左键对象
dummy = Sprite(cv,visible=False) # 新建角色叫dummy
dummy.color('blue') # dummy的颜色是蓝色的
zi = list('据说成功人士都有一颗童心'+chr(10084)) # 一个叫zi的列表
while zi:
if mouse.isdownup(): # 如果单击并弹起
c = zi.pop(0)
dummy.goto(*cv.mouseposition())
dummy.write(c,font=('',38))
cv.update()
g = Image.open('萌小海龟.png')
gs = [setalpha(g,a) for a in range(0,256,1)]
cv.config(bg='#FFFF00')
turtle = Sprite(cv,gs)
for i in range(len(gs)):
turtle.setindex(i)
cv.update()
time.sleep(1)
turtle.say('大家好,我是小海龟',3)
turtle.say('2021国庆节来了',3)
turtle.say('听完歌曲,有惊喜!',5)
##turtle.say('我要唠叨一下先',3)
##turtle.say('我家女儿啊,\n每天晚上6点多才回家。',3)
##turtle.say('天都黑了,\n到了冬天,\n那天更黑了',3)
##turtle.say('你说这样的“减负”好吗?',3)
##turtle.say('我理解的“减负”,\n是早点放学',3)
##turtle.say('孩子们有充足的业余时间,\n去探索去发现',3)
##turtle.say('现在呢,\n看不到孩子一丝笑容',4)
##turtle.say('把孩子们都关在学校里,\n学生没有新鲜感\n好奇心会被泯灭\n实乃教育之大忌?',8)
##turtle.say('我家女孩没有参加补习班,\n所以对于我家女儿来说\n就完全是增负',6)
##turtle.say('本来可早点回家画她喜欢的画儿的,\n自由自在的多好',6)
##turtle.say('这样的制度扼杀了个性化的发展',5)
##
time.sleep(1)
for i in range(len(gs)-1,-1,-1):
turtle.setindex(i)
cv.update()
song = Sprite(cv,visible=False)
song.sety(140)
song.color('cyan','black')
play(song,'杨烁 - 童年.wav',lrc_file='歌词2.txt')
cv.config(bg='#81A4CE')
g = Image.open('背景.png').convert("RGBA")
gs = [setalpha(g,a) for a in range(0,256,10)]
backgound = Sprite(cv,gs)
for i in range(len(gs)):
backgound.setindex(i)
cv.update()
hg = Sprite(cv,visible=False)
hg.bk(50)
hg.sety(80)
zi1 = hg.write('童',align='center',font=('楷体',38,'normal'))
hg.fd(100)
zi2 = hg.write("年",align='center',font=('楷体',38,'normal'))
cv.tag_raise(turtle.item)
turtle.goto(320,520)
for i in range(turtle.shapeamounts()):
turtle.setindex(i)
cv.update()
blank = Image.new("RGBA",(1,1)) # 制作空白造型
girl = Sprite(cv,blank)
girl.goto(160,500)
time.sleep(1)
turtle.say('嗨,我又来了',3)
girl.say('你好,小海龟',2)
turtle.say('我又编写一个\nPython程序玩',4)
girl.say('我知道,\n就是现在演示的这个',4)
turtle.say('顺便祝你国庆快乐',3)
girl.say('嗯嗯,有啥礼物呀:)',3)
turtle.say('在我博客里,\n有免费下载。',4)
girl.say('你的博客在哪儿呀',3)
turtle.say('www.lixingqiu.com\n就是上面的网址。',6)
girl.say('thank you,\n我去找找。',3)
turtle.say('一寸光阴一寸金\n寸金难买寸光阴\n少 壮 不 努 力\n老 大 送 快 递',400,False)
time.sleep(1)
girl.say('你说得太对了!',400,False)
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)
-
- 2026 年 7 月
- 2026 年 3 月
- 2026 年 2 月
- 2026 年 1 月
- 2025 年 12 月
- 2025 年 11 月
- 2025 年 10 月
- 2025 年 9 月
- 2025 年 6 月
- 2025 年 5 月
- 2025 年 3 月
- 2025 年 2 月
- 2025 年 1 月
- 2024 年 12 月
- 2024 年 8 月
- 2024 年 6 月
- 2024 年 5 月
- 2024 年 4 月
- 2024 年 3 月
- 2024 年 2 月
- 2023 年 11 月
- 2023 年 9 月
- 2023 年 6 月
- 2023 年 5 月
- 2023 年 4 月
- 2023 年 3 月
- 2023 年 2 月
- 2023 年 1 月
- 2022 年 12 月
- 2022 年 11 月
- 2022 年 10 月
- 2022 年 9 月
- 2022 年 8 月
- 2022 年 7 月
- 2022 年 6 月
- 2022 年 5 月
- 2022 年 4 月
- 2022 年 3 月
- 2022 年 2 月
- 2022 年 1 月
- 2021 年 12 月
- 2021 年 11 月
- 2021 年 10 月
- 2021 年 9 月
- 2021 年 8 月
- 2021 年 7 月
- 2021 年 6 月
- 2021 年 5 月
- 2021 年 4 月
- 2021 年 3 月
- 2021 年 2 月
- 2021 年 1 月
- 2020 年 12 月
- 2020 年 11 月
- 2020 年 10 月
- 2020 年 9 月
- 2020 年 8 月
- 2020 年 7 月
- 2020 年 6 月
- 2020 年 5 月
- 2020 年 4 月
- 2020 年 3 月
- 2020 年 2 月
- 2020 年 1 月
- 2019 年 12 月
- 2019 年 11 月
- 2019 年 10 月
- 2019 年 9 月
- 2019 年 8 月
- 2019 年 7 月
- 2019 年 6 月
- 2019 年 5 月
- 2019 年 4 月
- 2019 年 3 月
- 2019 年 2 月
- 2018 年 3 月
- 2018 年 1 月
- 2017 年 9 月
- 2017 年 5 月
- 2017 年 1 月

