Python implements minesweeper game

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&#39;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

Python implements minesweeper game
Python implements tic-tac-toe game
Python implements minesweeper games
Python implements guessing game
Python implements a guessing game
Python implements digital bomb game
Python implements simple tic-tac-toe game
Python implements text version minesweeper
Python implements the brick-and-mortar game
Python simply implements the snake game
Python implements digital bomb game program
Python implements Super Mario
Python implements man-machine gobang
Python implements image stitching
Python implements the source code of the snake game
Python implements scanning tools
Python implements threshold regression
Python implements electronic dictionary
Python implements simple tank battle
Python3 realizes airplane war game
Python implements udp chat window
python guess the word game
Python implements parking management system
Python implements TCP file transfer
Python realizes the guessing game
Python numpy implements rolling case
OpenCV Python implements puzzle games
Python implements password strength verification
Python implements car management system
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
Python implements multi-dimensional array sorting
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements image stitching function
Python implements student performance evaluation system
How Python implements the mail function
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements ten classic sorting algorithms
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down
Python implements alternate execution of two threads
Python solves the Tower of Hanoi game
Python implements the sum of fractional sequences
Python basic actual combat-guess the age game
python implements the gradient method python the fastest descent method
Python implements singly linked lists and dictionaries