扫雷游戏 python实现

扫雷游戏 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.   
  6. class  Model:  
  7.     """  
  8.     核心数据类,维护一个矩阵  
  9.     """   
  10.     def  __init__( self ,row,col):  
  11.         self .width=col  
  12.         self .height=row  
  13.         self .items=[[ 0   for  c  in  range(col)]  for  r  in  range(row)]  
  14.   
  15.     def  setItemValue( self ,r,c,value):  
  16.         """  
  17.         设置某个位置的值为value  
  18.         """   
  19.         self .items[r][c]=value;  
  20.   
  21.     def  checkValue( self ,r,c,value):  
  22.         """  
  23.         检测某个位置的值是否为value  
  24.         """   
  25.         if   self .items[r][c]!=- 1   and   self .items[r][c]==value:  
  26.             self .items[r][c]=- 1   #已经检测过   
  27.             return   True   
  28.         else :  
  29.             return   False   
  30.           
  31.     def  countValue( self ,r,c,value):  
  32.         """  
  33.         统计某个位置周围8个位置中,值为value的个数  
  34.         """   
  35.         count=0   
  36.         if  r- 1 >= 0   and  c- 1 >= 0 :  
  37.             if   self .items[r- 1 ][c- 1 ]== 1 :count+= 1   
  38.         if  r- 1 >= 0   and  c>= 0 :  
  39.             if   self .items[r- 1 ][c]== 1 :count+= 1   
  40.         if  r- 1 >= 0   and  c+ 1 <= self .width- 1 :  
  41.             if   self .items[r- 1 ][c+ 1 ]== 1 :count+= 1   
  42.         if  c- 1 >= 0 :  
  43.             if   self .items[r][c- 1 ]== 1 :count+= 1   
  44.         if  c+ 1 <= self .width- 1  :  
  45.             if   self .items[r][c+ 1 ]== 1 :count+= 1   
  46.         if  r+ 1 <= self .height- 1   and  c- 1 >= 0 :  
  47.             if   self .items[r+ 1 ][c- 1 ]== 1 :count+= 1   
  48.         if  r+ 1 <= self .height- 1  :  
  49.             if   self .items[r+ 1 ][c]== 1 :count+= 1   
  50.         if  r+ 1 <= self .height- 1   and  c+ 1 <= self .width- 1 :  
  51.             if   self .items[r+ 1 ][c+ 1 ]== 1 :count+= 1   
  52.         return  count  
  53.   
  54.       
  55. class  Mines(Frame):  
  56.     def  __init__( self ,m,master= None ):  
  57.         Frame.__init__(self ,master)  
  58.         self .model=m  
  59.         self .initmine()  
  60.         self .grid()  
  61.         self .createWidgets()  
  62.   
  63.    
  64.       
  65.     def  createWidgets( self ):  
  66.         #top=self.winfo_toplevel()   
  67.         #top.rowconfigure(self.model.height*2,weight=1)   
  68.         #top.columnconfigure(self.model.width*2,weight=1)   
  69.         self .rowconfigure( self .model.height,weight= 1 )  
  70.         self .columnconfigure( self .model.width,weight= 1 )  
  71.         self .buttongroups=[[Button( self ,height= 1 ,width= 2for  i  in  range( self .model.width)]  
  72.                            for  j  in  range( self .model.height)]  
  73.         for  r  in  range( self .model.width):  
  74.             for  c  in  range( self .model.height):  
  75.                 self .buttongroups[r][c].grid(row=r,column=c)  
  76.                 self .buttongroups[r][c].bind( '<Button-1>' , self .clickevent)  
  77.                 self .buttongroups[r][c][ 'padx' ]=r  
  78.                 self .buttongroups[r][c][ 'pady' ]=c  
  79.   
  80.     def  showall( self ):  
  81.         for  r  in  range(model.height):  
  82.             for  c  in  range(model.width):  
  83.                 self .showone(r,c)  
  84.   
  85.     def  showone( self ,r,c):  
  86.         if  model.checkValue(r,c, 0 ):  
  87.             self .buttongroups[r][c][ 'text' ]=model.countValue(r,c, 1 )  
  88.         else :  
  89.             self .buttongroups[r][c][ 'text' ]= 'Mines'   
  90.   
  91.     def  recureshow( self ,r,c):  
  92.         if   0 <=r<= self .model.height- 1   and   0 <=c<= self .model.width- 1 :  
  93.             if  model.checkValue(r,c, 0and  model.countValue(r,c, 1 )== 0 :  
  94.                 self .buttongroups[r][c][ 'text' ]= ''   
  95.                 self .recureshow(r- 1 ,c- 1 )  
  96.                 self .recureshow(r- 1 ,c)  
  97.                 self .recureshow(r- 1 ,c+ 1 )  
  98.                 self .recureshow(r,c- 1 )  
  99.                 self .recureshow(r,c+ 1 )  
  100.                 self .recureshow(r+ 1 ,c- 1 )  
  101.                 self .recureshow(r+ 1 ,c)  
  102.                 self .recureshow(r+ 1 ,c+ 1 )  
  103.             elif  model.countValue(r,c, 1 )!= 0 :  
  104.                 self .buttongroups[r][c][ 'text' ]=model.countValue(r,c, 1 )  
  105.         else :  
  106.             pass   
  107.                   
  108.               
  109.     def  clickevent( self ,event):  
  110.         """  
  111.         点击事件  
  112.         case 1:是雷,所有都显示出来,游戏结束  
  113.         case 2:是周围雷数为0的,递归触发周围8个button的点击事件  
  114.         case 3:周围雷数不为0的,显示周围雷数  
  115.         """   
  116.         r=int(str(event.widget['padx' ]))  
  117.         c=int(str(event.widget['pady' ]))  
  118.         if  model.checkValue(r,c, 1 ): #是雷   
  119.             self .showall()  
  120.         else : #不是雷   
  121.             self .recureshow(r,c)  
  122.           
  123.           
  124.     def  initmine( self ):  
  125.         """  
  126.         埋雷,每行埋height/width+2个暂定  
  127.         """   
  128.         r=random.randint(1 ,model.height/model.width+ 2 )  
  129.         for  r  in  range(model.height):  
  130.             for  i  in  range( 2 ):  
  131.                 rancol=random.randint(0 ,model.width- 1 )  
  132.                 model.setItemValue(r,rancol,1 )  
  133.   
  134.       
  135.     def  printf( self ):  
  136.         """  
  137.         打印  
  138.         """   
  139.         for  r  in  range(model.height):  
  140.             for  c  in  range(model.width):  
  141.                 print  model.items[r][c],  
  142.             print   '\n'   
  143.              
  144.   
  145. def  new( self ):  
  146.     """  
  147.     重新开始游戏  
  148.     """   
  149.     pass   
  150.   
  151. if  __name__== '__main__' :  
  152.     model=Model(10 , 10 )  
  153.     root=Tk()  
  154.       
  155.     #menu   
  156.     menu = Menu(root)  
  157.     root.config(menu=menu)  
  158.     filemenu = Menu(menu)  
  159.     menu.add_cascade(label="File" , menu=filemenu)  
  160.     filemenu.add_command(label="New" ,command=new)  
  161.     filemenu.add_separator()  
  162.     filemenu.add_command(label="Exit" , command=root.quit)  
  163.   
  164.     #Mines   
  165.     m=Mines(model,root)  
  166.     #m.printf()   
  167.     root.mainloop()  

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值