Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ApeGrid

By: a guest on Jan 26th, 2011  |  syntax: Python  |  size: 1.63 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import sys
  2. from PyQt4 import QtGui, QtCore
  3. from random import randint
  4.  
  5. def dist(p1, p2):
  6.     return max(abs(p1%4-p2%4), abs(p1/4-p2/4))
  7.  
  8. class ApeGrid(QtGui.QWidget):
  9.  
  10.     def __init__(self):
  11.         QtGui.QWidget.__init__(self)
  12.  
  13.         self.fieldLayouts = []
  14.         self.fields = []
  15.         self.layout = QtGui.QVBoxLayout()
  16.  
  17.         # Create 16 buttons
  18.         for x in range(16):
  19.             self.fields.append(QtGui.QPushButton(""))
  20.  
  21.         # Connect the buttons
  22.         for x in self.fields:
  23.             self.connect(x, QtCore.SIGNAL('clicked()'), self.fieldClicked)
  24.  
  25.         # Create four QHBoxLayouts for buttons
  26.         for x in range(4):
  27.             self.fieldLayouts.append(QtGui.QHBoxLayout())
  28.  
  29.         # Fill the layouts (4x4)
  30.         i = 0
  31.         for x in self.fieldLayouts:
  32.             for y in range(4):
  33.                 x.addWidget(self.fields[i])
  34.                 i = i + 1
  35.  
  36.         # Add the layouts to the main layout
  37.         for x in self.fieldLayouts:
  38.             self.layout.addLayout(x)
  39.  
  40.         # Set the main layout
  41.         self.setLayout(self.layout)
  42.         self.moveCircle()
  43.  
  44.     def fieldClicked(self):
  45.         button = self.sender()
  46.         self.moveCircle()
  47.  
  48.     def moveCircle(self):
  49.         for x in self.fields:
  50.             x.setText("")
  51.         av = range(16)
  52.         for i in range(3):
  53.             pick = av[randint(0, len(av)-1)]
  54.             available = [x for x in av if dist(x,pick) > 1]
  55.             self.fields[pick].setText("O")
  56.  
  57. if __name__ == "__main__":
  58.     app = QtGui.QApplication(sys.argv)
  59.  
  60.     a = ApeGrid()
  61.     a.show()
  62.  
  63.     sys.exit(app.exec_())
clone this paste RAW Paste Data