这篇文章主要讲解了“基于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)