这个游戏有点难,我卡在第4关了,看看你能过几关【猜数字】
本帖最后由 dltest 于 2022-7-24 17:38 编辑# -*- coding:utf-8 -*-
# @FileName:myguestnumberUI.py
# @Time :2022/7/23 12:03
#@AuThor :dltest@52pojie
import time
import tkinter as tk
import tkinter.font as tkFont
import random
LOG_LINE_NUM = 0
class App:
def __init__(self, root):
self.initUI(root)
self.initGame()
self.initData()
def initGame(self):
self.module = True # 升级模式True 积分模式 False猜准即升级
self.bonus = 10# 游戏积分,初始为10
self.coins = 10# 猜数字次数,初始为10
self.level = 1# 游戏级别,初始为1
self.buycoins_temp = 0# 额外猜数次数,初始为0,未消耗永久有效
self.buycoins_forever = 0# 额外永久猜数次数,初始为0,永久有效
self.status_coinsforever = True # 额外永久猜数次数是否使用过
self.status_leveup = False #升级状态
self.burynumber =0
self.helpnumber = # 助猜数字
self.status_helpnumber = False
self.status_hint = True
self.hintmin = 1
self.hintmax = 100
self.setDataLabel()
self.GButton_submit['state'] = 'disabled'
self.GButton_hint['state'] = 'disabled'
self.GButton_buycoins_temp['state'] = 'disabled'
self.GButton_buycoins_forever['state'] = 'disabled'
self.GLineEdit_buycoins_forever['fg']="green"
self.GLineEdit_buycoins_temp['fg']="green"
global LOG_LINE_NUM
LOG_LINE_NUM = 0
self.GLineEdit_log.delete(1.0, 'end')
def setDataLabel(self):
self.set_bonus_lable(self.GLineEdit_bonus, self.bonus)
self.set_bonus_lable(self.GLineEdit_level, self.level)
self.set_bonus_lable(self.GLineEdit_coins, self.coins)
self.set_bonus_lable(self.GLineEdit_buycoins_forever, self.buycoins_forever)
self.set_bonus_lable(self.GLineEdit_buycoins_temp, self.buycoins_temp)
if self.status_hint:
self.GLabel_numrange['text'] = f'1-{self.level*100}'
else:
self.GLabel_numrange['text'] = f'{self.hintmin}-{self.hintmax}'
if self.status_helpnumber:
self.GLabel_helpnum['text'] = f'健康游戏忠告{self.helpnumber}'
else:
self.GLabel_helpnum['text'] = f'健康游戏忠告'
self.GLabel_burynumber['text'] = f"抵制不良游戏,拒绝盗版游戏。\n注意自我保护,谨防受骗上当。\n" \
f"适度游戏益脑,沉迷游戏伤身。\n合理安排时间,享受健康生活。"
def initUI(self,root):
#setting title
root.title("猜数字游戏dltest@52pojie")
#setting window size
width=600
height=500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
ft_entry = tkFont.Font(family='Times',size=12)
GLabel_770=tk.Label(root,text = "级别")
GLabel_770.place(x=30,y=10,width=32,height=30)
GLabel_975=tk.Label(root,text = "积分")
GLabel_975.place(x=190,y=10,width=44,height=31)
GLabel_917=tk.Label(root,text = "额外次数")
GLabel_917.place(x=350,y=10,width=73,height=30)
GLabel_30=tk.Label(root,text = "临时次数")
GLabel_30.place(x=350,y=50,width=73,height=34)
GLabel_37=tk.Label(root,text = "剩余猜测")
GLabel_37.place(x=360,y=90,width=55,height=30)
self.GButton_buycoins_forever=tk.Button(root,text = "10分买一次")
self.GButton_buycoins_forever.place(x=510,y=10,width=71,height=30)
self.GButton_buycoins_forever["command"] = self.GButton_buycoins_forever_command
self.GButton_buycoins_temp=tk.Button(root,text = "2分买一次")
self.GButton_buycoins_temp.place(x=510,y=50,width=71,height=30)
self.GButton_buycoins_temp["command"] = self.GButton_buycoins_temp_command
self.GLabel_result=tk.Label(root,text = "猜测结果")
self.GLabel_result.place(x=110,y=50,width=205,height=72)
self.GLineEdit_log=tk.Text(root) #"记录"
self.GLineEdit_log.place(x=10,y=130,width=408,height=360)
self.GLineEdit_numinput=tk.Entry(root,text = "输入框",justify= "center",font = ft_entry,validate='key',
validatecommand=(root.register(self.checkInput), '%P'))
self.GLineEdit_numinput.place(x=460,y=240,width=98,height=34)
self.GButton_submit=tk.Button(root,text = "提交")
self.GButton_submit["command"] = self.GButton_submit_command
self.GButton_submit.place(x=460,y=280,width=99,height=30)
self.GLabel_helpnum=tk.Label(root,text = "健康游戏忠告")
self.GLabel_helpnum.bind('<Button-1>', self.changeHelpStatus)
self.GLabel_helpnum.place(x=430,y=370,width=160,height=30)
self.GLabel_burynumber=tk.Label(root,text= "抵制不良游戏,拒绝盗版游戏。\n注意自我保护,谨防受骗上当。\n"
"适度游戏益脑,沉迷游戏伤身。\n合理安排时间,享受健康生活。")
ft = tkFont.Font(family='宋体',size=8)
self.GLabel_burynumber["font"] = ft
self.GLabel_burynumber["fg"] = "#5fb878"
self.GLabel_burynumber.bind('<Button-1>', self.GLabel_getburynumber)
self.GLabel_burynumber.place(x=430,y=400,width=171,height=75)
self.GButton_star=tk.Button(root,text= "开始游戏")
self.GButton_star.place(x=510,y=90,width=72,height=30)
self.GButton_star["command"] = self.GButton_star_command
GLabel_512=tk.Label(root,text = "本次数字区间")
GLabel_512.place(x=450,y=170,width=120,height=30)
self.GLabel_numrange=tk.Label(root,text = "1-100") #"1-100"
self.GLabel_numrange.place(x=450,y=200,width=122,height=30)
self.GLineEdit_level=tk.Entry(root,state='readonly',justify= "center",font = ft_entry) #"级别"
self.GLineEdit_level.place(x=70,y=10,width=90,height=30)
self.GLineEdit_bonus=tk.Entry(root ,justify= "center" ,font = ft_entry) #"积分"
self.GLineEdit_bonus.place(x=240,y=10,width=81,height=30)
self.GLineEdit_buycoins_forever=tk.Entry(root,justify= "center",font = ft_entry) #额外
self.GLineEdit_buycoins_forever.place(x=420,y=10,width=72,height=30)
self.GLineEdit_buycoins_temp=tk.Entry(root,justify= "center",font = ft_entry) #临时
self.GLineEdit_buycoins_temp.place(x=420,y=50,width=72,height=30)
self.GLineEdit_coins=tk.Entry(root,justify= "center",font = ft_entry) #剩余
self.GLineEdit_coins.place(x=420,y=90,width=72,height=30)
self.GButton_reset=tk.Button(root,text = "重玩")
self.GButton_reset.place(x=30,y=70,width=61,height=30)
self.GButton_reset["command"] = self.GButton_reset_command
self.GButton_hint=tk.Button(root,text = "5积分缩小范围1次")
self.GButton_hint.place(x=430,y=130,width=149,height=30)
self.GButton_hint["command"] = self.GButton_hint_command
def initData(self):
pass
def GButton_buycoins_forever_command(self):
if self.bonus >=20 and self.buycoins_forever<10:
self.bonus -= 10
self.buycoins_forever +=1
self.write_log_to_Text(f'购买成功,已为您增加永久次数:1')
if self.status_coinsforever:
self.write_log_to_Text(f'本局可以使用购买得永久次数。')
else:
self.write_log_to_Text(f'永久次数已部份使用,下局可用新购次数。')
else:
if self.bonus < 20:
self.write_log_to_Text(f'购买失败!请预留至少10点游戏积分!您目前游戏积分:{self.bonus}')
elif self.buycoins_forever >=10:
self.write_log_to_Text(f'购买失败!额外猜数次数存储上限为10次!')
self.setDataLabel()
def GButton_buycoins_temp_command(self):
if self.bonus >=12 :
self.bonus -= 2
self.buycoins_temp +=1
self.write_log_to_Text(f'购买成功,已为您增加临时次数:1')
else:
self.write_log_to_Text(f'购买失败!请预留至少10点游戏积分!您目前游戏积分:{self.bonus}')
self.setDataLabel()
def changeHelpStatus(self,event):
self.status_helpnumber = not self.status_helpnumber
self.setDataLabel()
def GLabel_getburynumber(self,event):
self.GLabel_burynumber['text']= self.burynumber
def GButton_submit_command(self):
if self.bonus<=0:
self.write_log_to_Text(f'您的游戏积分为0,GAME OVER!重玩请点击按钮【重玩】')
# self.write_log_to_Text('很遗憾!您没有猜对数字!埋藏的数字为:{input:3}')
return
input= self.GLineEdit_numinput.get()
self.GLineEdit_numinput.delete(0,'end')
x=0
if input:
try:
x = int(input)
except:
self.write_log_to_Text(f'请输入整数')
return
else:
self.write_log_to_Text(f'未输入数字')
return
if x > self.burynumber:
self.GLabel_result['fg']="#ff7800"
ft = tkFont.Font(family='Times',size=12)
self.GLabel_result["font"] = ft
self.GLabel_result['text']="大了"
self.write_log_to_Text(f'您猜的数[{input:>3}]太大了!扣除1点游戏积分,剩余游戏积分:{self.bonus-1}')
self.helpnumber= x
self.helpnumber= int ((self.helpnumber + self.helpnumber ) / 2)
elif x < self.burynumber:
self.GLabel_result['text']="小了"
self.write_log_to_Text(f'您猜的数[{input:>3}]太小了!扣除1点游戏积分,剩余游戏积分:{self.bonus-1}')
self.helpnumber= x
self.helpnumber=int ((self.helpnumber + self.helpnumber ) / 2)
else:
self.status_leveup = True
print("神啦!您居然猜对了")
self.GLabel_result['text']="猜对了"
self.write_log_to_Text(f'神啦!您居然猜对了!埋藏的数确实是[{input:>3}]。')
self.write_log_to_Text(f'奖励10点游戏积分,剩余游戏积分:{self.bonus+10}')
self.setGameStaus()
def setGameStaus(self):
self.bonus -= 1
if self.status_leveup:
self.bonus += 11
self.coins = 10
self.GLineEdit_buycoins_forever['fg']="green"
else:
self.coins -= 1
if self.coins == 0 and self.bonus >0 and not self.status_leveup:
if self.status_coinsforever and self.buycoins_forever >0 :
self.status_coinsforever = False
self.GLineEdit_buycoins_forever['fg']="red"
self.coins += self.buycoins_forever
self.write_log_to_Text(f'使用道具“主角光环”成功!额外增加[{self.buycoins_forever}]次猜测,请继续加油!')
elif self.buycoins_temp > 0:
self.coins += 1
self.buycoins_temp -= 1
self.write_log_to_Text(f'幸运加持!额外增加[{1}]次猜测,请继续加油!')
else:
self.write_log_to_Text(f'很遗憾!您没有猜对数字!埋藏的数字为:{self.burynumber:>3}')
self.write_log_to_Text(f'您的猜测次数为0,GAME OVER!重玩请点击按钮【重玩】')
self.GameOver()
self.uplevel()
if self.status_leveup:
self.GButton_star_command()
self.setDataLabel() #刷新显示
def GButton_hint_command(self):
if self.bonus >5 :
self.status_hint = False
self.bonus -= 5
min = random.randint(1, 25)
max = random.randint(1, 25)
hintmin = 1
if self.burynumber - self.hintmin>min :
hintmin = self.burynumber - min
else:
hintmin = self.burynumber -random.randint(1, (self.burynumber-self.hintmin))
hintmax= self.level*100
if self.hintmax - self.burynumber >max :
hintmax = self.burynumber + max
else:
hintmax = self.burynumber +random.randint(1, (self.hintmax - self.burynumber))
if hintmin < 0:
hintmin = random.randint(1, self.burynumber)
if hintmax > self.level*100:
hintmax = random.randint(self.burynumber, self.level*100)
self.hintmin = hintmin
self.hintmax = hintmax
self.write_log_to_Text(f'-----------消耗5点游戏积分获取埋藏数字区间提示 ------')
self.write_log_to_Text(f'埋藏的数字在「{self.hintmin}~{self.hintmax}」之间,剩余游戏积分:{self.bonus}')
else:
self.write_log_to_Text(f'购买失败!请预留至少5点游戏积分!您目前游戏积分:{self.bonus}')
self.setDataLabel()
def GButton_star_command(self):
self.burynumber = self.BuryNumber(self.level)
self.GButton_star['state'] = 'disabled'
self.GButton_submit['state'] = 'active'
self.GButton_hint['state'] = 'active'
self.GButton_buycoins_temp['state'] = 'active'
self.GButton_buycoins_forever['state'] = 'active'
self.status_hint = True
self.status_leveup = False
self.status_coinsforever = True
print(self.level)
self.helpnumber = self.level*100
self.helpnumber = 1
self.helpnumber = int ((self.helpnumber + self.helpnumber ) / 2)
print(self.helpnumber)
def GButton_reset_command(self):
self.bonus = 10
self.level = 1
self.set_bonus_lable(self.GLineEdit_bonus,self.bonus)
self.set_bonus_lable(self.GLineEdit_level,self.level)
self.GButton_star['state'] = 'active'
self.initGame()
self.setDataLabel()
# 根据游戏级别埋藏数字
def BuryNumber(self,level):
return random.randint(1, level * 100)
def checkInput(self,char):
if (char.isdigit() or char == "") :
return True
else:
return False
def write_log_to_Text(self, logmsg):
global LOG_LINE_NUM
logmsg_in =str(logmsg) + "\n"# 换行
if LOG_LINE_NUM <= 24:
self.GLineEdit_log.insert('end', logmsg_in)
LOG_LINE_NUM = LOG_LINE_NUM + 1
else:
self.GLineEdit_log.delete(1.0, 2.0)
self.GLineEdit_log.insert('end', logmsg_in)
def set_bonus_lable(self,_Edit,value):
_Edit.config(state='normal')
_Edit.delete(0,'end')
_Edit.insert(0,f"{value}")
_Edit.config(state='readonly')
def GameOver(self):
self.GButton_submit['state'] = 'disabled'
#根据游戏积分提升游戏级别
def uplevel(self):
level = self.level
if self.status_leveup andnot self.module:
level += 1
else:
level = self.bonus//10+1
if level >self.level:
self.level = level
self.write_log_to_Text(f'-----------游戏难度升级至:{self.level}------')
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
是对该作者代码加了一个UI和一些调整。
https://www.52pojie.cn/thread-1649244-1-1.html
成品exe下载
下载:https://wwz.lanzoub.com/iFoZy08c1caj 密码:8nyd 哈哈哈楼主还给我们提供了参考答案 点一下那个健康游戏提示就可以看了 QAQ太"简单"辣 ChiXun 发表于 2022-7-25 09:55
哈哈哈楼主还给我们提供了参考答案 点一下那个健康游戏提示就可以看了 QAQ太"简单"辣
聪明的人已经发现一个彩蛋了 没啥难度,用二分法玩到第五关,没耐心玩下去了 代码看上去不是很复杂,很赞 从代码到成品需要什么? 这个可以袄{:1_921:}
代码整洁好评 已经把手机砸了!谢谢 有点像数字炸弹,一直对半砍就好了 学习一下,学习。