ITeye博客

扫雷游戏 python实现

acen.chen · 2009-04-27 09:26 · python · Markdown

扫雷游戏 python实现

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

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

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


  1. # -*- coding: utf-8 -*-
  2. import  random
  3. import  sys
  4. from  Tkinter  import  *
  5. class  Model:
  6. """
  7. 核心数据类,维护一个矩阵
  8. """
  9. def  __init__( self ,row,col):
  10. self .width=col
  11. self .height=row
  12. self .items=[[ 0   for  c  in  range(col)]  for  r  in  range(row)]
  13. def  setItemValue( self ,r,c,value):
  14. """
  15. 设置某个位置的值为value
  16. """
  17. self .items[r][c]=value;
  18. def  checkValue( self ,r,c,value):
  19. """
  20. 检测某个位置的值是否为value
  21. """
  22. if   self .items[r][c]!=- 1   and   self .items[r][c]==value:
  23. self .items[r][c]=- 1   #已经检测过
  24. return   True
  25. else :
  26. return   False
  27. def  countValue( self ,r,c,value):
  28. """
  29. 统计某个位置周围8个位置中,值为value的个数
  30. """
  31. count=0
  32. if  r- 1 >= 0   and  c- 1 >= 0 :
  33. if   self .items[r- 1 ][c- 1 ]== 1 :count+= 1
  34. if  r- 1 >= 0   and  c>= 0 :
  35. if   self .items[r- 1 ][c]== 1 :count+= 1
  36. if  r- 1 >= 0   and  c+ 1 <= self .width- 1 :
  37. if   self .items[r- 1 ][c+ 1 ]== 1 :count+= 1
  38. if  c- 1 >= 0 :
  39. if   self .items[r][c- 1 ]== 1 :count+= 1
  40. if  c+ 1 <= self .width- 1  :
  41. if   self .items[r][c+ 1 ]== 1 :count+= 1
  42. if  r+ 1 <= self .height- 1   and  c- 1 >= 0 :
  43. if   self .items[r+ 1 ][c- 1 ]== 1 :count+= 1
  44. if  r+ 1 <= self .height- 1  :
  45. if   self .items[r+ 1 ][c]== 1 :count+= 1
  46. if  r+ 1 <= self .height- 1   and  c+ 1 <= self .width- 1 :
  47. if   self .items[r+ 1 ][c+ 1 ]== 1 :count+= 1
  48. return  count
  49. class  Mines(Frame):
  50. def  __init__( self ,m,master= None ):
  51. Frame.__init__(self ,master)
  52. self .model=m
  53. self .initmine()
  54. self .grid()
  55. self .createWidgets()
  56. def  createWidgets( self ):
  57. #top=self.winfo_toplevel()
  58. #top.rowconfigure(self.model.height*2,weight=1)
  59. #top.columnconfigure(self.model.width*2,weight=1)
  60. self .rowconfigure( self .model.height,weight= 1 )
  61. self .columnconfigure( self .model.width,weight= 1 )
  62. self .buttongroups=[[Button( self ,height= 1 ,width= 2 )  for  i  in  range( self .model.width)]
  63. for  j  in  range( self .model.height)]
  64. for  r  in  range( self .model.width):
  65. for  c  in  range( self .model.height):
  66. self .buttongroups[r][c].grid(row=r,column=c)
  67. self .buttongroups[r][c].bind( '<Button-1>' , self .clickevent)
  68. self .buttongroups[r][c][ 'padx' ]=r
  69. self .buttongroups[r][c][ 'pady' ]=c
  70. def  showall( self ):
  71. for  r  in  range(model.height):
  72. for  c  in  range(model.width):
  73. self .showone(r,c)
  74. def  showone( self ,r,c):
  75. if  model.checkValue(r,c, 0 ):
  76. self .buttongroups[r][c][ 'text' ]=model.countValue(r,c, 1 )
  77. else :
  78. self .buttongroups[r][c][ 'text' ]= 'Mines'
  79. def  recureshow( self ,r,c):
  80. if   0 <=r<= self .model.height- 1   and   0 <=c<= self .model.width- 1 :
  81. if  model.checkValue(r,c, 0 )  and  model.countValue(r,c, 1 )== 0 :
  82. self .buttongroups[r][c][ 'text' ]= ''
  83. self .recureshow(r- 1 ,c- 1 )
  84. self .recureshow(r- 1 ,c)
  85. self .recureshow(r- 1 ,c+ 1 )
  86. self .recureshow(r,c- 1 )
  87. self .recureshow(r,c+ 1 )
  88. self .recureshow(r+ 1 ,c- 1 )
  89. self .recureshow(r+ 1 ,c)
  90. self .recureshow(r+ 1 ,c+ 1 )
  91. elif  model.countValue(r,c, 1 )!= 0 :
  92. self .buttongroups[r][c][ 'text' ]=model.countValue(r,c, 1 )
  93. else :
  94. pass
  95. def  clickevent( self ,event):
  96. """
  97. 点击事件
  98. case 1:是雷,所有都显示出来,游戏结束
  99. 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()

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。