
ApeGrid
By: a guest on
Jan 26th, 2011 | syntax:
Python | size: 1.63 KB | views:
32 | expires: Never
import sys
from PyQt4 import QtGui, QtCore
from random import randint
def dist(p1, p2):
return max(abs(p1%4-p2%4), abs(p1/4-p2/4))
class ApeGrid(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.fieldLayouts = []
self.fields = []
self.layout = QtGui.QVBoxLayout()
# Create 16 buttons
for x in range(16):
self.fields.append(QtGui.QPushButton(""))
# Connect the buttons
for x in self.fields:
self.connect(x, QtCore.SIGNAL('clicked()'), self.fieldClicked)
# Create four QHBoxLayouts for buttons
for x in range(4):
self.fieldLayouts.append(QtGui.QHBoxLayout())
# Fill the layouts (4x4)
i = 0
for x in self.fieldLayouts:
for y in range(4):
x.addWidget(self.fields[i])
i = i + 1
# Add the layouts to the main layout
for x in self.fieldLayouts:
self.layout.addLayout(x)
# Set the main layout
self.setLayout(self.layout)
self.moveCircle()
def fieldClicked(self):
button = self.sender()
self.moveCircle()
def moveCircle(self):
for x in self.fields:
x.setText("")
av = range(16)
for i in range(3):
pick = av[randint(0, len(av)-1)]
available = [x for x in av if dist(x,pick) > 1]
self.fields[pick].setText("O")
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
a = ApeGrid()
a.show()
sys.exit(app.exec_())