# -*- coding: utf-8 -*- """ Created on Sat Dec 3 11:45:42 2022 @author: 86155 """ import pygame import time import threading music=input("请输入歌曲名") Name=music musicName=open(file=music,mode='r',encoding='utf-8') music1=musicName.readline() music2=musicName.readline() musicName.close() print('播放{}'.format(Name)) rhythm=eval(input("请输入节拍(正常速度是0.5)")) beat=rhythm print('节拍已确定') tone=input('请确定基调') #创建键盘#确定基调 if tone == 'C' : pianokey={'1':'c1','2':'d1','3':'e1','4':'f1','5':'g1','6':'a1','7':'b1', 'q':'c','w':'d','e':'e','r':'f','t':'g','y':'a','u':'b', 'a':'c2','s':'d2','d':'e2','f':'f2','g':'g2','h':'a2','j':'b2', 'z':'_C','x':'_D','c':'_E','v':'_F','b':'_G','n':'_A','m':'_B', 'i':'c3','o':'d3','p':'e3','k':'f3','l':'g3',';':'a3',',':'b3', '0':'0' } elif tone =='D': pianokey={'1':'d1','2':'e1','3':'f1#','4':'g1','5':'a1','6':'b1','7':'c2#', 'q':'d','w':'e','e':'f#','r':'g','t':'a','y':'b','u':'c1#', 'a':'d2','s':'e2','d':'f2#','f':'g2','g':'a2','h':'b2','j':'c3#' } elif tone =='E': pianokey={'1':'e1','2':'f1#','3':'g1#','4':'a1','5':'b1','6':'c2#','7':'d2#', 'q':'e','w':'f#','e':'g#','r':'a','t':'b','y':'c1#','u':'d1#', 'a':'e2','s':'f2#','d':'g2#','f':'a2','g':'b2','h':'c#','j':'d#', '0':'0' } elif tone =='F': pianokey={'1':'f1','2':'g1','3':'a1','4':'a1#','5':'c2','6':'d2','7':'e2', 'q':'f','w':'g','e':'a','r':'a#','t':'c1','y':'d1','u':'e1', 'a':'f2','s':'g2','d':'a2','f':'a2#','g':'c3','h':'d3','j':'e3', '0':'0' } elif tone =='G': pianokey={'1':'g1','2':'a1','3':'b1','4':'c2','5':'d2','6':'e2','7':'f2#', 'q':'g','w':'a','e':'b','r':'c1','t':'d1','y':'e1','u':'f1#', 'a':'g2','s':'a2','d':'b2','f':'c3','g':'d3','h':'e3','j':'f3#', '0':'0' } elif tone =='A': pianokey={'1':'a1','2':'b1','3':'c2#','4':'d2','5':'e2','6':'g2#','7':'h2#', 'q':'a','w':'b','e':'c1#','r':'d1','t':'e1','y':'g1#','u':'h1#', 'a':'a2','s':'b2','d':'c3#','f':'d3','g':'e3','h':'g3#','j':'h3#', '0':'0'} elif tone =='B': pianokey={'1':'b1','2':'c2#','3':'d2#','4':'e2','5':'f2#','6':'g2#','7':'a2#', 'q':'b','w':'c1#','e':'d1#','r':'e1','t':'f1#','y':'g1#','u':'a1#', 'a':'b2','s':'c3#','d':'d3#','f':'e3','g':'f3#','h':'g3#','j':'g3#', '0':'0' } print('基调已确定') pygame.init()#初始化? pygame.mixer.init()#初始化混音器模块 def playpiano(music): music=music.split(' ') for n in music: if n=='|': continue elif '·_' in n : pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*5/4) elif '_·'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*(3/4)) elif '__'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat/4) elif '_'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat/2) elif '·'in n: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*(1.5)) elif '-'in n: k=n.count('-') pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat*(k+1)) else: pygame.mixer.Sound("audios/"+pianokey[n[0]]+ ".wav").play(maxtime = 1500) time.sleep(beat) bg_size = width, height = 1000, 700 background=pygame.image.load(r"钢琴大师.jpg") screen = pygame.display.set_mode(bg_size) screen.blit(background,(0,0)) pygame.display.update() pygame.display.set_caption("钢琴弹奏") lt = threading.Thread(target=playpiano, args=[music1]) rt = threading.Thread(target=playpiano, args=[music2]) rt.start() lt.start() rt.join() lt.join()