|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
先看看代码:
- import random
- import easygui as eg
- def bbb():
- secret = random.randint(1, 10)
- msg = '不妨猜一下我现在心里想的数字(1~10):'
- title = '数字小游戏'
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- while True:
- if guess == secret:
- eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
- eg.msgbox('猜中了也没有奖励')
- break
- else:
- if guess > secret:
- eg.msgbox('大了大了')
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- else:
- eg.msgbox('小了小了')
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- 重试 = eg.buttonbox(msg='是否重来?', title='重试', choices=('是', '否'))
- if 重试 == '是':
- bbb()
- else:
- eg.msgbox('游戏结束')
- def aaa():
- eg.msgbox('嗨,欢迎进入第一个界面小游戏!')
- secret = random.randint(1, 10)
- msg = '不妨猜一下我现在心里想的数字(1~10):'
- title = '数字小游戏'
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- while True:
- if guess == secret:
- eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
- eg.msgbox('猜中了也没有奖励')
- break
- else:
- if guess > secret:
- eg.msgbox('大了大了')
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- else:
- eg.msgbox('小了小了')
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- 重试 = eg.buttonbox(msg= '是否重来?', title='重试', choices=('是','否'))
- if 重试 == '是':
- bbb()
- else:
- eg.msgbox('游戏结束')
- aaa()
复制代码
我是想在第一次游戏结束后选择重试后再次运行代码,但是重试时我不想再出现:eg.msgbox('嗨,欢迎进入第一个界面小游戏!')这句,
想了好久,只好用笨方法了。
可以通过在函数 aaa() 中添加一个参数来控制是否显示欢迎信息。这样,你就可以在重试时避免重复显示欢迎信息。下面是修改后的代码:
- import random
- import easygui as eg
- def game(show_welcome=True):
- if show_welcome:
- eg.msgbox('嗨,欢迎进入第一个界面小游戏!')
-
- secret = random.randint(1, 10)
- msg = '不妨猜一下我现在心里想的数字(1~10):'
- title = '数字小游戏'
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
-
- while True:
- if guess == secret:
- eg.msgbox('卧槽,你是我肚子里的蛔虫吗?')
- eg.msgbox('猜中了也没有奖励')
- break
- else:
- if guess > secret:
- eg.msgbox('大了大了')
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
- else:
- eg.msgbox('小了小了')
- guess = eg.integerbox(msg, title, lowerbound=1, upperbound=10)
-
- 重试 = eg.buttonbox(msg='是否重来?', title='重试', choices=('是', '否'))
- if 重试 == '是':
- game(show_welcome=False) # 重试时不显示欢迎信息
- else:
- eg.msgbox('游戏结束')
- game()
复制代码
现在,当你第一次运行游戏时,会显示欢迎信息。但是在重试时,将不会再显示欢迎信息。
|
|