用户:L0_0查看:0 回复:1 评论:0 创建时间:2021-03-25T21:33:35
【作品展示】

【作品介绍】
tkinter游戏
【作品源代码】
import random
from tkinter import Tk, Menu, Label, Entry, Button, messagebox
root = Tk()
root.title('猜数字')
answer = random.randint(1, 10)
times = 4
over = False
easy_num = 0
hard_num = 10
def check():
global over, times
if over:
messagebox.showerror('猜数字', '本局已结束。')
return
try:
times -= 1
if int(entry.get()) == answer:
messagebox.showinfo('猜数字', '答对了。')
over = True
else:
if times == 0:
messagebox.showerror('猜数字', '猜错了,正确答案是:' + str(answer))
over = True
elif int(entry.get()) > answer:
messagebox.showwarning('猜数字', '猜大了,还有' + str(times) + '次机会。')
else:
messagebox.showwarning('猜数字', '猜小了,还有' + str(times) + '次机会。')
except:
messagebox.showerror('猜数字', entry.get() + '不是数字')
times += 1
label = Label(root, width=36, text='输入1~10的一个数')
label.pack()
entry = Entry(root, width=36)
entry.pack()
Button(width=9, command=check, text='提交').pack()
def difficuity_set(easy_num, hard_num, times_count):
if messagebox.askquestion('更换难度', '此操作将会放弃你目前的游戏,是否继续?') == 'yes':
new_game(easy_num, hard_num, times_count)
def new_game(easy, hard, time):
global over, answer, times, easy_num, hard_num
over = False
easy_num = easy
hard_num = hard
answer = random.randint(easy, hard)
times = time
label['text'] = '输入' + str(easy_num) + '~' + str(hard_num) + '的一个数'
def reset():
global answer, times, over, easy_num, hard_num
if messagebox.askquestion('重置', '是否重置?') == 'yes':
easy_num = 0
hard_num = 10
over = False
times = 4
answer = random.randint(0, 10)
menubar = Menu(root)
setupmenu = Menu(menubar, tearoff=0)
difficuitymenu = Menu(setupmenu, tearoff=0)
difficuitymenu.add_command(label='1~10', command=lambda easy_num=1, hard_num=10, times_count=4: difficuity_set(easy_num, hard_num, times_count))
difficuitymenu.add_command(label='1~100', command=lambda easy_num=1, hard_num=100, times_count=7: difficuity_set(easy_num, hard_num, times_count))
difficuitymenu.add_command(label='1~1000', command=lambda easy_num=1, hard_num=1000, times_count=10: difficuity_set(easy_num, hard_num, times_count))
difficuitymenu.add_command(label='1~10000', command=lambda easy_num=1, hard_num=10000, times_count=12: difficuity_set(easy_num, hard_num, times_count))
setupmenu.add_cascade(label='更换难度', menu=difficuitymenu)
setupmenu.add_command(label='重置', command=reset)
setupmenu.add_separator()
setupmenu.add_command(label='退出', command=root.quit)
menubar.add_cascade(label='设置', menu=setupmenu)
menubar.add_command(label='新游戏', command=lambda easy=easy_num, hard=hard_num, time=times: new_game(easy, hard, time))
root.config(menu=menubar)
root.mainloop()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn