当前位置:   article > 正文

小学生Python编程 —— 欢乐钢琴_f'music/{name}.mp3

f'music/{name}.mp3

孩子的又一作品,欢乐钢琴

from pgzrun import *

WIDTH = 960
HEIGHT = 720

#{
o = 0
name = "s"
song = Actor(name + str(o) + ".png", [480, 180])
a_press = False
#动效函数
def change():
    global o
    song.image = name + str(o) + ".png"
    o = o + 1
    if o > 39:
        o = 0
#}
bg = Actor("bg.png")
def draw():
    bg.draw()
    #{
    song.draw()#}
    
def play(n):
    music.play_once(n + ".mp3")
    bg.image = n + ".png"

note = []
def on_key_down():
    
    if keyboard.k_1 == True:
        play("1")
        note.append("1")
    elif keyboard.k_2 == True:
        play("2")
        note.append("2")
    #{
    elif keyboard.k_3 == True:
        play("3")
        note.append("3")
    elif keyboard.k_4 == True:
        play("4")
        note.append("4")
    elif keyboard.k_5 == True:
        play("5")
        note.append("5")
    elif keyboard.k_6 == True:
        play("6")
        note.append("6")
    elif keyboard.k_7 == True:
        play("7")
        note.append("7")
    elif keyboard.k_8 == True:
        play("8")
        note.append("8")
    #}
    
    elif keyboard.a == True:
        clock.schedule_interval(play_again, 0.6)
       
        #{
        #按下a后,调用动效函数change
        global o, a_press, name
        o = 0
        a_press = True
        name = "dance"
        clock.schedule_interval(change, 0.03)
        #}
        
    elif keyboard.s == True:
        clock.unschedule(play_again)
    
    #按x键保存
    elif keyboard.x == True:
        get_music("my_music") #可填入自己取的音乐名

def on_key_up():
    bg.image = "bg.png"

num = 0
def play_again():
    global num
    n = note[num]
    play(n)
    num = num + 1
    
    if num > len(note) -1:
        num = 0
    
#{ 
# 鼠标点击曲谱,切换
def on_mouse_down(pos):
    global o
    if song.collidepoint(pos):
        if not a_press:
            o = o + 1
            if o > 24:
                o = 0
            song.image = name + str(o) + ".png"
            
#按弹奏顺序读取单个音符文件,写入命名为name的文件            
def get_music(name):
    f_read = bytes()
    for n in note:
        with open(f"music/m{n}.mp3","rb") as f1:
            f_read += f1.read()
        with open(f"music/{name}.mp3","wb") as f2:
            f2.write(f_read)
#}

go()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/384483
推荐阅读
相关标签
  

闽ICP备14008679号