python扫雷的代码及原理_基于Python实现的扫雷游戏实例代码
摘要:这篇Python开发技术栏目下的“基于Python实现的扫雷游戏实例代码”,介绍的技术点是“Python实现、Python、实例代码、扫雷游戏、扫雷、游戏”,希望对大家开发技术学习和问题解决有帮助。这篇文章主要介绍了基于Python实现的扫雷游戏实例代码,对于Python的学习以及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=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('',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实现的扫雷游戏实例代码相关推荐
- python卡方检验筛选特征原理_基于Python的遥感特征筛选—递归特征消除(RFE)与极限树(Extra-Trees)...
引言 基于前几篇文章关于筛选方法的介绍,本篇同样给大家介绍两种python封装的经典特征降维方法,递归特征消除(RFE)与极限树(Extra-Trees, ET).其中,RFE整合了两种不同的超参数, ...
- python关于二手房的课程论文_基于python爬取链家二手房信息代码示例
基本环境配置 python 3.6 pycharm requests parsel time 相关模块pip安装即可 确定目标网页数据 哦豁,这个价格..................看到都觉得脑阔 ...
- python写安卓app控制蓝牙_基于python实现蓝牙通信代码实例
这篇文章主要介绍了基于python实现蓝牙通信代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装和示例 linux下安装 sudo apt ...
- python中numpy数组的合并_基于Python中numpy数组的合并实例讲解
基于Python中numpy数组的合并实例讲解 Python中numpy数组的合并有很多方法,如 - np.append() - np.concatenate() - np.stack() - np. ...
- python检测微信好友是否删除_基于Python+adb实现微信是否好友检测
本文介绍的基于Python+adb实现的微信好友检测,是通过adb操控手机,模拟人的点击.截屏操作,对应用无侵入,无需扫描登录即可实现好友检测. 网上看到一些文章类似功能的实现,总结起来千篇一律的引入 ...
- python的图书管理项目教程_基于python图书馆管理系统设计实例详解
写完这个项目后,导师说这个你完全可以当作毕业项目使用了,写的很全,很多的都设计考虑周全,但我的脚步绝不止于现在,我想要的是星辰大海!与君共勉! 这个项目不是我的作业, 只是无意中被拉进来了,然后就承担 ...
- python实现空气质量提醒程序_基于Python实现空气质量指数可视化
前面我们已经爬取了全国城市空气质量数据( 基于Python实现城市空气质量爬取 ),基于之前我们爬取的数据,本文将使用Python将空气质量最好的前20个城市以柱状图的形式展示出来,点击对应的柱状图能 ...
- python怎样使用各个日期赤纬_基于Python的天文软件命令行界面设计与实现
计算机软件技术的不断发展,推动了人机交互技术的长足进步.从传统的命令行(Command Line Interface,CLI),到图形用户界面(Graphical User Interface,GUI ...
- python版植物大战僵尸源码_基于python的植物大战僵尸游戏设计与实现.docx
湖南理工学院毕业设计(论文) PAGE PAGE 1 学 号 毕业设计(论文) 题目:基于python的植物大战僵尸游戏设计与实现 作 者 届 别 届 院 别 信息与通信工程学院 专 业 信息工程 指 ...
最新文章
- 省市县三级级联(模块化开发)
- 室外越野组的传感器长度:40厘米,45厘米,50厘米
- 【RAC】使用一条“ps”命令获取Linux环境下全部RAC集群进程信息
- Python 在定义函数时 为什么默认参数不能放在必选参数前面
- 【机器学习基础】Softmax与交叉熵的数学意义(信息论与概率论视角)
- 变频器服务器电路板维修,变频器线路板常见维修方法
- [html] 说说你对target=“_blank“的理解?有啥安全性问题?如何防范?
- mybatis传递多个参数_MyBatis 映射器
- NLP之路-warm up
- idea创建springboot项目+mybatis_从spring boot项目创建到netty项目过渡1
- 《高可用架构·中国初创故事(第3期)》一1.4 认同企业文化
- 爬虫python能做什么-Python 爬虫学到什么样就可以找工作了?
- 刚创建了蕝薱嚣张IT部落
- kubernetes如何进入指定的容器
- 孔乙己:new的五种写法
- 博客优化、收录、RSS技巧
- 好书推荐:创业必看好书排行榜推荐
- 【Baidu Apollo】5 预测
- 数字语音信号处理学习笔记——语音信号的数字模型(1)
- 11种典型的时间序列回归预测方法大集合——附代码
热门文章
- 看微软 Windows 30年发展简史,你用过最早的系统版本是什么?
- linux要不要home分区,有必要建/home/boot分区吗
- numpy 矩阵乘法_一起学习Python常用模块——numpy
- html页面加载转圈,纯CSS实现加载转圈样式
- 为什么不敢和别人竞争_看懂这个自我评价发展曲线,你就明白,为什么青春期孩子如此叛逆...
- python中mainloop什么意思_很难理解python中的Tkinter mainloop()
- 【若依(ruoyi)】重置密码SQL脚本
- icu入院宣教流程图_ICU患者及家属的健康教育PPT.ppt
- mysql server 5.0的jdbc驱动_MySQL5.0的JDBC驱动程序(转
- 通配符 不是jpg 结尾文件_Struts(十九)- 使用使用通配符匹配