猫史档案馆


【Python作品分享】猜数字大游戏【作品秀】

用户:庄nnnn额庄nnnn额查看:0 回复:0 评论:0 创建时间:2021-07-16T15:45:49


【作品展示】

center_image

 

【作品介绍】

旧版网址喵o.cn/community/304393

bug已解决,欢迎大家来玩大游戏!

什么界面、海龟都是浮云,tkinter高级GUI永远的神!

 

【作品源代码】

import random
import sys
import tkinter
from tkinter.messagebox import askokcancel,askyesno,showinfo,showwarning
from tkinter.font import Font,NORMAL,BOLD
from tkinter.simpledialog import askinteger
root=tkinter.Tk()
showx=askokcancel('game','是否开始游戏?')
def quit_(event):
    showx=showinfo('game','游戏结束。')
    if showx:
        sys.exit()
if showx:
    def rule(event):
        def ask_it(event):
            Anumber=random.randint(1,16384)
            Xround=0
            want=False
            for important in range(20):
                while True:
                    answers=askinteger('game','请输入答案:')
                    if answers==None:
                        x=askyesno('game','是否退出?')
                        if x:
                            quit_(event)
                        else:
                            continue
                    else:
                        break
                if answers!=Anumber:
                    if answers>Anumber:
                        x=showwarning('game','大了!')
                    else:
                        x=showwarning('game','小了!')
                    Xround+=1
                    if x:
                        continue
                else:
                    x=showinfo('game','对了!')
                    Xround+=1
                    want=True
                    if x:
                        break
            f=Font(font=('Courier',20,'bold'))
            if want:
                w['text']='哇,你猜了'+str(Xround)+'次就猜对了!\n正确答案:'+str(Anumber)
            else:
                w['text']='你怎么还没猜对?'
                Anumber=None
            w.pack()
            m['text']='确定'
            m['background']='blue'
            m.pack()
            fb=Font(font=w['font']).copy()
            fb.config(weight=BOLD)
            m.config(font=fb)            
            m.bind('',quit_)
            global n
            n=tkinter.Button(root, text='再玩一局', command=root.destroy)
            n['background']='red'
            n.pack()
            n.config(font=fb)
            n.bind('',start)
            tkinter.mainloop()
        w['text']='从前,有一个数字,\nta在1~16384之间,\n你想猜对ta吗?\n不过,你只有20次机会哦!\n否则,ta会消失!'
        w.pack()
        m['text']='开始游戏'
        m['background']='red'
        m.pack()
        fb=Font(font=w['font']).copy()
        fb.config(weight=BOLD)
        m.config(font=fb)
        m.bind('',ask_it)
        tkinter.mainloop()
    def start(event):
        w['text']='猜数字大作战'
        w.pack()
        n.pack_forget()
        m['text']='查看规则'
        m['background']='yellow'
        m.pack()    
        fb=Font(font=w['font']).copy()
        fb.config(weight=BOLD)
        m.config(font=fb)
        m.bind('',rule)
        tkinter.mainloop()
    f=Font(font=('华文楷体',20,'bold'))
    w=tkinter.Label(root,text='猜数字大作战',font=f)
    w.pack()
    m=tkinter.Button(root,text='查看规则',command=root.destroy)
    m['background']='yellow'
    m.pack()
    fb=Font(font=w['font']).copy()
    fb.config(weight=BOLD)
    m.config(font=fb)
    m.bind('', rule)
    tkinter.mainloop()
else:
    quit_(None)

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页