This article shares the specific code of python to realize minesweeper game for everyone, for your reference, the specific content is as follows
The example in this article draws on the mvc model, the core data is model, and 1 matrix is maintained, 0 means no thunder, 1 means mine, and -1 means tested.
This example uses python's tkinter as the gui. Because the usability is not considered, the UI is ugly, pygame is more interesting, powerful and better-looking. It is more suitable to make these small games, and interested readers can try it!
The specific function code is as follows:
# - *- coding: utf-8-*-import random
import sys
from Tkinter import*'''
Want to learn Python?
'''
classModel:"""
Core data class, maintain a matrix
"""
def __init__(self,row,col):
self.width=col
self.height=row
self.items=[[0for c inrange(col)]for r inrange(row)]
def setItemValue(self,r,c,value):"""
Set the value of a position to value
"""
self.items[r][c]=value;
def checkValue(self,r,c,value):"""
Check whether the value of a certain position is value
"""
if self.items[r][c]!=-1 and self.items[r][c]==value:
self.items[r][c]=-1 #Has been tested
return True
else:return False
def countValue(self,r,c,value):"""
Count the number of values in 8 locations around a location
"""
count=0if r-1=0 and c-1=0:if self.items[r-1][c-1]==1:count+=1if r-1=0 and c =0:if self.items[r-1][c]==1:count+=1if r-1=0 and c+1<=self.width-1:if self.items[r-1][c+1]==1:count+=1if c-1=0:if self.items[r][c-1]==1:count+=1if c+1<=self.width-1:if self.items[r][c+1]==1:count+=1if r+1<=self.height-1 and c-1=0:if self.items[r+1][c-1]==1:count+=1if r+1<=self.height-1:if self.items[r+1][c]==1:count+=1if r+1<=self.height-1 and c+1<=self.width-1:if self.items[r+1][c+1]==1:count+=1return count
classMines(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 inrange(self.model.width)]for j inrange(self.model.height)]for r inrange(self.model.width):for c inrange(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 inrange(model.height):for c inrange(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):if0<=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):"""
Click event
case1:It's Thunder, everything is displayed, the game is over
case2:The number of surrounding mines is 0, and the click events of the surrounding 8 buttons are triggered recursively
case3:If the number of surrounding mines is not 0, the number of surrounding mines is displayed
"""
r=int(str(event.widget['padx']))
c=int(str(event.widget['pady']))if model.checkValue(r,c,1):#Ray
self.showall()else:#Not thunder
self.recureshow(r,c)
def initmine(self):"""
Mine mine,Each row buried height/width+2 tentative
"""
r=random.randint(1,model.height/model.width+2)for r inrange(model.height):for i inrange(2):
rancol=random.randint(0,model.width-1)
model.setItemValue(r,rancol,1)
def printf(self):"""
print
"""
for r inrange(model.height):for c inrange(model.width):
print model.items[r][c],
print '/n'
def new(self):"""
Restart the game
"""
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()
The above is the whole content of this article, I hope it will be helpful to everyone's study.
Recommended Posts