完整的扫雷-Python

来源:互联网 发布:全国矢量数据下载 编辑:程序博客网 时间:2024/05/02 00:45
实现了完整的扫雷,但是很明显功能不如windows那个。首先,速度跟不上,所有格子出来的时候能明显的感觉到延迟;其次,没有那么好看,我本来想用地雷的图片,结果同一张图片不好放在多个按钮上,于是只有使用那个简单字符替代;最后,我不是完全清楚扫雷的规则,按照自己的理解设计算法,有的地方可能跟经典的扫雷有较大出入。
跟之前的纯粹的棋盘相比,这个完整的扫雷修改了一下棋盘。另外,我自己觉得之前设计棋盘的时候有的地方好像有点重复,但这个时候整个程序已经作了很多了,也不好推翻重来,如果各位有兴趣,希望能够进一步改进,并请将你的结果发到我的邮箱(hjjhegc@yahoo.com.cn),我好好学学,共同进步。至于我自己,恐怕不大会去进一步改进它了,接下来有很多事情要去做了,没有这么多闲功夫了。使用Py2exe转换成exe文件之后,在别人的没有Python环境的XP上也能够成功运行,但是整个需要的东西一共有5M多,实际上这个程序只有21K。。。。。。
整个程序500多行,有3个class。请不要嘲笑我的代码风格有问题,注释不够好,我只是做着玩,尽量做好,Python的很多东西都还没有学进去呢。做的时候因为Python的材料不多,我看Nutshell也不够,有时候也不够,还是自己去Tkinter.py的源码里边找的。整个原代码如下:
  1. #coding:UTF-8
  2. #Author: Hegc Huang
  3. import time, random
  4. import sys
  5. import Tkinter, tkMessageBox, tkFont
  6. from types import *
  7. __AUTHOR__  = "Hegc Huang"
  8. __NAME__    = "Mine Game"
  9. __VERSION__ = "1.0"
  10. __LOSE__    = 'lose.gif'
  11. __WIN__     = 'win.gif'
  12. __LBEVENT__ = '<ButtonRelease-1>'
  13. __RBEVENT__ = '<ButtonRelease-3>'
  14. __TITLE__   = 'Mine Game v1.0 - Hegc Huang for Jessica'
  15. #class for Mines
  16. class Mines:
  17.     """Class for the Mine Game's data structure.
  18.     
  19.     when initializing the game, note that:
  20.     Mine(minecount=16, width=9, height=None)
  21.     width: Chessboard's width.
  22.     minecount: Count of mines.
  23.     height: Chessboard's height.
  24.     minecount must less than width*height, and if height isn't set, it equals width as default.
  25.     so, Mines() means a 9*9 chessboard, and 16 mines;
  26.     Mines(17, 10) means 10*10 chessboard, and 17 mines;
  27.     Mines(17, 10, 11) means 10*11 chessboard, and 17 mines.
  28.     as a result of random generator, minecount may change to a realistic number.
  29.     """
  30.     
  31.     def __init__ (self, minecount=16, width=9, height=None):
  32.         if height == None:
  33.             height = width
  34.         self._width        = width       #for private use
  35.         self._minecount    = minecount     #for private use
  36.         self._height       = height       #
  37.         #print "width= %d, height= %d" % (width, height)
  38.         if self._minecount>=self._width * self._height:
  39.             print 'too small a chessboard. to exit.'
  40.             sys.exit()
  41.         self.mines = None
  42.         self.chessboard = None
  43.         self.reset(minecount, width, height)
  44.         #self.mines          = [0 for x in range(minecount)] #each 0, total minecount
  45.         #self.chessboard     = [[0 for x in range(width)] for y in range(height)]#size: width*height
  46.         #print self.chessboard
  47.         #self.__initialize()
  48.     def __initialize (self):
  49.         random.seed(time.time())    #set seed for random
  50.         count   = 0
  51.         size    = self._width * self._height - 1
  52.         while count<self._minecount:
  53.             randresult = int(random.random()*size + 1)    #random for chess
  54.             if not self.check(count, randresult):
  55.                 randresult = int(random.random()*size + 1)    #random for chess
  56.             self.mines[count] = randresult
  57.             count += 1
  58.             del randresult
  59.             #end initialize mines[]
  60.         #self.mines.sort()  #unuseful
  61.         #chessboard init
  62.         for r in self.mines:
  63.             x = r//self._width
  64.             y = r%self._width
  65.             #print 'x = %d, y = %d' % (x, y)
  66.             if self.chessboard[x][y] == -1:
  67.                 self.mines.remove(r)
  68.                 continue
  69.             self.chessboard[x][y] = -1
  70.         #
  71.         self._minecount = len(self.mines)
  72.         allmines = 0    #all indeed mines
  73.         cx = 0
  74.         while cx<self._height:
  75.             cy = 0
  76.             while cy<self._width:
  77.                 c = self.getcount(cx, cy)
  78.                 #print 'c =  ', c
  79.                 if c==-1:
  80.                     allmines += 1
  81.                 self.chessboard[cx][cy] = c
  82.                 cy += 1
  83.             #print self.chessboard[cx][:self._height]
  84.             cx += 1
  85.         #self.minecount = allmines
  86.         #end initialize chessboard[][]
  87.         #print "Mines : ", self.mines, ' ;XX; ', allmines
  88.         #print 'All mines = ', self._minecount
  89.     def check (self, count, rr):
  90.         if self.mines[:count].__contains__(rr):
  91.             return False
  92.         return True
  93.     #for external call
  94.     def ismine (self, x, y):
  95.         if self.chessboard[x][y] == -1:
  96.             return True;
  97.         return False
  98.     def getcount (self, x, y):
  99.         #print 'x=%d, y=%d' % (x, y)
  100.         ret = 0;
  101.         if self.chessboard[x][y]==-1:
  102.             ret =-1
  103.         elif x==0 and y==0 and self.chessboard[x][y]!=-1:
  104.             if self.chessboard[x+1][y] == -1:
  105.                 ret += 1
  106.             if self.chessboard[x+1][y+1] == -1:
  107.                 ret +=1
  108.             if self.chessboard[x][y+1] == -1:
  109.                 ret +=1
  110.         elif x==self._height-1 and y==self._width-1 and self.chessboard[x][y]!=-1:
  111.             if self.chessboard[x-1][y] == -1:
  112.                 ret += 1
  113.             if self.chessboard[x-1][y-1] == -1:
  114.                 ret +=1
  115.             if self.chessboard[x][y-1] == -1:
  116.                 ret +=1
  117.         elif y==0 and self.chessboard[x][y]!=-1:
  118.             if self.chessboard[x-1][y] == -1:
  119.                 ret += 1
  120.             if self.chessboard[x-1][y+1] == -1:
  121.                 ret +=1
  122.             if self.chessboard[x][y+1] == -1:
  123.                 ret +=1
  124.             if x < self._height-1:
  125.                 if self.chessboard[x+1][y] == -1:
  126.                     ret += 1
  127.                 if self.chessboard[x+1][y+1] == -1:
  128.                     ret += 1
  129.         elif x==0 and self.chessboard[x][y]!=-1:
  130.             if self.chessboard[x+1][y] == -1:
  131.                 ret += 1
  132.             if self.chessboard[x+1][y-1] == -1:
  133.                 ret +=1
  134.             if self.chessboard[x][y-1] == -1:
  135.                 ret +=1
  136.             if y < self._width-1:
  137.                 if self.chessboard[x+1][y+1] == -1:
  138.                     ret += 1
  139.                 if self.chessboard[x][y+1] == -1:
  140.                     ret += 1
  141.         elif x==self._height-1 and self.chessboard[x][y]!=-1:
  142.             if self.chessboard[x-1][y] == -1:
  143.                 ret += 1
  144.             if self.chessboard[x-1][y-1] == -1:
  145.                 ret +=1
  146.             if self.chessboard[x][y-1] == -1:
  147.                 ret +=1
  148.             if self.chessboard[x-1][y+1] == -1:
  149.                 ret += 1
  150.             if self.chessboard[x][y+1] == -1:
  151.                 ret += 1
  152.         elif y==self._width-1 and self.chessboard[x][y]!=-1:
  153.             if self.chessboard[x-1][y] == -1:
  154.                 ret += 1
  155.             if self.chessboard[x+1][y] == -1:
  156.                 ret +=1
  157.             if self.chessboard[x][y-1] == -1:
  158.                 ret +=1
  159.             if self.chessboard[x+1][y-1] == -1:
  160.                 ret += 1
  161.             if self.chessboard[x-1][y-1] == -1:
  162.                 ret += 1
  163.         elif self.chessboard[x][y]!=-1:
  164.             if self.chessboard[x-1][y-1] == -1:
  165.                 ret += 1
  166.             if self.chessboard[x-1][y] == -1:
  167.                 ret +=1
  168.             if self.chessboard[x-1][y+1] == -1:
  169.                 ret +=1
  170.             if self.chessboard[x][y+1] == -1:
  171.                 ret += 1
  172.             if self.chessboard[x+1][y+1] == -1:
  173.                 ret += 1
  174.             if self.chessboard[x+1][y] == -1:
  175.                 ret +=1
  176.             if self.chessboard[x+1][y-1] == -1:
  177.                 ret += 1
  178.             if self.chessboard[x][y-1] == -1:
  179.                 ret += 1
  180.         
  181.        
  182.         return ret
  183.         #end getcount
  184.     def reset (self, minecount = 16, width = 9, height = 9):
  185.         if self.mines:
  186.             del self.mines
  187.         if self.chessboard:
  188.             del self.chessboard
  189.         self._width         = width
  190.         self._height        = height
  191.         self._minecount     = minecount
  192.         self.mines          = [0 for x in range(minecount)] #each 0, total minecount
  193.         self.chessboard     = [[0 for x in range(width)] for y in range(height)]#size: width*height
  194.         self.__initialize()
  195.         #print self.chessboard
  196. #the following two methods is from Tkinter.py
  197. def _flatten(tuple):
  198.     """Internal function."""
  199.     res = ()
  200.     for item in tuple:
  201.         if type(item) in (TupleType, ListType):
  202.             res = res + _flatten(item)
  203.         elif item is not None:
  204.             res = res + (item,)
  205.     return res
  206. def _cnfmerge(cnfs):
  207.     """Internal function."""
  208.     if type(cnfs) is DictionaryType:
  209.         return cnfs
  210.     elif type(cnfs) in (NoneType, StringType):
  211.         return cnfs
  212.     else:
  213.         cnf = {}
  214.         for c in _flatten(cnfs):
  215.             try:
  216.                 cnf.update(c)
  217.             except (AttributeError, TypeError), msg:
  218.                 print "_cnfmerge: fallback due to:", msg
  219.                 for k, v in c.items():
  220.                     cnf[k] = v
  221.         return cnf
  222. #class for Game
  223. class Game:
  224.     def __init__ (self, master, mines):
  225.         self._mines = mines
  226.         self._errormines = []
  227.         self._master = master
  228.         self._font = tkFont.Font(weight=tkFont.BOLD, size=12)
  229.         self._allcount = mines._minecount
  230.         self.createimage()
  231.         self.initframe()
  232.     def createimage (self):
  233.         self.__LOSE__ = Tkinter.PhotoImage(file=__LOSE__)
  234.         self.__WIN__  = Tkinter.PhotoImage(file=__WIN__)
  235.     def power (self, event):
  236.         w = event.widget
  237.         t = w.cget('text')
  238.         if t == 'l':
  239.             w.config(text='w', image=self.__WIN__)
  240.         _m = self._mines
  241.         self.restart(_m._minecount, _m._width, _m._height)
  242.     def restart (self, ms, row, column, rs = True): #rs = True for real restart
  243.         #print 'ms = %d; r = %d, c = %d' % (ms, row, column)
  244.         if self.mainframe:
  245.             self.mainframe.destroy()
  246.         if rs:
  247.             self._mines.reset(ms, row, column)
  248.         self._allcount = self._mines._minecount
  249.         self._errormines = []
  250.         self.countlabel.config(text=self._allcount, font=self._font, fg='#FA3CAD')
  251.         self.mainframe = Tkinter.Frame(self._master)
  252.         self.buildmain(self.mainframe)
  253.         self.mainframe.pack(side=Tkinter.BOTTOM, fill=Tkinter.BOTH)
  254.     def initframe (self):
  255.         self.funcframe = Tkinter.Frame(self._master)
  256.         #
  257.         self.countlabel = Tkinter.Label(self.funcframe)   #display the number of mines
  258.         self.countlabel.pack(side = Tkinter.LEFT, expand=3, fill=Tkinter.BOTH)
  259.         #
  260.         #self.canvas = Tkinter.Canvas(self.funcframe)
  261.         #self.canvas.pack(side = Tkinter.LEFT, fill=Tkinter.X)
  262.         #self.canvas.create_text(10, 10, text=self._mines.minecount, fill='#FF3333')
  263.         #
  264.         self.powerbutton = Tkinter.Button(self.funcframe, image=self.__WIN__, text='w')
  265.         self.powerbutton.bind(__LBEVENT__, self.power)
  266.         self.powerbutton.pack(side=Tkinter.RIGHT)
  267.         #
  268.         self.funcframe.pack(side=Tkinter.TOP)
  269.         #
  270.         self.mainframe = None
  271.         _m = self._mines
  272.         self.restart(_m._minecount, _m._width, _m._height, False)#use restart to init
  273.     def buildmain (self, mf):
  274.         width  = self._mines._width
  275.         height = self._mines._height
  276.         #mf     = self.mainframe
  277.         r = 0
  278.         #print "R=%d, C=%d" % (height, width)
  279.         while r < height:
  280.             c = 0
  281.             while c < width:
  282.                 button = Tkinter.Button(mf,  text='', width=2, height=1)#width=2, height=1
  283.                 button.grid(row=r,column=c,rowspan=1, columnspan=1)
  284.                 button.bind(__LBEVENT__, self.lbclick)
  285.                 button.bind(__RBEVENT__, self.lbclick)
  286.                 #print "c = %d" % c
  287.                 c += 1
  288.             r += 1
  289.             #print "r = %d" % r
  290.     def lbclick (self, event):#handle click the chessboard
  291.         w = event.widget
  292.         g = w.grid_info()
  293.         r = int(g['row'])   #for row
  294.         c = int(g['column'])#for column
  295.         mnum = event.num    #mouse button
  296.         #print u"鼠标num = ", mnum
  297.         if mnum == 1:       #left button
  298.             self.clickleft(w, r, c)
  299.         elif mnum == 3:     #right button
  300.             self.clickright(w, r, c)
  301.         #if self._mines.ismine(r, c):
  302.     def clickright (self, w, r, c):
  303.         t = w.cget('text')
  304.         if t=='?':
  305.             w.config(text='')
  306.             w.bind(__LBEVENT__, self.lbclick)
  307.         elif t=='$':
  308.             w.config(text='?')
  309.             w.unbind(__LBEVENT__)
  310.             _m = self._mines
  311.             ml = r*_m._width + c    #mine location
  312.             if not _m.ismine(r,c):
  313.                 self._errormines.remove(ml)
  314.                 self._allcount += 1 #allcount + 1
  315.                 self.countlabel.config(text=self._allcount, font=self._font, fg='#FA3CAD')
  316.             else:
  317.                 _m.mines.append(ml)
  318.         else:
  319.             w.config(text='$')
  320.             w.unbind(__LBEVENT__)
  321.             _m = self._mines
  322.             ml = r*_m._width + c    #mine location
  323.             if not _m.ismine(r,c):
  324.                 self._errormines.append(ml)
  325.                 self._allcount -= 1 #allcount - 1
  326.                 self.countlabel.config(text=self._allcount, font=self._font, fg='#FA3CAD')
  327.             else:
  328.                 _m.mines.remove(ml)
  329.                 self._allcount -= 1 #allcount - 1
  330.                 self.countlabel.config(text=self._allcount, font=self._font, fg='#FA3CAD')
  331.             if self._allcount==0:   #all found
  332.                 if len(self._errormines)>0#some error
  333.                     self._showresult(r, c)
  334.                 else:
  335.                     self._showresult(r, c, 'WIN')
  336.     def _showresult (self, r, c, result = 'LOSE'):
  337.         if result == 'WIN':
  338.             tkMessageBox.showinfo(message=u'Congratulations!!/nYou Winned.', title=u'Win')
  339.             return;
  340.         self.powerbutton.config(text='l', image=self.__LOSE__)
  341.         for m in self._errormines:
  342.             x = m//self._mines._width
  343.             y = m%self._mines._width
  344.             self._forbutton(x, y, True, text='X', state=Tkinter.DISABLED, disabledforeground='#FFAA00', relief=Tkinter.FLAT)   #show all error mines
  345.         for m in self._mines.mines:      
  346.             x = m//self._mines._width
  347.             y = m%self._mines._width
  348.             self._forbutton(x, y, True, text='#', state=Tkinter.DISABLED, disabledforeground='#FFAFCD', relief=Tkinter.FLAT)   #show all mines, and unbind all action
  349.         tkMessageBox.showinfo(message=u'Sorry!!/nBut you Failed.', title=u'Fail')    #third, tell the result 'LOSE'
  350.     def clickleft (self, w, r, c):
  351.         if self._mines.ismine(r, c):    #click a mine, game over
  352.             ml = r*self._mines._width + c
  353.             self._mines.mines.remove(ml)
  354.             self._forbutton(r, c, True, text='#', state=Tkinter.DISABLED, disabledforeground='#FF0000', relief=Tkinter.FLAT)
  355.             #for m in self._mines.mines:      
  356.             #    x = m//self._mines._width
  357.             #    y = m%self._mines._width
  358.             #    self._forbutton(x, y, True, text='#', state=Tkinter.DISABLED, disabledforeground='#FF0000', relief=Tkinter.FLAT)   #show all mines, and unbind all action
  359.             #self.powerbutton.config(text='l', image=self.__LOSE__)   
  360.             #tkMessageBox.showinfo(message=u'踩到雷了!!', title=u'失败')    #third, tell the result 'LOSE'
  361.             self._showresult(r, c)
  362.         else:
  363.             m = self._mines
  364.             t = ''
  365.             n = m.chessboard[r][c]
  366.             if n>0:
  367.                 t = '%s' % n
  368.             w.config(relief = Tkinter.FLAT, state = Tkinter.DISABLED, text = t)
  369.             self.unbind(w)      #disable, and unbind
  370.             if n==0:    #none around
  371.                 self._showaround(r, c)
  372.     def _forbutton (self, r, c, ub, cnf={}, **kw): #imitate the Button.__init__(), in fact, 'n=v's impose on kw(tuple). ub true for unbind
  373.         bt = self.mainframe.grid_slaves(row=r, column=c)[0]
  374.         state = bt.cget('state')
  375.         if state == Tkinter.DISABLED:
  376.             return 'done'
  377.         #bt.config(relief = Tkinter.FLAT, state = st, text = t, disabledforeground = dfg)
  378.         if kw:
  379.             #print 'cnf = ', cnf, ' ; kw = ', kw    #cnf is alwayes {}
  380.             cnf = _cnfmerge((cnf, kw))
  381.             #print 'CNF = ', cnf
  382.         bt.config(cnf)
  383.         if ub:
  384.             self.unbind (bt)
  385.         return None
  386.     def _decide (self, r, c):
  387.         n = self._mines.chessboard[r][c]
  388.         if n==0:
  389.             st = self._forbutton(r, c, True, state=Tkinter.DISABLED, relief=Tkinter.FLAT)
  390.             #print 'st = ', st
  391.             if st == None#hasn't handled
  392.                 self._showaround(r, c)
  393.         else:
  394.             self._forbutton(r, c, True, text = '%s' % n, state=Tkinter.DISABLED, relief=Tkinter.FLAT)
  395.     def _showaround (self, r, c):
  396.         cb = self._mines.chessboard
  397.         if r>0:     
  398.             _r = r-1    #north
  399.             _c = c
  400.             #self._forbutton(_r, _c, '')
  401.             self._decide(_r, _c)
  402.             if c>0:     #northwest
  403.                 _r = r-1
  404.                 _c = c-1
  405.                 #self._forbutton(_r, _c, '')
  406.                 self._decide(_r, _c)
  407.             if c<self._mines._width-1#northeast
  408.                 _r = r-1
  409.                 _c = c+1
  410.                 #self._forbutton(_r, _c, '')
  411.                 self._decide(_r, _c)
  412.         if r<self._mines._height-1:
  413.             _r = r+1    #south
  414.             _c = c
  415.             #self._forbutton(_r, _c, '')
  416.             self._decide(_r, _c)
  417.             if c>0:     #southwest
  418.                 _r = r+1
  419.                 _c = c-1
  420.                 #self._forbutton(_r, _c, '')
  421.                 self._decide(_r, _c)
  422.             if c<self._mines._width-1#southeast
  423.                 _r = r+1
  424.                 _c = c+1
  425.                 #self._forbutton(_r, _c, '')
  426.                 self._decide(_r, _c)
  427.         if c>0:     #west
  428.             _r = r
  429.             _c = c-1
  430.             #self._forbutton(_r, _c, '')
  431.             self._decide(_r, _c)
  432.         if c<self._mines._width-1:  #east
  433.             _r = r
  434.             _c = c+1
  435.             #self._forbutton(_r, _c, '')
  436.             self._decide(_r, _c)
  437.     def unbind (self, w):
  438.         w.unbind(__LBEVENT__)
  439.         w.unbind(__RBEVENT__)
  440. #main class
  441. class MineGame:
  442.     def __init__ (self):
  443.         self.mine = Mines(401616)   #默认:中级
  444.         self.root = Tkinter.Tk()
  445.         self.root.title(__TITLE__)
  446.         self.game = Game(self.root, self.mine)
  447.         self._addmenu(self.root)
  448.         self.root.wm_resizable(FalseFalse)    #can not resize
  449.         self.root.mainloop()
  450.     def _addmenu (self, _master):
  451.         bar = Tkinter.Menu()
  452.         fil = Tkinter.Menu()
  453.         for x in u'初级', u'中级', u'高级', u'自定义''-', u'退出':
  454.             if x=='-':
  455.                 fil.add_separator()
  456.             else:
  457.                 fil.add_command(label=x, command = lambda x=x: self.newgame(x))
  458.         bar.add_cascade(label=u'游戏', menu=fil)
  459.         _master.config(menu = bar)
  460.     def newgame (self, x):
  461.         if x==u'初级':
  462.             self.game.restart(1099)
  463.         elif x==u'中级':
  464.             self.game.restart(401616)
  465.         elif x==u'高级':
  466.             self.game.restart(993016)
  467.         elif x==u'自定义':
  468.             def _okb ():
  469.                 try:
  470.                     wd = int(wfunc.get())
  471.                     hg = int(hfunc.get())
  472.                     mc = int(cfunc.get())
  473.                 except TypeError:
  474.                     tkMessageBox.showwarning(message=u'请输入正确的数字', title='警告')
  475.                     return
  476.                 
  477.                 m = wd * hg
  478.                 if m//mc>10:
  479.                     tkMessageBox.showwarning(message=u'棋盘太大了,而雷太少了。/n请确保地雷数目不少于棋盘大小的1/10。', title='警告')
  480.                     return
  481.                 elif m//mc<3:
  482.                     tkMessageBox.showwarning(message=u'棋盘太小了,而雷太多了。/n请确保地雷数目不超过棋盘大小的1/4。', title='警告')
  483.                     return
  484.                 root.destroy()
  485.                 self.root.wm_resizable(TrueTrue)
  486.                 self.game.restart(mc, wd, hg)
  487.                 self.root.wm_resizable(FalseFalse)
  488.             def _ccb ():
  489.                 root.destroy()
  490.             root = Tkinter.Tk()
  491.             fram = Tkinter.Frame(root)
  492.             Tkinter.Label(fram, text=u'宽度(6~30  ): ').pack(side=Tkinter.LEFT)
  493.             wfunc = Tkinter.Entry(fram)
  494.             wfunc.pack(side=Tkinter.RIGHT, fill=Tkinter.BOTH, expand=1)
  495.             fram.pack()
  496.             fram = Tkinter.Frame(root)
  497.             Tkinter.Label(fram, text=u'高度(6~24  ): ').pack(side=Tkinter.LEFT)
  498.             hfunc = Tkinter.Entry(fram)
  499.             hfunc.pack(side=Tkinter.RIGHT, fill=Tkinter.BOTH, expand=1)
  500.             fram.pack()
  501.             fram = Tkinter.Frame(root)
  502.             Tkinter.Label(fram, text=u'雷数(6~180): ').pack(side=Tkinter.LEFT)
  503.             cfunc = Tkinter.Entry(fram)
  504.             cfunc.pack(side=Tkinter.RIGHT, fill=Tkinter.BOTH, expand=1)
  505.             fram.pack()
  506.             fram = Tkinter.Frame(root)
  507.             Tkinter.Button(fram,text=u'确定', command=_okb).pack(side= Tkinter.LEFT)
  508.             Tkinter.Button(fram,text=u'取消', command=_ccb).pack(side= Tkinter.RIGHT)
  509.             fram.pack()
  510.             root.mainloop()
  511.         elif x==u'退出':
  512.             sys.exit()
  513. #main start function
  514. def start (arg):
  515.     if arg=='c':
  516.         mines = Mines(169)
  517.         print Mines.__doc__
  518.         _l = len(mines.chessboard)
  519.         l = 0
  520.         while l<_l:
  521.             for i in mines.chessboard[:][l]:
  522.                 if i==-1:
  523.                     i = '/x0F'
  524.                 print i, ' ',/
  525.             
  526.             print ''
  527.             l += 1
  528.     else:
  529.         MineGame()
  530.         
  531. argc = len(sys.argv)
  532. if argc>1:
  533.     arg = sys.argv[1]
  534.     start(arg)
  535. else:
  536.     start('g')

原创粉丝点击