基于Python实现的扫雷游戏实例代码

作者:shichen2014 时间:2023-06-26 12:29:34 

本文实例借鉴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('<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,扫雷,游戏
0
投稿

猜你喜欢

  • php下pdo的mysql事务处理用法实例

    2023-11-14 17:44:13
  • Entity Framework Core生成列并跟踪列记录

    2023-07-01 07:06:46
  • Python命令行参数解析模块getopt使用实例

    2021-02-20 08:53:48
  • Windows下将Python文件打包成.EXE可执行文件的方法

    2021-08-04 02:47:59
  • Go/C语言LeetCode题解997找到小镇法官

    2024-05-21 10:18:54
  • php实现xml与json之间的相互转换功能实例

    2023-11-22 05:38:00
  • Python新手实现2048小游戏

    2021-02-19 14:12:57
  • python错误处理详解

    2023-04-24 11:10:30
  • python人工智能tensorflow常用激活函数Activation Functions

    2023-08-01 12:45:02
  • 浅谈MySQL排序原理与案例分析

    2024-01-15 23:08:21
  • javascript+css图片切换效果

    2007-08-04 20:00:00
  • python实现猜数字小游戏

    2021-09-26 02:23:11
  • python opencv3实现人脸识别(windows)

    2023-11-09 11:21:17
  • 详解Python常用标准库之os模块与shutil模块

    2023-06-13 04:19:20
  • Python字符编码与函数的基本使用方法

    2023-11-29 17:01:30
  • 配置MySQL与卸载MySQL实例操作

    2024-01-19 21:55:26
  • 瞎扯之Web导航

    2009-03-18 19:32:00
  • mysql 通配符(sql 高级过滤)

    2024-01-24 17:15:39
  • pandas选择或添加列生成新的DataFrame操作示例

    2023-03-02 00:41:51
  • 基于python爬取有道翻译过程图解

    2021-11-03 23:57:27
  • asp之家 网络编程 m.aspxhome.com