链接: link
本程序在链接程序中基础上,添加了人机交互界面,使得游戏更加友好,可玩性更高,并打包成.exe文件。如有需求也可以连接数据库进行数据库储存。
from random import *
#from pymongo import MongoClient
from tkinter import *
from tkinter import messagebox
"""存放玩家数据的字典"""
Users = {}
class GFGame():
"""GFGame的类"""
def __init__(self,user,difficulty_state=7):
"""initial for GFGame"""
self.user = user
self.difficulty_state = difficulty_state
#根据困难状态标志位在困难字典中寻找,并确定猜测次数和所在范围的答案。若不存在,默认简单模式。
self.times = self.difficulty_state
self.answer = randint(0,2 ** difficulty_state)
self.range_L = 0
self.range_U = 2 ** difficulty_state
self.flag = 0
#读取玩家历史数据。若无该玩家数据,新建玩家数据。
if Users.get(user,0) == 0:
self.score = 0
info = {self.user:{'score':self.score,
'answer':[self.answer],
'guess':[],},}
Users.update(info)
else:
Users[self.user]['answer'].append(self.answer)
pass
pass
def Guess(self,user_guess):
"""Guess for the answer of GFGame"""
self.user_answer = user_guess
#记录玩家猜测的数据
Users[self.user]['guess'].append(self.user_answer)
pass
def Prompt(self):
"""Prompt about answer for user"""
if self.user_answer == self.answer:
self.flag = 1
self.times -= 0
elif self.user_answer > self.answer:
self.range_U=self.user_answer
self.times -= 1
else:
self.range_L=self.user_answer
self.times -= 1
pass
pass
def Judge(self,window):
"""Judge about user'answer """
if self.times > 0 and self.flag == 1:
#答对:根据剩余猜测次数加法
messagebox.showinfo('Win',f'挑战成功!\n+{self.times}分')
Users[self.user]['score'] += self.times
#每一次游戏历史猜测的分隔符
Users[self.user]['guess'].append('|')
window.destroy()
elif self.times ==0 and self.flag == 0:
#答错:给出答案数字
messagebox.showinfo('T_T',f'挑战失败\n正确答案为:{self.answer}')
#每一次游戏历史猜测的分隔符
Users[self.user]['guess'].append('|')
window.destroy()
else:
None
pass
pass
class WindowforGame(Frame):
"""docstring for WindowforGame"""
def __init__(self, master=None):
super(WindowforGame, self).__init__()
self.master = master
self.pack()
self.createWidgets()
pass
def createWidgets(self):
#root界面控件布置
self.lab01=Label(self,text='玩家:')
self.lab01.grid(row=0,column=0,padx=10,pady=10)
self.entry01=Entry(self)
self.entry01.grid(row=0,column=1,padx=10,pady=10)
self.difficulty_state=IntVar()
self.rabtn01=Radiobutton(self,text='新手',variable=self.difficulty_state,value=7)
self.rabtn01.grid(row=1,column=0,columnspan=2,padx=10,pady=10)
self.rabtn02=Radiobutton(self,text='基础',variable=self.difficulty_state,value=8)
self.rabtn02.grid(row=2,column=0,columnspan=2,padx=10,pady=10)
self.rabtn03=Radiobutton(self,text='中等',variable=self.difficulty_state,value=9)
self.rabtn03.grid(row=3,column=0,columnspan=2,padx=10,pady=10)
self.rabtn04=Radiobutton(self,text='困难',variable=self.difficulty_state,value=10)
self.rabtn04.grid(row=4,column=0,columnspan=2,padx=10,pady=10)
self.btn01=Button(self,text='开始',heigh=2,width=8,command=self.begin)
self.btn01.grid(row=5,column=0,padx=5,pady=10)
self.btn02=Button(self,text='历史记录',heigh=2,width=8,command=self.history)
self.btn02.grid(row=5,column=1,padx=5,pady=10)
self.btn03=Button(self,text='结束',heigh=2,width=8,command=self.over)
self.btn03.grid(row=5,column=2,padx=5,pady=10)
pass
def begin(self):
'''游戏开始'''
#获取输入框中信息
user=self.entry01.get()
difficulty_state=self.difficulty_state.get()
user_1 = GFGame(user,difficulty_state)
#确认按键对应事件
def sure():
guess=entry01.get()
#保证输入为数值
try:
#将用户输入类型化为数值
guess=int(guess)
user_1.Guess(guess)
user_1.Prompt()
#更新界面信息
lab02['text']=f'答案在:{user_1.range_L}~{user_1.range_U}'
lab03['text']=f'还剩{user_1.times}次机会 '
gamewindow.update()
user_1.Judge(gamewindow)
except:
messagebox.showinfo('数值有误','请重新输入数值!')
pass
#创建一个猜数字的交互界面
gamewindow=Tk()
gamewindow.title('GuessFigure')
gamewindow.geometry('200x300+550+250')
#设置界面控件
lab01=Label(gamewindow,text=f'玩家:{user}')
lab01.grid(row=0,columnspan=3,padx=10,pady=10)
lab02=Label(gamewindow,text=f'答案在:{user_1.range_L}~{user_1.range_U}')
lab02.grid(row=1,columnspan=3,padx=10,pady=10)
lab03=Label(gamewindow,text=f'还剩{user_1.times}次机会 ')
lab03.grid(row=2,columnspan=3,padx=10,pady=10)
entry01=Entry(gamewindow)
entry01.grid(row=3,columnspan=3,padx=10,pady=10)
btn01=Button(gamewindow,text='确认',heigh=2,width=8,command=sure)
btn01.grid(row=4,column=0,padx=5,pady=10)
btn02=Button(gamewindow,text='结束',heigh=2,width=8,command=gamewindow.quit)
btn02.grid(row=4,column=1,padx=5,pady=10)
gamewindow.mainloop()
def history(self):
#查看玩家数据并存档
user=self.entry01.get()
if Users.get(user,0) == 0:
messagebox.showinfo(f'History of {user}',f'There is no information about {user}')
else:
hiswindow=Tk()
hiswindow.title('History')
hiswindow.geometry('250x300+500+250')
lab01=Label(hiswindow,text=f'玩家:{user}')
lab01.grid(row=0,column=0,padx=10,pady=10)
lab02=Label(hiswindow,text=f"得分:{Users[user]['score']}")
lab02.grid(row=1,column=0,padx=10,pady=10)
lab03=Label(hiswindow,text=f"答案:{Users[user]['answer']}")
lab03.grid(row=2,column=0,padx=10,pady=10)
lab03=Label(hiswindow,text=f"历史猜测:{Users[user]['guess']}")
lab03.grid(row=3,column=0,padx=10,pady=10)
btn04=Button(hiswindow,text='返回',command=hiswindow.destroy)
btn04.grid(row=4,column=0,padx=10,pady=10)
#存档到database['GuessFigure']
#date={'玩家':user,'得分':Users[user]['score'],'答案':Users[user]['answer'],'历史猜测':Users[user]['guess']}
#collection.insert_one(date)
pass
def over(self):
#结束游戏
self.quit()
pass
if __name__ == '__main__':
#数据库
'''client = MongoClient()
database = client['Game']
collection = database['GuessFigure']'''
#输入用户名
root=Tk()
root.title('猜数字游戏')
root.geometry('500x400+400+200')
frame=WindowforGame(root)
root.mainloop()
1、win+r输入cmd打开命令运行框
2、输入 e:进入python游戏程序所在e盘
3、cd +python游戏程序所在文件夹路径
4、在该文件夹路径下安装Pyintaller,指令如下
pip installer Pyintaller
5、输入指令Pyinstaller -F -w -i tubiao.ico GuessFigure.py
其中:tubiao.ico为.exe文件的图标(可在网上找通过格式工厂转换格式),GuessFigure.py为该程序名称,图标和程序应存放在同一文件夹下。
1、游戏开始界面,可输入玩家名,选择游戏难易程度,并查看当前玩家历史游戏记录。因未连接数据库,每一次关闭游戏无法保存,只记录当前游戏多名玩家数据。
新手:0~128,有7次机会
基础:0~256,有8次机会
中等:0~512,有9次机会
困难:0~1024,有10次机会
2、猜数字界面:答案范围和剩余次数动态刷新。
3、答对加分提示,加分多少与剩余次数相等。
4、查询三下伍除二的历史游戏记录。'|'为一次游戏的分割符。