Python程序设计之24点小游戏

172 阅读3分钟
参与拿奖:本文已参与「新人创作礼」活动,一起开启掘金创作之路
PS:代码文末自取

1.运行效果

屏幕截图(189).png

2.导入相关的包

import tkinter
import tkinter.messagebox
import tkinter.ttk
import random

3.创建窗口


#初始化窗体
root=tkinter.Tk()

#初始化窗口大小
root["width"]=1000
root["height"]=1000

#初始化窗口标题
root.title("Points24")

# 用来记录用户答对题目数量
count=0

# 用来记录总的题目数量
total=1

# 用来判断是否添加了元素
isnext=True

4.设置随机数

# 返回四个字符和一个字典
def randNumber(n):
    cards=[str(i) for i in range(2,11)]
    cards.insert(0,'A')
    cards.extend(['J','Q','K'])
    value=[i for i in range(1,14)]
    dic=dict(zip(cards,value))
    tempCards=cards*4
    showvalue=random.sample(tempCards,n)
    return showvalue,dic

5.显示题目以及答题框


# 展示所获得的结果
showvalue,dic=randNumber(4)
#设置用户名标签
labelFirstValue=tkinter.Label(root,text='init value: '+' '.join(showvalue),justify=tkinter.RIGHT,width=100)
#设置位置
labelFirstValue.place(x=150,y=100,width=200,height=50)

#创建文本框初始值/两种初始化方式
varName=tkinter.StringVar(value='')
#设置用户名标签
labelName=tkinter.Label(root,text="input answer:",justify=tkinter.RIGHT,width=100)
#设置位置
labelName.place(x=125,y=200,width=100,height=50)
#设置文本框
entryName=tkinter.Entry(root,width=100,textvariable=varName)
#设置文本框位置
entryName.place(x=250,y=200,width=100,height=25)

6.验证答案

def Check():
    global count
    global entryName
    global total
    global isnext
    answer=entryName.get()
    target=False
    v=list(answer)
    for i in range(len(v)):
        if v[i] in ['A','J','K','Q']:
            v[i]=str(dic[v[i]])
    #print(v)
    #print("".join(v))
    if answer=='False':
        t = list(showvalue)
        for i in range(len(t)):
            if t[i] in ['A', 'J', 'K', 'Q']:
                t[i] = str(dic[t[i]])
        trueValue = [int(i) for i in t]
        if twentyfour(trueValue)==False:
            count+=1
            target=True
    else:
        if eval(''.join(v))==24:
            count+=1
            target=True

    if target==False:
        tkinter.messagebox.showerror(title='Error',message='The answer is error!')
    else:
        tkinter.messagebox.showinfo(title='OK',message='Congratulations!')
        if not isnext:
            count-=1
    isnext=False

#定义操作函数
buttonOk=tkinter.Button(root,text="Check",command=Check)
buttonOk.place(x=50,y=350,width=50,height=20)

7.重置答案

def Cancel():
    varName.set('')

buttonCancel=tkinter.Button(root,text = "Reset",command = Cancel)
buttonCancel.place(x=150,y=350,width=50,height=20)

8.下一题

# 下一题
def Next():
    global showvalue
    global varName
    global entryName
    global isnext
    isnext=True
    showvalue,dic=randNumber(4)
    # 设置用户名标签
    labelFirstValue = tkinter.Label(root, text='init value: ' + ' '.join(showvalue), justify=tkinter.RIGHT, width=100)
    # 设置位置
    labelFirstValue.place(x=150, y=100, width=200, height=50)

    global total
    total+=1
    # 创建文本框初始值/两种初始化方式
    varName = tkinter.StringVar(value='')
    # 设置用户名标签
    labelName = tkinter.Label(root, text="input answer:", justify=tkinter.RIGHT, width=100)
    # 设置位置
    labelName.place(x=125, y=200, width=100, height=50)
    # 设置文本框
    entryName = tkinter.Entry(root, width=100, textvariable=varName)
    # 设置文本框位置
    entryName.place(x=250, y=200, width=100, height=25)


buttonNext=tkinter.Button(root,text="Next",command=Next)
buttonNext.place(x=350,y=350,width=50,height=20)

9.显示答案


def Show():
    answer=showvalue
    target=False
    v=list(answer)
    #print(v)
    for i in range(len(v)):
        if v[i] in ['A','J','K','Q']:
            v[i]=str(dic[v[i]])
    #print(list(dic.keys()),v)
    trueValue=[int(i) for i in v]
    # print(trueValue)
    if twentyfour(trueValue) !=False:
        answer=twentyfour(trueValue)
        tkinter.messagebox.showinfo(title='OK', message=answer)
    else:
        tkinter.messagebox.showerror(title='Error', message='No answer!')


buttonOut=tkinter.Button(root,text="Show",command=Show)
buttonOut.place(x=450,y=350,width=50,height=20)

10.其他按钮

def Score():
    global total
    global count
    tkinter.messagebox.showinfo(title='Accuracy rate', message="total %d , accuracy %d  accuracy rate %.2f"%(total,count,count/total))
buttonScore=tkinter.Button(root,text="Score",command=Score)
buttonScore.place(x=550,y=350,width=50,height=20)

def Out():
    if tkinter.messagebox.showinfo(title="Tips",message="Are You sure!"):
        root.destroy()
buttonOut=tkinter.Button(root,text="Out",command=Out)
buttonOut.place(x=650,y=350,width=50,height=20)

11.main函数

if __name__=="__main__":
    #运行程序
    #print(random.sample([i for i in range(13)],4))
    root.mainloop()

12.代码链接:gihub仓库自取