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使用PyCrypto实现AES加密功能示例
May 22 Python
Python基于递归算法实现的汉诺塔与Fibonacci数列示例
Apr 18 Python
python 将数据保存为excel的xls格式(实例讲解)
May 03 Python
Pycharm远程调试原理及具体配置详解
Aug 08 Python
使用matlab或python将txt文件转为excel表格
Nov 01 Python
python实现一个点绕另一个点旋转后的坐标
Dec 04 Python
使用Python串口实时显示数据并绘图的例子
Dec 26 Python
python编程进阶之类和对象用法实例分析
Feb 21 Python
Python基于requests库爬取网站信息
Mar 02 Python
配置python的编程环境之Anaconda + VSCode的教程
Mar 29 Python
python接入支付宝的实例操作
Jul 20 Python
使用Python判断一个文件是否被占用的方法教程
Dec 16 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缩略图生成程式(需要GD库支持)
2007/03/06 PHP
php设计模式 Template (模板模式)
2011/06/26 PHP
PHP防止post重复提交数据的简单例子
2014/06/07 PHP
基于laravel制作APP接口(API)
2016/03/15 PHP
PHP中for循环与foreach的区别
2017/03/06 PHP
form自动提交实例讲解
2017/07/10 PHP
PHP实现防止表单重复提交功能【基于token验证】
2018/05/24 PHP
让jQuery Mobile不显示讨厌loading界面的方法
2014/02/19 Javascript
JQuery给元素绑定click事件多次执行的解决方法
2014/05/29 Javascript
拥Bootstrap入怀——导航栏篇
2016/05/30 Javascript
JS 滚动事件window.onscroll与position:fixed写兼容IE6的回到顶部组件
2016/10/10 Javascript
JS实现动态修改table及合并单元格的方法示例
2017/02/20 Javascript
谈谈VUE种methods watch和compute的区别和联系
2017/08/01 Javascript
让axios发送表单请求形式的键值对post数据的实例
2018/08/11 Javascript
elementUI Vue 单个按钮显示和隐藏的变换功能(两种方法)
2018/09/04 Javascript
使用Vue.set()方法实现响应式修改数组数据步骤
2019/11/09 Javascript
5分钟快速看懂ES6中的反射与代理
2019/12/19 Javascript
[08:44]和酒神一起战斗 DOTA2教你做大人
2014/03/27 DOTA
pyqt4教程之实现windows窗口小示例分享
2014/03/07 Python
对numpy中shape的深入理解
2018/06/15 Python
python如何生成网页验证码
2018/07/28 Python
Python实现的删除重复文件或图片功能示例【去重】
2019/04/23 Python
python项目对接钉钉SDK的实现
2019/07/15 Python
python实现单目标、多目标、多尺度、自定义特征的KCF跟踪算法(实例代码)
2020/01/08 Python
Python 实现一行输入多个数字(用空格隔开)
2020/04/29 Python
移动端html5判断是否滚动到底部并且下拉加载
2019/11/19 HTML / CSS
韩国著名的在线综合购物网站:Akmall
2016/08/07 全球购物
澳大利亚在线生活方式商店:Mytopia
2018/07/08 全球购物
药品质量检测应届生求职信
2013/11/14 职场文书
文科教师毕业的自我评价
2014/01/16 职场文书
《自选商场》教学反思
2014/02/14 职场文书
《白鹅》教学反思
2014/04/13 职场文书
安全负责人任命书
2014/06/06 职场文书
运动会广播稿300字
2015/08/19 职场文书
MySQL 百万级数据的4种查询优化方式
2021/06/07 MySQL
25张裸眼3D图片,带你重温童年的记忆,感受3D的魅力
2022/02/06 杂记