使用Python制作一个简单的游戏

使用Python中的tkinter模块制作一款文字解密游戏。需要使用tkinter中的label模块显示文字解密的提示信息和玩家解密是否成功的反馈信息;使用tkinter中的button模块供玩家点击以获取提示信息和提交答案;使用tkinter的entry模块供玩家写出自己解密的答案。以下是代码:

import Tkinter as tk  //引入tkinter模块


root=tk.Tk()  //建立具体的实例


root.title('Who is the criminal?')  //将此实例命名


label1=tk.Label(root,text="There are 3 criminal suspects (A,B,C).Please choose the crimianl(s).")
label1.pack()    //使用label模块写出本次解密的问题


def info1():    //构建第一个button模块触发的函数
  global label2
  label2=tk.Label(root,text="A can not lie.")   //如果点击第一个button,则显示第一个提示信息。
  label2.pack()


def delete1():   //构建第二个button模块触发的函数
  label2.pack_forget()  //触发第二个button会删除第一个button触发的提示信息。


//按照刚才的构建过程再构建2组这样的button按钮
def info2():
  global label3
  label3=tk.Label(root,text="B is the victim's son.")
  label3.pack()


def delete2():
  label3.pack_forget()


def info3():
  global label4
  label4=tk.Label(root,text="C thinks that B is criminal.")
  label4.pack()


def delete3():
  label4.pack_forget()
//2组按钮构建完毕


def answer():   //构建一个button触发函数判断玩家解密是否正确
  ans=entry.get()
  if ans=='A':
    label5=tk.Label(root,text='You are right!')
  if ans=='a':
    label5=tk.Label(root,text='You are right!')
  else:
    label5=tk.Label(root,text='You are wrong!')
  label5.pack()


//依次将提示信息button的触发函数导入到button中
button1=tk.Button(root,text="Show inforamtion 1",command=info1)
button1.pack()


button2=tk.Button(root,text="Hide information 1",command=delete1)
button2.pack()


button3=tk.Button(root,text="Show information 2",command=info2)
button3.pack()


button4=tk.Button(root,text="Hide information 2",command=delete2)
button4.pack()


button5=tk.Button(root,text="Show information 3",command=info3)
button5.pack()


button6=tk.Button(root,text="Hide information 3",command=delete3)
button6.pack()
//导入button触发函数完毕


label6=tk.Label(root,text="Show your answer.")  //使用label模块提示玩家输入自己的答案
label6.pack()


entry=tk.Entry(root,text=None)   //使用entry模块为玩家提供答题板
entry.pack()


button7=tk.Button(root,text='Submit',command=answer)  //使用button让玩家提交信息,并判断玩家提交的答案是否正确
button7.pack()


root.mainloop()

相关链接:

使用python制作一个日期序号计算器

使用python制作留言板

使用python进行简单的二分法操作

我们的公众号:认知无线电

我们的网站:www.lolplayer.club

我们的Github:Yue-Xiong

欢迎关注和指正

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月丶匈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值