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制作爬虫采集小说
Oct 25 Python
用pickle存储Python的原生对象方法
Apr 28 Python
Python中turtle作图示例
Nov 15 Python
Python获取当前函数名称方法实例分享
Jan 18 Python
Python对多属性的重复数据去重实例
Apr 18 Python
Python中asyncio模块的深入讲解
Jun 10 Python
TensorFlow tensor的拼接实例
Jan 19 Python
pandas中ix的使用详细讲解
Mar 09 Python
python 识别登录验证码图片功能的实现代码(完整代码)
Jul 03 Python
python文件操作seek()偏移量,读取指正到指定位置操作
Jul 05 Python
python实现图片,视频人脸识别(dlib版)
Nov 18 Python
详解用 python-docx 创建浮动图片
Jan 24 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
全国FM电台频率大全 - 5 内蒙古自治区
2020/03/11 无线电
php echo()和print()、require()和include()函数区别说明
2010/03/27 PHP
处理单名多值表单的详解
2013/06/08 PHP
实例介绍PHP的Reflection反射机制
2014/08/05 PHP
php实现根据url自动生成缩略图的方法
2014/09/23 PHP
PHP页面静态化――纯静态与伪静态用法详解
2020/06/05 PHP
Yii redis集合的基本使用教程
2020/06/14 PHP
使用新的消息弹出框blackbirdjs
2008/10/16 Javascript
js动态往表格的td中添加图片并注册事件
2014/06/12 Javascript
jquery操作checkbox示例分享
2014/07/21 Javascript
通过jquery 获取URL参数并进行转码
2014/08/18 Javascript
js怎么覆盖原有方法实现重写
2014/09/04 Javascript
javascript实现base64 md5 sha1 密码加密
2015/09/09 Javascript
轻松使用jQuery双向select控件Bootstrap Dual Listbox
2015/12/13 Javascript
jQuery position() 函数详解以及jQuery中position函数的应用
2015/12/14 Javascript
JS获取中文拼音首字母并通过拼音首字母快速查找页面内对应中文内容的方法【附demo源码】
2016/08/19 Javascript
jQuery双向列表选择器DIV模拟版
2016/11/01 Javascript
原生JS实现图片轮播切换效果
2016/12/15 Javascript
分析JS单线程异步io回调的特性
2017/12/01 Javascript
JavaScript数据结构与算法之二叉树添加/删除节点操作示例
2019/03/01 Javascript
vue 更改连接后台的api示例
2019/11/11 Javascript
[28:28]Ti4 冒泡赛第二天NEWBEE vs NaVi 2
2014/07/15 DOTA
100行python代码实现跳一跳辅助程序
2018/01/15 Python
Python冲顶大会 快来答题!
2018/01/17 Python
解决django服务器重启端口被占用的问题
2019/07/26 Python
Python中zip()函数的简单用法举例
2019/09/02 Python
python由已知数组快速生成新数组的方法
2020/04/08 Python
Python实现汇率转换操作
2020/05/03 Python
Python实现验证码识别
2020/06/15 Python
css3给背景图片加颜色遮罩的方法
2019/11/05 HTML / CSS
Daisy London官网:英国最大的首饰集团IBB旗下
2019/02/28 全球购物
全球最大的瓷器、水晶和银器零售商:Replacements
2020/06/15 全球购物
学校十一活动方案
2014/02/01 职场文书
公司活动总结范文
2014/07/01 职场文书
2019年特色火锅店的创业计划书模板
2019/08/28 职场文书
《乙女游戏世界对路人角色很不友好》OP主题曲无字幕动画MV公开
2022/04/05 日漫