From 5b1c737dc2259b5e6cf7eea9cba48b49d8ebd1d1 Mon Sep 17 00:00:00 2001 From: hnu202109060315 Date: Sun, 4 Dec 2022 17:10:47 +0800 Subject: [PATCH] ADD file via upload --- piano3.py | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 piano3.py diff --git a/piano3.py b/piano3.py new file mode 100644 index 0000000..c39f1ef --- /dev/null +++ b/piano3.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- +""" +Created on Sun Dec 4 15:31:43 2022 + +@author: 86155 +""" + +# -*- coding: utf-8 -*- +""" +Created on Sat Dec 3 11:45:42 2022 + +@author: 86155 +""" + +import pygame +import time +import threading +from turtle import * +from random import random,randint + +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) + +def painting (a): + screen = Screen() + width ,height = 800,600 + screen.setup(width,height) + screen.title("星图") + screen.bgcolor("black") + screen.mode("logo") + screen.delay(0) + + + t = Turtle(visible = False,shape='circle') + t.pencolor("white") + t.fillcolor("white") + t.penup() + t.setheading(-90) + t.goto(width/2,randint(-height/2,height/2)) + + stars = [] + for i in range(200): + star = t.clone() + s =random() /3 + star.shapesize(s,s) + star.speed(int(s*10)) + star.setx(width/2 + randint(1,width)) + star.sety( randint(-height/2,height/2)) + star.showturtle() + stars.append(star) + + while True: + for star in stars: + star.setx(star.xcor() - 3 * star.speed()) + if star.xcor()<-width/2: + star.hideturtle() + star.setx(width/2 + randint(1,width)) + star.sety( randint(-height/2,height/2)) + star.showturtle() +lt = threading.Thread(target=playpiano, args=[music1]) +rt = threading.Thread(target=playpiano, args=[music2]) +hand= threading.Thread(target=painting, args=('1')) +hand.start() +rt.start() +lt.start() +hand.join() +rt.join() +lt.join() +