python实现扫雷游戏

作者:嗨学编程 时间:2022-05-19 00:59:01 

本文为大家分享了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()

来源:https://blog.csdn.net/fei347795790/article/details/89606044

标签:python,扫雷
0
投稿

猜你喜欢

  • python连接PostgreSQL数据库的过程详解

    2023-08-24 03:42:31
  • js控制图片闪烁代码

    2008-07-30 12:35:00
  • Python元组定义及集合的使用

    2023-11-22 12:32:03
  • 让你一文弄懂Pandas文本数据处理

    2023-07-17 19:12:08
  • 关于文件命名

    2009-09-18 16:29:00
  • 详解Python中图像边缘检测算法的实现

    2021-02-08 09:18:27
  • Python3将jpg转为pdf文件的方法示例

    2021-06-25 11:31:17
  • 模拟下拉菜单[兼容IE系列以及火狐]

    2009-12-13 10:23:00
  • REPAIR TABLE语法介绍——MySQL数据库

    2012-01-05 19:08:59
  • 对python 多个分隔符split 的实例详解

    2022-12-26 11:24:27
  • 用SQL语句添加删除修改字段、一些表与字段的基本操作、数据库备份等

    2011-12-01 07:53:11
  • Python3使用PySynth制作音乐的方法

    2021-03-18 19:41:01
  • asp版FCKEditor编辑器的用法

    2008-07-05 12:15:00
  • 《写给大家看的设计书》阅读笔记之对齐原则

    2009-07-09 16:32:00
  • Python实现分割文件及合并文件的方法

    2022-01-28 00:02:02
  • javascript中类的创建和继承

    2008-05-08 12:07:00
  • 浅析Django 接收所有文件,前端展示文件(包括视频,文件,图片)ajax请求

    2023-03-30 14:35:25
  • Python高级特性之闭包与装饰器实例详解

    2021-09-19 03:17:25
  • 剖析SQL Server 事务日志的收缩和截断

    2009-01-15 13:04:00
  • python global关键字的用法详解

    2023-12-30 03:32:02
  • asp之家 网络编程 m.aspxhome.com