本篇内容主要讲解“Python如何实现梦幻钢琴小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python如何实现梦幻钢琴小游戏”吧!
一、环境准备
1)运行环境
开发环境:Python3、Pycharm社区版、tkinter,部分自带的模块安装Python即可使用。
2)模块安装
第三方库的安装方式如下:
一般安装:pip install +模块名
镜像源安装:pip install -i https://pypi.douban.com/simple/+模块名
二、代码展示
1)导入模块
import winsoundfrom tkinter import *
2)主程序
def doone():for i in range(1,11):i=i*500winsound.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 = 1j=0k=0m=0time1 = [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+1if i==0:if music[i] == '0' and music[i+1] == '.' and music[i+2] == '5':time1[j]=0.5j=j+1t=0while t<10:if music[i] == match[t] and music[i+3] == '/':time1[j]=tj=j+1t=t+1else:if music[i] == '0' :time1[j]=0.5j=j+1for tp in range(1,9):if (music[i] == match[tp] and music[i-1] == '/' ):time1[j]=tpj=j+1for tn in range(0,4):if music[i] == char[tn] :order1[m]=tnm=m+1for 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]=ttk=k+1print(n)l=0while 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+1l=0while 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)小程序截图
到此,相信大家对“Python如何实现梦幻钢琴小游戏”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!