import tkinter
import tkinter.messagebox
import math
import sys
import random
root = tkinter.Tk()
root.title('数字炸弹')
root.geometry('400x400')
n = num1=random.randint(1,100)
i=0
def cai_shu_zi():
global i
if Button1['text'] == "游戏结束":
sys.exit()
else:
try:
sz = ''.join(j for j in entryNum.get() if j in '0123456789')
int_cin = int(sz)
if i == 5 and int_cin != n:
Label2['text'] = "你已经猜了5次了,并且都没有猜中,本局是你的败北"
Button1['text'] = "游戏结束"
entryNum.delete(0, 'end')
elif int_cin < n:
Label2['text'] = "你输入的数比较小"
i += 1
Button1['text'] = "输入下一个数字"
entryNum.delete(0, 'end')
elif int_cin > n:
Label2['text'] = "你输入的数比较大"
i += 1
Button1['text'] = "输入下一个数字"
entryNum.delete(0, 'end')
elif int_cin == n:
Label2['text'] = "恭喜你猜对了"
Button1['text'] = "游戏结束"
entryNum.delete(0, 'end')
except ValueError:
tkinter.messagebox.showerror(title='Error', message='请输入数字')
label = tkinter.Label(root,text='Wellcome to our game!',font=(16))
label.place(x=10,y=40,height=30,width=250)
labelNum = tkinter.Label(root,text='请输入你猜的数字')
labelNum.place(x=10,y=120,height=30,width=150)
Label2 = tkinter.Label(root,text='游戏未开始')
Label2.place(x=10,y=160,height=30,width=150)
entryNum = tkinter.Entry(root,width=150,textvariable=n)
entryNum.place(x=150,y=120,height=30,width=80)
Button1 = tkinter.Button(root,text='确定',command=cai_shu_zi)
Button1.place(x=170,y=170,height=30,width=150)
root.mainloop()
用tkinter实现数字炸弹游戏
最新推荐文章于 2024-06-13 21:43:16 发布