Python-Based Clearance game instance code, python clearance
The example in this article draws on the mvc mode. The core data is model, and one matrix is maintained. Table 0 has no thunder, Table 1 has thunder, and table-1 has been detected.
In this example, tkinter of python is used for gui. Because availability is not considered, the UI is ugly, and pygame is more interesting and better-looking. These games are more suitable, interested readers can try it!
The specific function code is as follows:
#-*-Coding: UTF-8-*-import randomimport sysfrom Tkinter import * class Model: "core data class, maintain a matrix" "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 ): "set the value of a location to value" self. items [r] [c] = value; def checkValue (self, r, c, value): "checks whether the value at a location is 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) has been detected ): "count the number of eight locations around a location with the 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 <= sel F. 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. crea TeWidgets () 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. recures How (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 case 1: Yes Ray, all are displayed, game end case 2: the number of surrounding mines is 0, and the Click Event of the eight buttons is triggered recursively. case 3: The number of surrounding mines is not 0, show the number of surrounding mines "r = int (str (event. widget ['padx']) c = int (str (event. widget ['pady']) if model. checkValue (r, c, 1): # Is Ray self. showall () else: # Not Ray self. recureshow (r, c) def initmine (self): "Mine, height/width on each line + 2 Tentative" 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): "print" for r in range (model. height): for c in range (model. width): print model. items [r] [c], print '/N' def new (self ): "Restarting the game" passif _ name __= = '_ main _': model = Model (10, 10) root = Tk () # 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 makes a simple clearance game code. It's urgent. You can help me with this. No clearance is required, and games can also be used. There should be no more than 150 lines.
The graphic interface is basically not small ..
Python class instantiation
You do not understand the relationship between class variables and instance variables.
In the first example, append is an operation on class variables in init. Therefore, the Instance Object newmen1/2 does not have its own variable a, and class variables are accessed.
If you display a class variable,
Print Men. a, newmen1.a, and newmen2.a are the same. Point to the same variable.
In the second example, init generates the object's own variable a. Note that '= '! When the instance object calls init, each instance object has its own variable a, and you can no longer access the class variable through the instance object. In fact, this writing method is not good.
Then you can display the class variable again,
Print Men. a, newmen1.a, newmen2
> 0 1 2
If you want all objects to share class variables, you can write
Class Men:
A = 0
Def _ init _ (self, B ):
Men. a = B
Def sayHi (self ):
Print 'hello, my name is ', Men.
Instead of using self to represent the public variables of the Instance Object. It will only make you dizzy.