型号:
import numpy as np
import scipy.ndimage as spndmg
from PIL import Image as PIL_image
from PIL import ImageTk
class Structure(object):
def __init__(self):
self.w = 400
self.h = 400
self.cells = np.zeros((self.w, self.h))
#self.disp_cells = PIL_image.fromarray(self.cells, '1')
# Compute cells neighbours based on convolution
def computeNeighbours(self):
kernel = np.array([[2, 2, 2], [2, 1, 2], [2, 2, 2]])
conv_cells = spndmg.filters.convolve(self.cells, kernel, mode='constant', cval=0)
return conv_cells
# Compute the next state of game
def nextState(self):
conv_cells = self.computeNeighbours()
for i in range(self.w):
for j in range(self.h):
if conv_cells[i, j] % 2 == 0:
if conv_cells[i, j] / 2 == 3:
self.cells[i, j] = 1
else:
self.cells[i, j] = 0
elif (conv_cells[i, j] - 1) / 2 == 2 or (conv_cells[i, j] - 1) / 2 == 3:
self.cells[i, j] = 1
else:
self.cells[i, j] = 0
return self.cells
# Return the current state of cells
def getState(self):
return self.disp_cells
def setCellActive(self, i, j):
self.cells[i, j] = 1
def setCellInactive(self, i, j):
self.cells[i, j] = 0
查看:
import sys
from PyQt5 import QtCore
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit, QFileDialog,QGraphicsView,QGraphicsScene,QVBoxLayout
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter, QColor, QPen, QPainterPath
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QPainter
from PyQt5.QtCore import pyqtSignal, QObject, QPoint
from PyQt5.QtWidgets import QMainWindow, QApplication
class Drawer(QWidget):
newPoint = pyqtSignal(QPoint)
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.path = QPainterPath()
self.x = 0
self.y = 0
def paintEvent(self, event):
painter = QPainter(self)
painter.drawPath(self.path)
self.x = (int(self.path.currentPosition().x()))
self.y = (int(self.path.currentPosition().y()))
#print(self.x, self.y)
def mousePressEvent(self, event):
self.path.moveTo(event.pos())
self.update()
def mouseMoveEvent(self, event):
self.path.lineTo(event.pos())
self.newPoint.emit(event.pos())
self.update()
def sizeHint(self):
return QSize(400, 400)
class View(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.setLayout(QVBoxLayout())
label = QLabel(self)
drawer = Drawer(self)
drawer.newPoint.connect(lambda p: label.setText('Coordinates: ( %d : %d )' % (p.x(), p.y())))
self.layout().addWidget(label)
self.layout().addWidget(drawer)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = View()
w.show()
sys.exit(app.exec_())