基于Python实现的扫雷游戏实例代码
2023-06-13 09:15:40 时间
本文实例借鉴mvc模式,核心数据为model,维护1个矩阵,0表无雷,1表雷,-1表已经检测过。
本例使用python的tkinter做gui,由于没考虑可用性问题,因此UI比较难看,pygame更有趣更强大更好看,做这些小游戏更合适,感兴趣的读者可以尝试一下!
具体的功能代码如下:
#-*-coding:utf-8-*- importrandom importsys fromTkinterimport* classModel: """ 核心数据类,维护一个矩阵 """ def__init__(self,row,col): self.width=col self.height=row self.items=[[0forcinrange(col)]forrinrange(row)] defsetItemValue(self,r,c,value): """ 设置某个位置的值为value """ self.items[r][c]=value; defcheckValue(self,r,c,value): """ 检测某个位置的值是否为value """ ifself.items[r][c]!=-1andself.items[r][c]==value: self.items[r][c]=-1#已经检测过 returnTrue else: returnFalse defcountValue(self,r,c,value): """ 统计某个位置周围8个位置中,值为value的个数 """ count=0 ifr-1>=0andc-1>=0: ifself.items[r-1][c-1]==1:count+=1 ifr-1>=0andc>=0: ifself.items[r-1][c]==1:count+=1 ifr-1>=0andc+1<=self.width-1: ifself.items[r-1][c+1]==1:count+=1 ifc-1>=0: ifself.items[r][c-1]==1:count+=1 ifc+1<=self.width-1: ifself.items[r][c+1]==1:count+=1 ifr+1<=self.height-1andc-1>=0: ifself.items[r+1][c-1]==1:count+=1 ifr+1<=self.height-1: ifself.items[r+1][c]==1:count+=1 ifr+1<=self.height-1andc+1<=self.width-1: ifself.items[r+1][c+1]==1:count+=1 returncount classMines(Frame): def__init__(self,m,master=None): Frame.__init__(self,master) self.model=m self.initmine() self.grid() self.createWidgets() defcreateWidgets(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)foriinrange(self.model.width)] forjinrange(self.model.height)] forrinrange(self.model.width): forcinrange(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 defshowall(self): forrinrange(model.height): forcinrange(model.width): self.showone(r,c) defshowone(self,r,c): ifmodel.checkValue(r,c,0): self.buttongroups[r][c]["text"]=model.countValue(r,c,1) else: self.buttongroups[r][c]["text"]="Mines" defrecureshow(self,r,c): if0<=r<=self.model.height-1and0<=c<=self.model.width-1: ifmodel.checkValue(r,c,0)andmodel.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) elifmodel.countValue(r,c,1)!=0: self.buttongroups[r][c]["text"]=model.countValue(r,c,1) else: pass defclickevent(self,event): """ 点击事件 case1:是雷,所有都显示出来,游戏结束 case2:是周围雷数为0的,递归触发周围8个button的点击事件 case3:周围雷数不为0的,显示周围雷数 """ r=int(str(event.widget["padx"])) c=int(str(event.widget["pady"])) ifmodel.checkValue(r,c,1):#是雷 self.showall() else:#不是雷 self.recureshow(r,c) definitmine(self): """ 埋雷,每行埋height/width+2个暂定 """ r=random.randint(1,model.height/model.width+2) forrinrange(model.height): foriinrange(2): rancol=random.randint(0,model.width-1) model.setItemValue(r,rancol,1) defprintf(self): """ 打印 """ forrinrange(model.height): forcinrange(model.width): printmodel.items[r][c], print"/n" defnew(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获取时间戳_python爬虫时间戳
- python的random()函数用法_Python随机函数random用法示例
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- h5 Python_python做h5网站
- vscode运行python_vscode python 调试
- python中copy.deepcopy_Python eval
- python实例代码爬虫_python 网络爬虫实例代码
- 20 个 解决日常编程问题的Python 代码片段
- python实践之电商系列
- Python类变量和实例变量(类属性和实例属性)
- python代码实例大小写转换,首字母大写,去除特殊字符详解编程语言
- Python玩转Redis:提升缓存效率(python使用redis)
- Python编程连接MySQL:从零开始(python与mysql)
- Python异常处理实例详解
- 使用Python判断IP地址合法性的方法实例
- python操作xml文件示例
- 实例讲解python函数式编程
- python中使用enumerate函数遍历元素实例
- Python编程中的反模式实例分析
- Python常用列表数据结构小结
- Python中正则表达式的用法实例汇总