首页 > 文章列表 > Python实战之梦幻钢琴小游戏的实现

Python实战之梦幻钢琴小游戏的实现

Python
486 2023-03-17

导语

今天来给大家写一款界面化的(Tkinter)电子钢琴小程序。

音乐是声音的艺术,它通过人们的听觉,来传达思想情感、表现社会生活。但它与舞蹈、绘画不同的呈现形式不同,舞蹈是通过肢体语言,绘画是通过色彩和线条来体现艺术形象。而音乐不是一种具象的形态,它需要通过人们的内心去感受,去理解。

因此,在音乐的学习中,有很多抽象的概念对孩子来说非常难以理解,例如律动、放松、呼吸等。其实,这些看似“只可意会不可言传”的概念,以及一些手指技巧,都可以通过一些形象的比喻,向孩子们说明问题,这不仅能提高孩子们的学习兴趣,并且还能使这些知识在脑海里留下深刻印象。

今天这款电子钢琴小程序不仅可以学到小知识还能边玩儿边锻炼手速鸭~爱了没?

一、环境准备

1)运行环境 

开发环境:Python3、Pycharm社区版、tkinter,部分自带的模块安装Python即可使用。 

2)模块安装 

第三方库的安装方式如下: 

一般安装:pip install +模块名  

镜像源安装:pip install -i https://pypi.douban.com/simple/+模块名 

模块安装问题可以详细的找我给大家讲一下的哈,之前其实也有的文章写了几个点的。 

二、代码展示

1)导入模块

import winsound



from tkinter import *

2)主程序

def doone():

	for i in range(1,11):

		i=i*500

		winsound.Beep(i,500)

def submit():#读取乐谱并播放的函数

	music = list(map(str,u.get()))  #	print(music)	print(p.get())

	order =[[131,147,165,175,196,220,247],[262,296,330,349,392,440,494],[523,587,659,698,784,880,988],[1047,1175,1319,1397,1568,1760,1976]]

	match =['0','1','2','3','4','5','6','7','8','9']

	char =[ 'A','B','C','D']

	n = 1

	j=0

	k=0

	m=0

	time1 = [0 for x in range(10,1000)]

	order1 = [0 for x in range(0,1000)]

	order11 = [0 for x in range(0,1000)]

	orderm = [0 for x in range(0,1000)]

	for i in range(len(music)):

		if music[i] == '/':

			n=n+1

		if i==0:

			if music[i] == '0' and music[i+1] == '.' and music[i+2] == '5':

				time1[j]=0.5

				j=j+1

			t=0

			while t<10:

				if music[i] == match[t] and music[i+3] == '/':

					time1[j]=t

					j=j+1

				t=t+1

		else:

			if music[i] == '0' :

				time1[j]=0.5

				j=j+1

			for tp in range(1,9):

				if (music[i] == match[tp] and music[i-1] == '/' ):

					time1[j]=tp

					j=j+1

			for tn in range(0,4):

				if music[i] == char[tn]  :

					order1[m]=tn

					m=m+1

			for tt in range(1,9):

				if music[i] == match[tt] and ( music[i-1] == 'A' or music[i-1] == 'B' or music[i-1] == 'C' or music[i-1] == 'D') :

					order11[k]=tt

					k=k+1

	print(n)

	l=0

	while l< n:

		time1[l]=time1[l]*int(p.get())

		orderm[l]=order[order1[l]][order11[l]-1]

		time1[l]=int(time1[l])#		print("时间,字母,顺序,音律以此为:")		print(time1[l],   order1[l],  order11[l],orderm[l])

		l=l+1

	l=0

	while l <n:#		print(orderm[l],time1[l])

		winsound.Beep(orderm[l],time1[l])

		l=l+1

#图形界面

root = Tk()

root.title("电子钢琴简易版")

frame = Frame(root)

frame.pack(padx=8, pady=8, ipadx=4)

lab1 = Label(frame, text="请输入乐谱:")

lab1.grid(row=0, column=0, padx=5, pady=5, sticky=W)

#u = StringVar()

#ent1 = Entry(frame, textvariable=u)

#ent1.grid(row=0, column=1, sticky='ew', columnspan=2)

lab2 = Label(frame, text="请输入每个音节代表的时间(单位ms):")

lab2.grid(row=1, column=0, padx=5, pady=5, sticky=W)

p = StringVar()

ent2 = Entry(frame, textvariable=p)

ent2.grid(row=1, column=1, sticky='ew', columnspan=2)

button = Button(frame, text="确认", command=submit, default='active')

button.grid(row=2, column=1)

button2 = Button(frame, text="退出", command=quit)

button2.grid(row=2, column=2, padx=5, pady=5)

button3 = Button(frame, text="第一题播放",command=doone)

button3.grid(row=2, column=0)

三、效果展示

1)运行界面

2)小程序截图