---
title: "扫雷游戏 python实现"
author: "acen.chen"
date: "2009-04-27T09:26:45Z"
tags: []
slug: "python"
draft: false
lastmod: "2009-04-27T09:27:21Z"
id: 375319
url: "https://acen-chen.iteye.com/blog/375319"
---

# 扫雷游戏 python实现

借鉴mvc模式，核心数据为model，维护1个矩阵，0表无雷，1表雷，-1表已经检测过。

第一次使用python 的tkinter做gui,从构思到实现，基本1天时间，真是感慨python优越性。

还没考虑可用性问题，UI比较难看，pygame更有趣更强大更好看，做这些小游戏更合适。

[](http://blog.csdn.net/lwj1396/archive/2009/01/24/3852323.aspx)  
[](http://blog.csdn.net/lwj1396/archive/2009/01/24/3852323.aspx)

001. # -\*- coding: utf-8 -\*-
002. import  random
003. import  sys
004. from  Tkinter  import  *
005. class  Model:
006. """
007. 核心数据类，维护一个矩阵
008. """
009. def  \_\_init\_\_( self ,row,col):
010. self .width=col
011. self .height=row
012. self .items=\[\[ 0   for  c  in  range(col)]  for  r  in  range(row)]
013. def  setItemValue( self ,r,c,value):
014. """
015. 设置某个位置的值为value
016. """
017. self .items\[r]\[c]=value;
018. def  checkValue( self ,r,c,value):
019. """
020. 检测某个位置的值是否为value
021. """
022. if   self .items\[r]\[c]!=- 1   and   self .items\[r]\[c]==value:
023. self .items\[r]\[c]=- 1   #已经检测过
024. return   True
025. else :
026. return   False
027. def  countValue( self ,r,c,value):
028. """
029. 统计某个位置周围8个位置中，值为value的个数
030. """
031. count=0
032. if  r- 1 &gt;= 0   and  c- 1 &gt;= 0 :
033. if   self .items\[r- 1 ]\[c- 1 ]== 1 :count+= 1
034. if  r- 1 &gt;= 0   and  c&gt;= 0 :
035. if   self .items\[r- 1 ]\[c]== 1 :count+= 1
036. if  r- 1 &gt;= 0   and  c+ 1 &lt;= self .width- 1 :
037. if   self .items\[r- 1 ]\[c+ 1 ]== 1 :count+= 1
038. if  c- 1 &gt;= 0 :
039. if   self .items\[r]\[c- 1 ]== 1 :count+= 1
040. if  c+ 1 &lt;= self .width- 1  :
041. if   self .items\[r]\[c+ 1 ]== 1 :count+= 1
042. if  r+ 1 &lt;= self .height- 1   and  c- 1 &gt;= 0 :
043. if   self .items\[r+ 1 ]\[c- 1 ]== 1 :count+= 1
044. if  r+ 1 &lt;= self .height- 1  :
045. if   self .items\[r+ 1 ]\[c]== 1 :count+= 1
046. if  r+ 1 &lt;= self .height- 1   and  c+ 1 &lt;= self .width- 1 :
047. if   self .items\[r+ 1 ]\[c+ 1 ]== 1 :count+= 1
048. return  count
049. class  Mines(Frame):
050. def  \_\_init\_\_( self ,m,master= None ):
051. Frame.\_\_init\_\_(self ,master)
052. self .model=m
053. self .initmine()
054. self .grid()
055. self .createWidgets()
056. def  createWidgets( self ):
057. #top=self.winfo\_toplevel()
058. #top.rowconfigure(self.model.height\*2,weight=1)
059. #top.columnconfigure(self.model.width\*2,weight=1)
060. self .rowconfigure( self .model.height,weight= 1 )
061. self .columnconfigure( self .model.width,weight= 1 )
062. self .buttongroups=\[\[Button( self ,height= 1 ,width= 2 )  for  i  in  range( self .model.width)]
063. for  j  in  range( self .model.height)]
064. for  r  in  range( self .model.width):
065. for  c  in  range( self .model.height):
066. self .buttongroups\[r]\[c].grid(row=r,column=c)
067. self .buttongroups\[r]\[c].bind( '&lt;Button-1&gt;' , self .clickevent)
068. self .buttongroups\[r]\[c]\[ 'padx' ]=r
069. self .buttongroups\[r]\[c]\[ 'pady' ]=c
070. def  showall( self ):
071. for  r  in  range(model.height):
072. for  c  in  range(model.width):
073. self .showone(r,c)
074. def  showone( self ,r,c):
075. if  model.checkValue(r,c, 0 ):
076. self .buttongroups\[r]\[c]\[ 'text' ]=model.countValue(r,c, 1 )
077. else :
078. self .buttongroups\[r]\[c]\[ 'text' ]= 'Mines'
079. def  recureshow( self ,r,c):
080. if   0 &lt;=r&lt;= self .model.height- 1   and   0 &lt;=c&lt;= self .model.width- 1 :
081. if  model.checkValue(r,c, 0 )  and  model.countValue(r,c, 1 )== 0 :
082. self .buttongroups\[r]\[c]\[ 'text' ]= ''
083. self .recureshow(r- 1 ,c- 1 )
084. self .recureshow(r- 1 ,c)
085. self .recureshow(r- 1 ,c+ 1 )
086. self .recureshow(r,c- 1 )
087. self .recureshow(r,c+ 1 )
088. self .recureshow(r+ 1 ,c- 1 )
089. self .recureshow(r+ 1 ,c)
090. self .recureshow(r+ 1 ,c+ 1 )
091. elif  model.countValue(r,c, 1 )!= 0 :
092. self .buttongroups\[r]\[c]\[ 'text' ]=model.countValue(r,c, 1 )
093. else :
094. pass
095. def  clickevent( self ,event):
096. """
097. 点击事件
098. case 1:是雷，所有都显示出来，游戏结束
099. case 2:是周围雷数为0的，递归触发周围8个button的点击事件
100. case 3:周围雷数不为0的，显示周围雷数
101. """
102. r=int(str(event.widget\['padx' ]))
103. c=int(str(event.widget\['pady' ]))
104. if  model.checkValue(r,c, 1 ): #是雷
105. self .showall()
106. else : #不是雷
107. self .recureshow(r,c)
108. def  initmine( self ):
109. """
110. 埋雷,每行埋height/width+2个暂定
111. """
112. r=random.randint(1 ,model.height/model.width+ 2 )
113. for  r  in  range(model.height):
114. for  i  in  range( 2 ):
115. rancol=random.randint(0 ,model.width- 1 )
116. model.setItemValue(r,rancol,1 )
117. def  printf( self ):
118. """
119. 打印
120. """
121. for  r  in  range(model.height):
122. for  c  in  range(model.width):
123. print  model.items\[r]\[c],
124. print   '\\n'
125. def  new( self ):
126. """
127. 重新开始游戏
128. """
129. pass
130. if  \_\_name\_\_== '\_\_main\_\_' :
131. model=Model(10 , 10 )
132. root=Tk()
133. #menu
134. menu = Menu(root)
135. root.config(menu=menu)
136. filemenu = Menu(menu)
137. menu.add\_cascade(label="File" , menu=filemenu)
138. filemenu.add\_command(label="New" ,command=new)
139. filemenu.add\_separator()
140. filemenu.add\_command(label="Exit" , command=root.quit)
141. #Mines
142. m=Mines(model,root)
143. #m.printf()
144. root.mainloop()