Advertisement
Guest User

Minesweeper PyQt4

a guest
Jun 6th, 2010
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.75 KB | None | 0 0
  1. #!/usr/bin/python
  2. """
  3. Mine Sweeper Game
  4. """
  5.  
  6. import optparse
  7. import sys
  8. import os
  9. import re
  10. import subprocess
  11. import random
  12. from PyQt4 import QtCore, QtGui
  13.  
  14. #-----------------------------------------------------
  15. #-----------------------------------------------------
  16. #-----------------------------------------------------
  17.  
  18. class MyButton(QtGui.QPushButton):
  19.   def __init__(self, parent=None):
  20.     super(MyButton, self).__init__(parent)
  21.   def contextMenuEvent(self, event):
  22.     if self.text() == "?":
  23.       self.setText("")
  24.       self.setPalette(QtGui.QPalette(QtGui.QColor(222,222,222)))
  25.       self.parent.marked -= 1
  26.     else:
  27.       self.setText("?")
  28.       self.setPalette(QtGui.QPalette(QtGui.QColor(222,255,222)))
  29.       self.parent.marked += 1
  30.     self.parent.bombcount.setText("%d/40" %self.parent.marked)
  31.  
  32. #-----------------------------------------------------
  33. #-----------------------------------------------------
  34. #-----------------------------------------------------
  35. class MainWindow(QtGui.QWidget):
  36.   """ Main Wrapper For GUI """
  37.   def __init__(self, parent=None):
  38.     super(MainWindow, self).__init__(parent)
  39.     setting = QtCore.QSettings()
  40.     tmp = setting.value("position", QtCore.QPoint(0,0))
  41. #    self.move(tmp)
  42.     layout = QtGui.QGridLayout()
  43.     layout.setMargin(1)
  44.     layout.setSpacing(1)
  45.     self.button = []
  46.     for r in range(16):
  47.       self.button.append( [] )
  48.       for c in range(16):
  49.         self.button[r].append( MyButton() )
  50.         self.button[r][c].setFixedSize(20,20)
  51.         self.button[r][c].row = r
  52.         self.button[r][c].col = c
  53.         self.button[r][c].parent = self
  54.         self.button[r][c].revealed = False
  55.         layout.addWidget( self.button[r][c], r, c)
  56.         QtCore.QObject.connect(self.button[r][c], QtCore.SIGNAL("clicked()"), self.revealButtonWrapper )
  57.     layouttop = QtGui.QHBoxLayout()
  58.     layouttop.addWidget(QtGui.QLabel("BombCount"))
  59.     self.bombcount = QtGui.QLabel("0/40")
  60.     layouttop.addWidget(self.bombcount)
  61.     self.timer = QtCore.QTimer()
  62.     self.timelabel = QtGui.QLabel("0.0 secs")
  63.     self.timelabel.setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(0,255,0)")
  64.     self.newGame()
  65.     layouttop.addWidget(self.timelabel)
  66.     newgameButton = QtGui.QPushButton("&NewGame")
  67.     newgameButton.setEnabled(True)
  68.     layouttop.addWidget(newgameButton)
  69.     topscore = QtGui.QPushButton("Top&Score>>")
  70.     topscore.setCheckable(True)
  71.     layouttop.addWidget(topscore)
  72.     self.scoreframe = QtGui.QFrame()
  73.     self.scoreframe.setFrameStyle(QtGui.QFrame.StyledPanel|QtGui.QFrame.Sunken)
  74.     scorelayout = QtGui.QVBoxLayout()
  75.     scorelayout.addWidget(QtGui.QLabel("<b>Created By</b> <i>-Lionel Tan-</i> <br><br><br>" +
  76.                                         "Email:<a href=#>yltan@altera.com</a><br>" +
  77.                                         "Ext  :<a href=#>#6315</a> <br><br>" +
  78.                                         "<a href=#>http://lionel.textmalaysia.com</a><br>") )
  79.     self.scoreframe.setLayout(scorelayout)
  80.     self.scoreframe.setVisible(False)
  81.     mainLayout = QtGui.QGridLayout()
  82.     mainLayout.addLayout(layouttop,0,0)
  83.     mainLayout.addLayout(layout,1,0)
  84.     mainLayout.addWidget(self.scoreframe,0,1,2,1)
  85.     self.setLayout(mainLayout)
  86.     self.layout().setSizeConstraint(QtGui.QLayout.SetFixedSize)
  87.     QtCore.QObject.connect(newgameButton, QtCore.SIGNAL("clicked()"), self.newGame)
  88.     QtCore.QObject.connect(self.timer, QtCore.SIGNAL("timeout()"), self.updateTime)
  89.     QtCore.QObject.connect(topscore, QtCore.SIGNAL("clicked(bool)"), self.scoreframe.setVisible )
  90.  
  91. #-----------------------------------------------------
  92. #-----------------------------------------------------
  93. #-----------------------------------------------------
  94.   def newGame(self):
  95.     self.populateBombs()
  96.     self.marked = 0
  97.     self.gamestarted = 0
  98.     self.timer.stop()
  99.     self.time = QtCore.QTime(0,0,0)
  100.     self.timelabel.setText("0.0 secs")
  101.     self.bombcount.setText("0/40")
  102.  
  103.     for r in range(16):
  104.       for c in range(16):
  105.         self.button[r][c].revealed  = False
  106.         self.button[r][c].setText("")
  107.         self.button[r][c].setFlat(False)
  108.         self.button[r][c].setEnabled(True)
  109.         self.button[r][c].setPalette(QtGui.QPalette(QtGui.QColor(222,222,222)))
  110. #-----------------------------------------------------
  111.   def disableAll(self):
  112.     for r in range(16):
  113.       for c in range(16):
  114.         self.button[r][c].setEnabled(False)
  115.         self.button[r][c].setFlat(True)
  116. #-----------------------------------------------------
  117.   def updateTime(self):
  118.     tmp = round(self.time.elapsed() / 1000, 1)
  119.     self.timelabel.setText( str(tmp) + " secs" )
  120. #-----------------------------------------------------
  121.   def closeEvent(self, event):
  122.     setting = QtCore.QSettings()
  123.     setting.setValue("position", self.pos())
  124. #-----------------------------------------------------
  125.   def revealButtonWrapper(self):
  126.     if self.gamestarted == 0:
  127.       self.time.start()
  128.       self.timer.start(100)
  129.       self.gamestarted = 1
  130.     button = self.sender()
  131.     self.revealButton( button )
  132. #-----------------------------------------------------
  133.   def revealButton(self, button):
  134.     if button.bomb == True:
  135.       txt = "X"
  136.       color = QtGui.QColor(222,0,0)
  137.       self.timer.stop()
  138.       self.disableAll()
  139.     else:
  140.       txt = str( self.grepSurroundingBombCount(button.row, button.col) )
  141.       color = QtGui.QColor(0,0,222)
  142.     button.revealed = True
  143.     button.setText(txt)
  144.     button.setFlat(True)
  145.     button.setEnabled(False)
  146.     button.setPalette(QtGui.QPalette(color))
  147.     if txt == "0":
  148.       for r,c in self.grepSurroundingLocation(button.row, button.col):
  149.         if self.button[r][c].revealed == False:
  150.           self.revealButton( self.button[r][c] )
  151. #-----------------------------------------------------
  152.   def grepSurroundingBombCount(self, row, col):
  153.     count = 0
  154.     tmp = self.grepSurroundingLocation(row,col)
  155.     for r,c in self.grepSurroundingLocation(row,col):
  156.       count = count + 1 if self.button[r][c].bomb == True else count
  157.     return(count)
  158. #-----------------------------------------------------
  159.   def grepSurroundingLocation(self, row, col):
  160.     """
  161.    Grep all the surrounding cells, and
  162.    return back the location in a list of [row,col]
  163.    format. e.g:-
  164.    [ [row,col], [r,c], [r,c] ....]
  165.    """
  166.     ret = []
  167.     top = row-1
  168.     bot = row+1
  169.     left = col-1
  170.     right = col+1
  171.     ### Getting top 3 cells if available
  172.     if top > -1:
  173.       ret.append( [top, col] )      # Top
  174.     if bot < 16:
  175.       ret.append( [bot, col] )      # Bot
  176.     if left > -1:
  177.       ret.append( [row, left] )     # Left
  178.     if right < 16:
  179.       ret.append( [row, right] )    # Right
  180.     if top > -1 and left > -1:
  181.       ret.append( [top, left] )   # UpperLeft
  182.     if top > -1 and right < 16:
  183.       ret.append( [top, right] )  # UpperRight
  184.     if bot < 16 and left > -1:
  185.       ret.append( [bot, left] )   # LowerLeft
  186.     if bot < 16 and right < 16:
  187.       ret.append( [bot, right] )  # LowerRight
  188.     return(ret)
  189. #-----------------------------------------------------
  190.   def populateBombs(self):
  191.     bomblocation = random.sample(range(256), 40)
  192.     x = 0
  193.     for r in range(16):
  194.       for c in range(16):
  195.         self.button[r][c].bomb = True if x in bomblocation else False
  196.         x = x + 1
  197. #-----------------------------------------------------
  198. #-----------------------------------------------------
  199. #-----------------------------------------------------
  200.  
  201. ### Main script
  202. if __name__ == "__main__":
  203.     app = QtGui.QApplication(sys.argv)
  204.     myapp = MainWindow()
  205.     myapp.show()
  206.     sys.exit(app.exec_())
  207. #__________________________________________________________________________
  208. #__________________________________________________________________________
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement