Pyqt生活游戏更新视图

时间:2018-11-27 20:19:44

标签: python python-3.x pyqt pyqt5 conways-game-of-life

我想遵循模型视图控制器模式,用pyqt实现“生命游戏”。 我用基于卷积的游戏规则实现了该模型,用于计算单元格的下一个状态,并使用getter和setter方法设置单元格的生死状态。 该视图基于pyqt5事件从鼠标输入获取绘制单元格,并获取绘制的活动单元格的索引。 但是我不知道如何将模型连接到视图以更新单元格:

型号:

 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_())

0 个答案:

没有答案