您好,登錄后才能下訂單哦!
本篇內容主要講解“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。