本篇内容主要讲解“Python如何实现梦幻钢琴小游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python如何实现梦幻钢琴小游戏”吧!
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)小程序截图
到此,相信大家对“Python如何实现梦幻钢琴小游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。