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通过yield实现数组全排列的方法
Mar 18 Python
在Python中使用cookielib和urllib2配合PyQuery抓取网页信息
Apr 25 Python
Python处理字符串之isspace()方法的使用
May 19 Python
Python正则抓取网易新闻的方法示例
Apr 21 Python
Python中执行存储过程及获取存储过程返回值的方法
Oct 07 Python
Django框架教程之正则表达式URL误区详解
Jan 28 Python
Python smtplib实现发送邮件功能
May 22 Python
python实现最长公共子序列
May 22 Python
python实现词法分析器
Jan 31 Python
PyQt5下拉式复选框QComboCheckBox的实例
Jun 25 Python
python 同时读取多个文件的例子
Jul 16 Python
python 抓包保存为pcap文件并解析的实例
Jul 23 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下使用以下代码连接并测试
2008/04/09 PHP
JavaScript 常用函数
2009/12/30 Javascript
js获取URL的参数的方法(getQueryString)示例
2013/09/29 Javascript
js中split和replace的用法实例
2015/02/28 Javascript
谈谈AngularJs中的隐藏和显示
2015/12/09 Javascript
BootStrap Tooltip插件源码解析
2016/12/27 Javascript
原生Javascript插件开发实践
2017/01/09 Javascript
web打印小结
2017/01/11 Javascript
node.js中grunt和gulp的区别详解
2017/07/17 Javascript
深入浅析JavaScript中的RegExp对象
2017/09/18 Javascript
jQuery中可见性过滤器简单用法示例
2018/03/31 jQuery
使用gulp构建前端自动化的方法示例
2018/12/25 Javascript
el-select 下拉框多选实现全选的实现
2019/08/02 Javascript
angular inputNumber指令输入框只能输入数字的实现
2019/12/03 Javascript
vue+animation实现翻页动画
2020/06/29 Javascript
Openlayers实现图形绘制
2020/09/28 Javascript
Python 中的 else详解
2016/04/23 Python
Python 搭建Web站点之Web服务器与Web框架
2016/11/06 Python
python使用__slots__让你的代码更加节省内存
2018/09/05 Python
python钉钉机器人运维脚本监控实例
2019/02/20 Python
Python 离线工作环境搭建的方法步骤
2019/07/29 Python
opencv之为图像添加边界的方法示例
2019/12/26 Python
俄罗斯花园种植材料批发和零售网上商店:Беккер
2019/07/22 全球购物
祖国在我心中演讲稿
2014/01/15 职场文书
文员岗位职责范本
2014/03/08 职场文书
本科生就业推荐信
2014/05/19 职场文书
学生安全责任书模板
2014/07/25 职场文书
三方股东合作协议书范本
2014/09/28 职场文书
学校政风行风评议心得体会
2014/10/21 职场文书
党的群众路线专项整治方案
2014/11/03 职场文书
志愿者事迹材料
2014/12/26 职场文书
学生上课迟到检讨书
2015/01/01 职场文书
幼儿园大班教学反思
2016/03/02 职场文书
python数据库批量插入数据的实现(executemany的使用)
2021/04/30 Python
MySQL系列之二 多实例配置
2021/07/02 MySQL
深入浅出讲解Java8函数式编程
2022/01/18 Java/Android