猜数字easygui纯图形界面版(仅用鼠标可玩)
本帖最后由 txxcat 于 2020-3-20 11:03 编辑#作为写的第一个游戏,一直想写成图形版,现在开始学GUI终于可以改造了,作业的要求还是要输入答案,不仅不符合图形界面的玩法,而且要防止输入错误字符而写入异常处理代码,现在改写成纯按钮选择,可以节省不少代码,而且不容易出错。
#guessnumber_easygui_0.1
import random
import easygui as g
import sys
while True:
secret = random.randint(1,10)
title='数字小游戏'
msg='不妨猜一下小甲鱼现在心里想的是哪个数字:'
choices=('1', '2', '3','4','5','6','7','8','9','10')
times=3
while True:
temp=g.buttonbox(msg=msg, title=title, choices=choices, image=None, images=None, default_choice=None, cancel_choice=None, callback=None, run=True)
if type(temp)==str: #如果选择按钮就转换,选择关闭窗口或按ESC就退出
guess = int(temp)
else:
sys.exit(0)
times-=1
if times==0 and guess!=secret:
if g.ccbox("三振出局,你没有机会了,正确答案是"+str(secret)+"!\n你想重新开始小游戏吗?"):
break
else:
sys.exit(0)
if guess>secret:
g.msgbox("哎呀,猜大了,你还有"+str(times)+"次机会,请重新选择吧!")
elif guess<secret:
g.msgbox("哎呀,猜小了,你还有"+str(times)+"次机会,请重新选择吧!")
else:
if g.ccbox("我草,你是小甲鱼心里的蛔虫吗?!\n哼,猜中了也没有奖励!\n你想重新开始小游戏吗?"):
break
else:
sys.exit(0)
#第二版修改了一下,增加了一个函数,节约了代码量,去掉了猜错的提示窗口,直接在主窗口提示,可以少点一次鼠标,作为游戏也流畅了些^-^
#guessnumber_easygui_0.2
import random
import easygui as g
import sys
def restartgame(msg):
if g.ccbox(msg):
return True
else:
sys.exit(0)
while True:
secret = random.randint(1,10)
title='数字小游戏'
msg='不妨猜一下小甲鱼现在心里想的是哪个数字,你有3次机会:'
choices=('1', '2', '3','4','5','6','7','8','9','10')
times=3
while True:
temp=g.buttonbox(msg=msg, title=title, choices=choices, image=None, default_choice=None, cancel_choice=None, callback=None, run=True)
if type(temp)==str: #如果选择按钮就转换,选择关闭窗口或按ESC就退出
guess = int(temp)
else:
sys.exit(0)
times-=1
if times==0 and guess!=secret:
if restartgame("三振出局,你没有机会了,正确答案是"+str(secret)+"!\n你想重新开始小游戏吗?"):
break
if guess>secret:
msg="哎呀,猜大了,你还有"+str(times)+"次机会,请重新选择吧!"
elif guess<secret:
msg="哎呀,猜小了,你还有"+str(times)+"次机会,请重新选择吧!"
else:
if restartgame("我草,你是小甲鱼心里的蛔虫吗?!\n哼,猜中了也没有奖励!\n你想重新开始小游戏吗?"):
break
第三版,弃用os.exit()退出程序;改用更合理的indexbox,不用转换字符为整数了;理顺判断逻辑,减少代码量,去掉函数。
#guessnumber_easygui_0.3
import random
import easygui as g
rtmm=False
while True:
if rtmm:
break
secret = random.randint(1,10)
title='数字小游戏'
msg='不妨猜一下小甲鱼现在心里想的是哪个数字,你有3次机会:'
choices=('1', '2', '3','4','5','6','7','8','9','10')
times=3
while True:
temp=g.indexbox(msg=msg, title=title, choices=choices)
if temp==None:
rtmm=True
break
else:
guess =temp+1
times-=1
if guess==secret:
msg="什么!猜对了!你是小甲鱼心里的蛔虫吗?!\n哼,猜中了也没有奖励!\n你想重新开始小游戏吗?"
elif times==0:
msg="三振出局,你没有机会了,正确答案是【 %d 】!\n你想重新开始小游戏吗?" % secret
elif guess>secret:
msg="哎呀,猜大了,你还有"+str(times)+"次机会,请重新选择吧!"
continue
elif guess<secret:
msg="哎呀,猜小了,你还有"+str(times)+"次机会,请重新选择吧!"
continue
if not g.ccbox(msg):
rtmm=True
break
easygui是什么?我在运行的时候出错了 easygui是不是Python的一个函数哈,这个需要用pip来安装才能使用啊 easygui是个入门级的GUI库,需要安装才能进行,不过我在deepin下没有安装成功,你可以试试看:
pip install easygui
或者
pip3 install easygui buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)
Display a msg, a title, and a set of buttons.
The buttons are defined by the members of the choices list.
Return the text of the button that the user selected.
和你这个程序写的有点差别,我在执行的是总是保报错,
File "E:\python_code\game.py", line 12, in <module>
temp=g.buttonbox(msg=msg, title=title, choices=choices, image=None,images=None,default_choice=None, cancel_choice=None, callback=None, run=True)
TypeError: buttonbox() got an unexpected keyword argument 'images'
Xwj_xt 发表于 2020-3-19 18:15
buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)
...
你在deepin下面安装好了easygui吗?可能版本不同,你的buttonbox的参数里不支持images插入图片,把image=None这段删除看看。 Xwj_xt 发表于 2020-3-19 18:15
buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)
...
关于Easygui的帖子,小甲鱼的这篇是很全面的了:https://fishc.com.cn/thread-46069-1-1.html,不过要鱼币打开,如果没有的话百度一下,头几页几乎一半都是搬运小甲鱼的,我就不发外链了。 txxcat 发表于 2020-3-19 20:11
你在deepin下面安装好了easygui吗?可能版本不同,你的buttonbox的参数里不支持images插入图片,把image= ...
我在win系统下装了Python3.8.1,但是一运行就报错,你这个程序运行没问题吗? Xwj_xt 发表于 2020-3-20 09:42
我在win系统下装了Python3.8.1,但是一运行就报错,你这个程序运行没问题吗?
我运行easygui其他程序还是可以的,就是感觉你这个temp=g.buttonbox(msg=msg, title=title, choices=choices, image=None,images=None,default_choice=None, cancel_choice=None, callback=None, run=True),不知道怎么解释 Xwj_xt 发表于 2020-3-20 09:43
我运行easygui其他程序还是可以的,就是感觉你这个temp=g.buttonbox(msg=msg, title=title, choices=choi ...
#我把你的程序修改好了,下面可以执行了,谢谢!
#guessnumber_easygui_0.1
import random
import easygui as g
import sys
while True:
secret = random.randint(1,10)
title='数字小游戏'
msg='不妨猜一下小甲鱼现在心里想的是哪个数字:'
choices=('1', '2', '3','4','5','6','7','8','9','10')
times=3
while True:
temp=g.buttonbox(msg=msg, title=title, choices=choices, image=None,root=None)
if type(temp)==str: #如果选择按钮就转换,选择关闭窗口或按ESC就退出
guess = int(temp)
else:
sys.exit(0)
times-=1
if times==0 and guess!=secret:
if g.ccbox("三振出局,你没有机会了,正确答案是"+str(secret)+"!\n你想重新开始小游戏吗?"):
break
else:
sys.exit(0)
if guess>secret:
g.msgbox("哎呀,猜大了,你还有"+str(times)+"次机会,请重新选择吧!")
elif guess<secret:
g.msgbox("哎呀,猜小了,你还有"+str(times)+"次机会,请重新选择吧!")
else:
if g.ccbox("我草,你是小甲鱼心里的蛔虫吗?!\n哼,猜中了也没有奖励!\n你想重新开始小游戏吗?"):
break
else:
sys.exit(0)
页:
[1]