您好,登錄后才能下訂單哦!
這篇文章主要講解了“基于Python如何實現骰子小游戲”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“基于Python如何實現骰子小游戲”吧!
1)運行環境
開發環境:Python3、Pycharm社區版、tkinter,部分自帶的模塊安裝Python即可使用。
2)模塊安裝
第三方庫的安裝方式如下:
一般安裝:pip install +模塊名
鏡像源安裝:pip install -i pypi.douban.com/simple/+模塊名…
1)主程序界面
import tkinter as tk from sohagame import Unique_SohaGame from Windows import Unique_Windows """主函數程序入口""" if __name__ == '__main__': root = tk.Tk() root['width'] = 600 root['height'] = 400 root['bg'] = 'white' root.title("骰子梭哈游戲") soha = Unique_SohaGame() Unique_Windows(root, soha) root.mainloop()
2)界面類
界面類,主要管理tkintter界面布局, 界面與界面之間的跳轉、 信息傳遞注:為減少冗余描述,類似屬性的相同功能僅介紹一遍, 不重復介紹。 整個游戲界面采用tkinter布局中的place布局
import tkinter as tk import tkinter.messagebox class Unique_Windows: # 界面管理類初始化操作, 傳入兩個參數, root是Tk()對象,用于生成窗口、界面,soha是游戲類對象,用于給按鍵等附加功能 def __init__(self, root, soha): self.root = root self.soha = soha self.lbl = tk.Label(self.root, text='歡迎來到骰子梭哈游戲', font=('microsoft yahei', 15), fg='white', bg='#007d65') self.lbl.place(x=150, y=0) self.lbl1 = tk.Label(self.root, bg='#007d65', text='請操作:', font=('microsoft yahei', 15), fg='Orange') self.lbl1.place(x=120, y=40) self.f1 = None self.f2 = None self.cfl_createStartWidget() def cfl_createStartWidget(self): # 創造游戲root開始界面(下面相同屬性就不一一介紹,道理相同) self.root['width'] = 600 # 設置寬高 self.root['height'] = 400 self.root['bg'] = "#007d65" # 設置背景顏色 if self.f1: # 如果存在一個Frame窗口框架控件,則刪除, 然后新建一個該控件。 self.f1.destroy() self.f1 = tk.Frame(self.root) self.f1['width'] = 400 # 設置控件寬高 self.f1['height'] = 300 self.f1['bg'] = '#007d65' # 設置控件背景顏色 '''添加組件到Frame控件中''' self.btStart = tk.Button(self.f1, text='開始游戲', font=('黑體', 18), # 開始游戲按鍵,點擊開始游戲, 即可進入游戲界面 width=20, fg='black', command=self.cfl_startSohaGame) # 添加按鈕關聯的方法 self.btStart.place(x=80, y=50) # 整個游戲界面采用place布局 self.btEnd = tk.Button(self.f1, text='結束游戲', font=('黑體', 18), width=20, fg='black', command=self.cfl_gameOver) # 添加結束游戲方法 self.btEnd.place(x=80, y=150) self.f1.place(x=0, y=80) def cfl_createContentWidge(self): # 創造游戲內容界面(本質上是重寫了Frame類型f2的布局, 并將以前的f2布局銷毀) """創造游戲內容界面""" self.root['width'] = 800 self.root['height'] = 600 if self.f2: self.f2.destroy() self.f2 = tk.Frame(self.root) self.f2['width'] = 800 self.f2['height'] = 600 self.f2['bg'] = '#d9d6c3' self.f2.place(x=0, y=0) self.lab01 = tk.Label(self.f2, text='雙人骰子/多人骰子游戲', font=('黑體', 30), fg='black', bg="#d9d6c3") self.lab01.place(x=200, y=100) """主要按鈕及顯示效果""" '''返回按鈕''' self.btn1 = tk.Button(self.f2, text="返回", fg='#224b8f', font=('黑體', 18), bg="wheat", command=self.cfl_backToRoot) self.btn1.place(x=150, y=500) '''選擇雙人游戲按鈕''' self.lab03 = tk.Label(self.f2, text="人數限定:2", bg="#d9d6c3", fg='black', font=('黑體', 18)) self.lab03.place(x=150, y=300) self.btn2 = tk.Button(self.f2, text="雙人骰子", bg="wheat", font=('黑體', 18), command=self.cfl_createDoubleDiceGame) self.btn2.place(x=150, y=200) '''選擇多人游戲按鈕''' self.entry03 = tk.Entry(self.f2, bg="white", font=('黑體', 18)) self.entry03.place(x=350, y=260) self.lab04 = tk.Label(self.f2, bg="#d9d6c3", fg='black', text="人數限定:3~10(在上方輸入框輸入人數)", font=('黑體', 18)) self.lab04.place(x=350, y=300) def cmd1(): # 內置方法, 主要用于實現按鈕的功能 self.cfl_Multiplayer(self.entry03.get()) self.btn2 = tk.Button(self.f2, bg="wheat", text="多人骰子", font=('黑體', 18), command=cmd1) self.btn2.place(x=350, y=200) def cfl_startSohaGame(self): # 游戲開始 self.cfl_createContentWidge() def cfl_gameOver(self): # 游戲結束 self.root.quit() def cfl_backToRoot(self): # 返回開始游戲界面 self.cfl_createStartWidget() self.f2.destroy() def cfl_createDoubleDiceGame(self): # 雙人骰子游戲界面布局 if self.f2: self.f2.destroy() self.f2 = tk.Frame(self.root) self.f2['width'] = 800 self.f2['height'] = 600 self.lab01 = tk.Label(self.f2, font=('黑體', 14), text="@玩家1,請在下方輸入框輸入您的名字:") self.lab01.place(x=140, y=40) self.entry01 = tk.Entry(self.f2, width=40) self.entry01.place(x=200, y=90) self.lab02 = tk.Label(self.f2, font=('黑體', 14), text="@玩家2,請在下方輸入框輸入您的名字:") self.lab02.place(x=140, y=160) self.entry02 = tk.Entry(self.f2, width=40) self.entry02.place(x=200, y=210) def cmd1(): '''獲取骰子''' self.soha.zmx_getPlayerDiceList() def cmd2(): '''獲取玩家姓名''' self.soha.llq_getPlayerName(self.entry01.get(), self.entry02.get()) def cmd3(): '''判斷玩家輸贏''' if len(self.soha.playerList) == 0: tk.messagebox.showinfo("錯誤提示", "名字不能為空!!!") elif self.soha.playerList[0].Dice_List == []: tk.messagebox.showinfo("錯誤提示", "請為每個玩家獲取骰子數據!!!") else: self.cfl_outputDoubleWinner() self.soha.playerList = [] # 清空數據 self.btn2 = tk.Button(self.f2, font=('黑體', 14), text="提交姓名", command=cmd2) self.btn2.place(x=180, y=280) self.btn6 = tk.Button(self.f2, font=('黑體', 14), text="獲取骰子", command=cmd1) self.btn6.place(x=360, y=280) self.btn3 = tk.Button(self.f2, font=('黑體', 14), text="獲取輸贏結果", command=cmd3) self.btn3.place(x=360, y=350) self.btn4 = tk.Button(self.f2, font=('黑體', 14), text="返回", command=self.cfl_createContentWidge) self.btn4.place(x=180, y=350) self.f2.place(x=20, y=0) self.f2.place(x=0, y=0) def cfl_outputDoubleWinner(self): result = self.soha.mhl_judgeTwoVictory() if self.f2: self.f2.destroy() self.f2 = tk.Frame(self.root) self.f2['width'] = 800 self.f2['height'] = 600 self.lab01 = tk.Label(self.f2, font=('黑體', 16), text="雙人骰子本輪結果展示如下:") self.lab01.place(x=100, y=80) self.lab01 = tk.Label(self.f2, font=('黑體', 16), text=result) self.lab01.place(x=100, y=150) def cmd1(): # 返回功能實現函數 self.cfl_createDoubleDiceGame() self.soha.playerList = [] # 清空數據 self.btn1 = tk.Button(self.f2,font=('黑體', 16), text="返回", command=cmd1) self.btn1.place(x=100, y=500) self.f2.place(x=0, y=0) def cfl_Multiplayer(self, strnum): # 多人游戲骰子 numlist = {'3', '4', '5', '6', '7', '8', '9', '10'} if strnum not in numlist: tkinter.messagebox.showinfo("錯誤提示", "輸入錯誤或未輸入,請重新輸入!") else: num = int(strnum) if self.f2: self.f2.destroy() self.f2 = tk.Frame(self.root) self.f2['width'] = 800 self.f2['height'] = 600 self.labs = [] self.entrys = [] if num <= 5: for i in range(0, num): p = tk.Label(self.f2, font=('黑體', 12), text="@玩家"+str(i+1)+":請在下邊輸入框輸入您的名字:") p.place(x=100, y=80 * i + 20) e = tk.Entry(self.f2, width=25) e.place(x=120, y=80 * i + 60) self.labs.append(p) self.entrys.append(e) else: for i in range(0, num): p = tk.Label(self.f2, font=('黑體', 10), text="@玩家"+str(i+1)+":請在右邊輸入框輸入您的名字:") p.place(x=100, y=50 * i + 20) e = tk.Entry(self.f2, width=25) e.place(x=370, y=50 * i + 20) self.labs.append(p) self.entrys.append(e) def cmd1(): '''獲取骰子''' self.soha.zmx_getPlayersDiceList(num) def cmd2(): '''獲取多人玩家姓名''' namelist = [] for i in range(0, num): p = self.entrys[i].get() namelist.append(p) self.soha.llq_getPlayersName(num, namelist) def cmd3(): '''判斷多人玩家輸贏''' if len(self.soha.playerList) == 0: tk.messagebox.showinfo("錯誤提示", "名字不能為空!!!") elif self.soha.playerList[0].Dice_List == []: tk.messagebox.showinfo("錯誤提示", "請為每個玩家獲取骰子數據!!!") else: self.cfl_ouputMultipleWinner(num) self.soha.playerList = [] self.btn2 = tk.Button(self.f2, font=('黑體', 14), text="提交姓名", command=cmd2) self.btn2.place(x=180, y=500) self.btn6 = tk.Button(self.f2, font=('黑體', 14), text="獲取骰子", command=cmd1) self.btn6.place(x=360, y=500) self.btn3 = tk.Button(self.f2, font=('黑體', 14), text="獲取骰子輸贏排名", command=cmd3) self.btn3.place(x=360, y=550) self.btn4 = tk.Button(self.f2, font=('黑體', 14), text="返回", command=self.cfl_createContentWidge) self.btn4.place(x=180, y=550) self.f2.place(x=0, y=0) def cfl_ouputMultipleWinner(self, num): # 多人骰子游戲大小排名輸出界面 result = self.soha.mhl_judgeMultipleWinner(num) strResult = self.soha.mhl_getPlayersListData() tkinter.messagebox.showinfo("恭喜", "@"+result[0].name+",恭喜您,力壓眾強,獲得了本輪游戲的第一!") if self.f2: self.f2.destroy() self.f2 = tk.Frame(self.root) self.f2['width'] = 800 self.f2['height'] = 600 self.lab01 = tk.Label(self.f2, font=('黑體', 16), text="多人骰子大小排名結果如下") self.lab01.place(x=100,y=80) self.labresult = tk.Label(self.f2, font=('黑體', 14), text=strResult) self.labresult.place(x=100,y=120) def cmd1(): # 返回功能實現函數 strNum = str(num) self.cfl_Multiplayer(strNum) self.soha.playerList = [] # 清空數據 self.btn1 = tk.Button(self.f2,font=('黑體', 16), text="返回", command=cmd1) self.btn1.place(x=100, y=500) self.f2.place(x=0, y=0)
感謝各位的閱讀,以上就是“基于Python如何實現骰子小游戲”的內容了,經過本文的學習后,相信大家對基于Python如何實現骰子小游戲這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。