Python实现猜数字小游戏

作者:brief 时间:2022-10-09 18:38:12 

首先需求一共有五次猜测机会,在五次机会中才对就赢了,结束游戏,五次都猜错就输了,也结束游戏。首先先画个草图,这是我画的草图

Python实现猜数字小游戏

再根据草图编写一个窗口,一个Label,一个Entry,一个按钮,然后编写功能,将功能绑定函数


import random
from tkinter import *
NUM=random.randint(1,101)#随机数
time=[]#猜测次数
def Sstart():
   """
   先判断结果,在判断猜测数和随机数的大小关系,然后在判断结果
   :return:
   """
   global NUM,time
   if result() == None:
       if NUM == int(Vgue.get()):
           print("猜对了",NUM)
           time.append(True)
           result()
       elif NUM < int(Vgue.get()):
           print("猜错了,偏大了")
           Vgue.set("")
           time.append(False)
           result()
       elif NUM > int(Vgue.get()):
           print("猜错了,偏小了")
           Vgue.set("")
           time.append(False)
           result()

def result():
   """
   根据结果做出判断是否继续玩游戏
   :return:
   """
   if len(time) <= 5 and time.count(True) == 1:
       print("i win the game")
       window.destroy()
   elif len(time) == 5 and time.count(False) == 5:
       print("i lose the game")
       window.destroy()
   else:
       return None

window=Tk()
window.title("猜数字")
window.geometry("400x100+100+100")
frametar=Frame(window)
flogin=Frame(window)
Vgue=StringVar()

Gue_ture=Label(frametar,fg="black",font="微软雅黑,20",text="请猜猜看这次随机数是:").grid(column=0,row=0)#显示标签
Gue=Entry(frametar,fg="black",font="微软雅黑,20",text=Vgue).grid(column=1,row=0)#随机数输入框
Start=Button(flogin,text="START",fg="Blue",command=Sstart,width=30).grid(column=0,row=1)#开始按钮
frametar.grid()
flogin.grid()
mainloop()

以上所述是小编给大家介绍的Python实现猜数字小游戏网站的支持!

来源:https://blog.csdn.net/qq_58370894/article/details/121725432

标签:python,猜数字,游戏
0
投稿

猜你喜欢

  • JavaScript Math 对象常用方法总结

    2024-06-05 09:33:56
  • Python调用Matplotlib绘制振动图、箱型图和提琴图

    2022-02-08 05:56:09
  • Python读取实时数据流示例

    2023-09-11 14:20:00
  • python类中的self和变量用法及说明

    2022-05-27 10:33:12
  • Python即时网络爬虫项目启动说明详解

    2022-11-29 18:09:24
  • PHP开发技巧之PHAR反序列化详解

    2023-11-15 02:23:45
  • Python 多线程爬取案例

    2022-11-17 16:02:06
  • Python如何把不同类型数据的json序列化

    2021-06-01 22:52:16
  • 获取MSSQL数据字典的SQL语句

    2024-01-20 11:35:16
  • go语言睡眠排序算法实例分析

    2023-07-15 17:42:56
  • PyCharm设置注释字体颜色以及是否倾斜的操作

    2022-05-02 06:03:29
  • Go语言函数学习教程

    2024-02-22 14:17:41
  • Spring Boot整合 NoSQL 数据库 Redis详解

    2024-01-29 18:16:36
  • Vue3 全局实例上挂载属性方法案例讲解

    2023-07-02 16:46:14
  • python unittest单元测试的步骤分析

    2022-03-21 05:59:10
  • opencv 图像加法与图像融合的实现代码

    2021-07-20 14:57:30
  • python处理列表的部分元素的实例详解

    2021-04-16 01:13:05
  • Java利用套接字实现应用程序对数据库的访问

    2024-01-15 19:37:47
  • python定向爬取淘宝商品价格

    2023-10-03 23:33:12
  • 分析SQL语句性能3种方法分享

    2012-06-06 20:09:30
  • asp之家 网络编程 m.aspxhome.com