扫雷扫雷,我想用过电脑的朋友,应该都玩过这个小游戏吧。有时候我敲完代码无聊的时候,就会打开扫雷来玩玩,现在python也能用代码实现扫雷游戏了。

实例借鉴mvc模式,核心数据为model,维护1个矩阵,0表无雷,1表雷,-1表已经检测过。
本例使用python的tkinter做gui,由于没考虑可用性问题,因此UI比较难看,pygame更有趣更强大更好看,做这些小游戏更合适,感兴趣的朋友可以尝试一下!

具体的功能代码如下:

# -*- coding: utf-8 -*-
import random
import sys
from Tkinter import *
class Model:"""核心数据类,维护一个矩阵"""def __init__(self,row,col):self.width=colself.height=rowself.items=[[0 for c in range(col)] for r in range(row)]def setItemValue(self,r,c,value):"""设置某个位置的值为value"""self.items[r][c]=value;def checkValue(self,r,c,value):"""检测某个位置的值是否为value"""if self.items[r][c]!=-1 and self.items[r][c]==value:self.items[r][c]=-1 #已经检测过return Trueelse:return Falsedef countValue(self,r,c,value):"""统计某个位置周围8个位置中,值为value的个数"""count=0if r-1>=0 and c-1>=0:if self.items[r-1][c-1]==1:count+=1if r-1>=0 and c>=0:if self.items[r-1][c]==1:count+=1if r-1>=0 and c+1<=self.width-1:if self.items[r-1][c+1]==1:count+=1if c-1>=0:if self.items[r][c-1]==1:count+=1if c+1<=self.width-1 :if self.items[r][c+1]==1:count+=1if r+1<=self.height-1 and c-1>=0:if self.items[r+1][c-1]==1:count+=1if r+1<=self.height-1 :if self.items[r+1][c]==1:count+=1if r+1<=self.height-1 and c+1<=self.width-1:if self.items[r+1][c+1]==1:count+=1return countclass Mines(Frame):def __init__(self,m,master=None):Frame.__init__(self,master)self.model=mself.initmine()self.grid()self.createWidgets()def createWidgets(self):#top=self.winfo_toplevel()#top.rowconfigure(self.model.height*2,weight=1)#top.columnconfigure(self.model.width*2,weight=1)self.rowconfigure(self.model.height,weight=1)self.columnconfigure(self.model.width,weight=1)self.buttongroups=[[Button(self,height=1,width=2) for i in range(self.model.width)]for j in range(self.model.height)]for r in range(self.model.width):for c in range(self.model.height):self.buttongroups[r][c].grid(row=r,column=c)self.buttongroups[r][c].bind('<Button-1>',self.clickevent)self.buttongroups[r][c]['padx']=rself.buttongroups[r][c]['pady']=cdef showall(self):for r in range(model.height):for c in range(model.width):self.showone(r,c)def showone(self,r,c):if model.checkValue(r,c,0):self.buttongroups[r][c]['text']=model.countValue(r,c,1)else:self.buttongroups[r][c]['text']='Mines'def recureshow(self,r,c):if 0<=r<=self.model.height-1 and 0<=c<=self.model.width-1:if model.checkValue(r,c,0) and model.countValue(r,c,1)==0:self.buttongroups[r][c]['text']=''self.recureshow(r-1,c-1)self.recureshow(r-1,c)self.recureshow(r-1,c+1)self.recureshow(r,c-1)self.recureshow(r,c+1)self.recureshow(r+1,c-1)self.recureshow(r+1,c)self.recureshow(r+1,c+1)elif model.countValue(r,c,1)!=0:self.buttongroups[r][c]['text']=model.countValue(r,c,1)else:passdef clickevent(self,event):"""点击事件case 1:是雷,所有都显示出来,游戏结束case 2:是周围雷数为0的,递归触发周围8个button的点击事件case 3:周围雷数不为0的,显示周围雷数"""r=int(str(event.widget['padx']))c=int(str(event.widget['pady']))if model.checkValue(r,c,1):#是雷self.showall()else:#不是雷self.recureshow(r,c)def initmine(self):"""埋雷,每行埋height/width+2个暂定"""r=random.randint(1,model.height/model.width+2)for r in range(model.height):for i in range(2):rancol=random.randint(0,model.width-1)model.setItemValue(r,rancol,1)def printf(self):"""打印"""for r in range(model.height):for c in range(model.width):print model.items[r][c],print '/n'def new(self):"""重新开始游戏"""passif __name__=='__main__':model=Model(10,10)root=Tk()#menumenu = Menu(root)root.config(menu=menu)filemenu = Menu(menu)menu.add_cascade(label="File", menu=filemenu)filemenu.add_command(label="New",command=new)filemenu.add_separator()filemenu.add_command(label="Exit", command=root.quit)#Minesm=Mines(model,root)#m.printf()root.mainloop()

用Python自制扫雷游戏代码相关推荐

  1. python扫雷游戏代码_python实现扫雷游戏的示例

    扫雷是一款益智类小游戏,最早于 1992 年由微软在 Windows 上发行,游戏适合于全年龄段,规则简单,即在最短的时间内找出所有非雷格子且在中间过程中不能踩到雷, 踩到雷则失败,需重新开始. 本文 ...

  2. python手机版做小游戏代码大全-python简单小游戏代码 怎么用Python制作简单小游戏...

    1.Python猜拳小游戏代码: 2.import random #导入随机模块 3. 4.num = 1 5.yin_num = 0 6.shu_num = 0 7.while num <= ...

  3. python小游戏代码大全-python简单小游戏代码 怎么用Python制作简单小游戏

    1.Python猜拳小游戏代码: 2.import random #导入随机模块 3. 4.num = 1 5.yin_num = 0 6.shu_num = 0 7.while num <= ...

  4. c++扫雷游戏代码_C语言学习教程,用C语言编写扫雷游戏

    本文实例为大家分享了C语言实现扫雷游戏及其优化的具体代码,供大家参考,具体内容如下 关于扫雷优化 1.核心思想:使用两个二维数组进行设计,一个用于显示,一个用于后台雷的布置. 2.使用宏常量,后期可以 ...

  5. python 战棋游戏代码实现(2):六边形地图寻路和显示

    python 战棋游戏代码实现(2):六边形地图寻路和显示 六边形地图介绍 代码介绍 地图六边形显示 A*算法的六边形寻路修改 判断某个点在哪个六边形中 完整代码 编译运行 六边形地图介绍 之前的文章 ...

  6. 详细的Python炸金花游戏代码

    ** 详细的Python炸金花游戏代码 ** 觉得有用请点个赞吧 #!/usr/bin/env python # -*- coding: utf-8 -*- ''' # @Time: 2022/07/ ...

  7. 【180720】微软Windows扫雷游戏代码

    源码简介   本源码是一个微软Windows扫雷游戏代码,可选择难度级别:初级.中级.高级. 注意事项: 1.开发环境为Visual Studio 2010,使用.net 2.0开发. 源码下载地址: ...

  8. python小游戏代码200行左右,python编程小游戏代码

    大家好,本文将围绕python小游戏代码200行左右展开说明,小游戏程序代码python是一个很多人都想弄明白的事情,想搞清楚python编程小游戏代码需要先了解以下几个事情. 1.python简单小 ...

  9. python扫雷游戏实验分析_用python写扫雷游戏实例代码分享

    扫雷是一个非常经典的WIN游戏,我们教给大家用python语言来写出这个游戏,以下是全部实例代码: #!/usr/bin/python #coding:utf-8 #python 写的扫雷游戏 imp ...

最新文章

  1. iOS开发中用到的一些第三方库
  2. keil4在win10上无法启动_斯柯达的一键启动装置除了方便,还有啥功能?
  3. cordova 学习笔记
  4. 全局异步和主线程异步区别、改变PlaceHolder颜色、解决键盘弹起挡住文本框问题...
  5. JS和安卓 IOS的交互 例子式记录
  6. python实现采样函数_python中resample函数实现重采样和降采样代码
  7. 屠呦呦3年后再上热搜:女先生,世无双!
  8. 【计算机网络】复习荟萃(四)
  9. 【Java】JDK1.8新特性Date类----日期/时间改进
  10. Python笔记(二)——python调用C/C++模块
  11. 虚拟机中安装windows XP系统
  12. 【统计学】三大相关系数之斯皮尔曼相关系数(spearman correlation coefficient)
  13. 五类/超五类/六类/超六类/七类等多类网线的比较
  14. React Native学习笔记(二)---运行开源App,形成直观感受
  15. 应用程序无法正常启动0xc000007b解决方法
  16. 美国大厂码农薪资曝光:年薪18万美元,够养家,不够买海景房
  17. PPT制作毛玻璃效果
  18. 限流的抖音号怎么养?养号方法是什么?
  19. Android文件读写操作(assets 文件、 raw文件、内部存储文件、外部存储文件)
  20. RESTful 标准接口教程

热门文章

  1. hcie网上培训的优势什么?
  2. 如何对matlab的scope图编辑
  3. 服务型机器人的“立春”来临了吗?
  4. 深度 | 黑客 Only_Guest 讲述:如何优雅地手刃骗子?
  5. python中进制转换关系
  6. 反锯齿渲染--TXAA
  7. 2022年中国铝合金产量、下游应用及主要企业经营情况分析[图]
  8. JAVA语言中 文本框类的类名是_这是什么?
  9. Mac安装WineHQ
  10. DDD(领域驱动设计)系列主题:DDD兴起的原因以及与微服务的关系