python实现扫雷游戏


Posted in Python onMarch 03, 2020

本文为大家分享了python实现扫雷游戏的具体代码,供大家参考,具体内容如下

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

具体的功能代码如下:

# -*- coding: utf-8 -*-
import random
import sys
from Tkinter import *
'''
想要学习Python?

'''
class Model:
 """
 核心数据类,维护一个矩阵
 """
 def __init__(self,row,col):
 self.width=col
 self.height=row
 self.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 True
 else:
 return False
 
 def countValue(self,r,c,value):
 """
 统计某个位置周围8个位置中,值为value的个数
 """
 count=0
 if r-1>=0 and c-1>=0:
 if self.items[r-1][c-1]==1:count+=1
 if r-1>=0 and c>=0:
 if self.items[r-1][c]==1:count+=1
 if r-1>=0 and c+1<=self.width-1:
 if self.items[r-1][c+1]==1:count+=1
 if c-1>=0:
 if self.items[r][c-1]==1:count+=1
 if c+1<=self.width-1 :
 if self.items[r][c+1]==1:count+=1
 if r+1<=self.height-1 and c-1>=0:
 if self.items[r+1][c-1]==1:count+=1
 if r+1<=self.height-1 :
 if self.items[r+1][c]==1:count+=1
 if r+1<=self.height-1 and c+1<=self.width-1:
 if self.items[r+1][c+1]==1:count+=1
 return count
 
 
class Mines(Frame):
 def __init__(self,m,master=None):
 Frame.__init__(self,master)
 self.model=m
 self.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']=r
 self.buttongroups[r][c]['pady']=c
 
 def 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:
 pass
 
 
 def 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):
 """
 重新开始游戏
 """
 pass
 
if __name__=='__main__':
 model=Model(10,10)
 root=Tk()
 
 #menu
 menu = 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)
 
 #Mines
 m=Mines(model,root)
 #m.printf()
 root.mainloop()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python的Bottle框架写一个简单的服务接口的示例
Aug 25 Python
python使用代理ip访问网站的实例
May 07 Python
python 文件查找及内容匹配方法
Oct 25 Python
在python中pandas读文件,有中文字符的方法
Dec 12 Python
Python面向对象之类的内置attr属性示例
Dec 14 Python
python 产生token及token验证的方法
Dec 26 Python
Python反爬虫技术之防止IP地址被封杀的讲解
Jan 09 Python
Python3转换html到pdf的不同解决方案
Mar 11 Python
pytorch::Dataloader中的迭代器和生成器应用详解
Jan 03 Python
Python 下载Bing壁纸的示例
Sep 29 Python
Python QT组件库qtwidgets的使用
Nov 02 Python
python 爬虫之selenium可视化爬虫的实现
Dec 04 Python
python实现从ftp服务器下载文件
Mar 03 #Python
python实现简单的购物程序代码实例
Mar 03 #Python
python实现跨excel sheet复制代码实例
Mar 03 #Python
python剪切视频与合并视频的实现
Mar 03 #Python
详解Pycharm出现out of memory的终极解决方法
Mar 03 #Python
基于python 等频分箱qcut问题的解决
Mar 03 #Python
python实现快递价格查询系统
Mar 03 #Python
You might like
php事务回滚简单实现方法示例
2017/03/28 PHP
Javascript 实现TreeView CheckBox全选效果
2010/01/11 Javascript
Exjs 入门篇
2010/04/07 Javascript
js调试系列 初识控制台
2014/06/18 Javascript
js图片轮播特效代码分享
2015/09/07 Javascript
使用json来定义函数,在里面可以定义多个函数的实现方法
2016/10/28 Javascript
Sortable.js拖拽排序使用方法解析
2016/11/04 Javascript
利用js的闭包原理做对象封装及调用方法
2017/04/07 Javascript
JS 学习总结之正则表达式的懒惰性和贪婪性
2017/07/03 Javascript
node.js实现微信JS-API封装接口的示例代码
2017/09/06 Javascript
微信小程序实战篇之购物车的实现代码示例
2017/11/30 Javascript
微信小程序实现给嵌套template模板传递数据的方式总结
2017/12/18 Javascript
Vue2.0 实现单选互斥的方法
2018/04/13 Javascript
jQuery中将json数据显示到页面表格的方法
2018/05/27 jQuery
学习LayUI时自研的表单参数校验框架案例分析
2019/07/29 Javascript
详解js location.href和window.open的几种用法和区别
2019/12/02 Javascript
python入门之语句(if语句、while语句、for语句)
2015/01/19 Python
python用BeautifulSoup库简单爬虫实例分析
2018/07/30 Python
Python3模拟curl发送post请求操作示例
2019/05/03 Python
Python Django 简单分页的实现代码解析
2019/08/21 Python
Python爬虫:url中带字典列表参数的编码转换方法
2019/08/21 Python
python 读取更新中的log 或其它文本方式
2019/12/24 Python
TensorFlow tensor的拼接实例
2020/01/19 Python
Python confluent kafka客户端配置kerberos认证流程详解
2020/10/12 Python
基于Python的图像阈值化分割(迭代法)
2020/11/20 Python
20行代码教你用python给证件照换底色的方法示例
2021/02/05 Python
No7 Beauty美国官网:英国国民护肤品牌
2019/10/31 全球购物
报关报检委托书
2014/04/08 职场文书
药品营销策划方案
2014/06/15 职场文书
微笑服务标语
2014/06/24 职场文书
纪检干部现实表现材料
2014/08/21 职场文书
2016年少先队活动总结
2016/04/06 职场文书
Html5页面播放M4a音频文件
2021/03/30 HTML / CSS
JavaScript 防篡改对象的用法示例
2021/04/24 Javascript
JavaWeb 入门篇(3)ServletContext 详解 具体应用
2021/07/16 Java/Android
java实现自定义时钟并实现走时功能
2022/06/21 Java/Android