如何将网格坐标转换回(x,y)?

时间:2022-02-08 05:44:41

I am creating a game in PyQt5 where the user can click inside a grid to form squares. If the user chooses four grid spaces that form a square, the corners are connected with lines. My code currently looks like this:

我在PyQt5中创建一个游戏,用户可以在网格内单击以形成正方形。如果用户选择形成正方形的四个网格空间,则角线与线连接。我的代码目前看起来像这样:

import sys
from PyQt5.QtGui import QPainter, QColo, QPen, QBrush
from PyQt5.QtCore import Qt, QRect, QPoint
from PyQt5.QtWidgets import QWidget, QApplication

CELL_COUNT = 8
CELL_SIZE = 50
GRID_ORIGINX = 150
GRID_ORIGINY = 150
W_WIDTH = 700
W_HEIGHT = 700

class TribeSquares(QWidget):

def __init__(self):
    super().__init__()
    self.setGeometry(300, 300, W_WIDTH, W_HEIGHT)
    a = ''
    self.__board = [[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a],[a,a,a,a,a,a,a,a]]
    self.__row = None
    self.__col = None
    self.__clicked0 = []
    self.show()

First, I converted (x,y) coordinates to grid coordinates.

首先,我将(x,y)坐标转换为网格坐标。

def mousePressEvent(self, event):
    self.__x = event.x()
    self.__y = event.y()
    self.__row = (event.y() - GRID_ORIGINY) // CELL_SIZE
    self.__col = (event.x() - GRID_ORIGINX) // CELL_SIZE
    row = self.__row
    col = self.__col
    self.update()

For another method, I want to convert (row,col) back to (x,y). I tried reversing the process and doing it like this:

对于另一种方法,我想将(row,col)转换回(x,y)。我尝试颠倒过程并像这样做:

def cell2coord(self, row, col):
    return ((row * CELL_SIZE) + GRID_ORIGINX, (col * CELL_SIZE) + GRID_ORIGINY)

Next, in the paintEvent, I try to call the method I created.

接下来,在paintEvent中,我尝试调用我创建的方法。

def paintEvent(self, event):
    qp = QPainter()
    qp.begin(self)
    for r in range(CELL_COUNT):
        for c in range(CELL_COUNT):
            qp.setPen(QPen(Qt.black, 1))
            qp.drawRect(CELL_SIZE * c + GRID_ORIGINX, CELL_SIZE * r + GRID_ORIGINY, CELL_SIZE, CELL_SIZE)
            if (r, c) in self.__clicked0:
                qp.setPen(QPen(QColor(40, 85, 66), 1))
                qp.setBrush(QColor(40, 85, 66))
                qp.drawRect(CELL_SIZE * c + GRID_ORIGINX + 7.5, CELL_SIZE * r + GRID_ORIGINY + 7.5, 35, 35)
                qp.setBrush(QColor(237, 237, 237))
            elif (r, c) in self.__clicked1:
                qp.setPen(QPen(QColor(231, 181, 71), 1))
                qp.setBrush(QColor(231, 181, 71))
                qp.drawRect(CELL_SIZE * c + GRID_ORIGINX + 7.5, CELL_SIZE * r + GRID_ORIGINY + 7.5, 35, 35)
                qp.setBrush(QColor(237, 237, 237))
    for (r, c) in self.__clicked0:
        x,y = self.cell2coord(r,c)
        x2,y2 = self.cell2coord(self.__row, self.__col)
        if r == self.__row or c == self.__col:
            qp.setPen(QPen(QColor(40, 85, 66), 5))
            qp.drawLine(x, y, x2, y2)
    for (r, c) in self.__clicked1:
        if r == self.__row or c == self.__col:
            qp.setPen(QPen(QColor(231, 181, 71), 5))
            qp.drawLine(x, y, x2, y2)
    qp.end()

if __name__ == '__main__':
  app = QApplication(sys.argv)
  ex = TribeSquares()
  sys.exit(app.exec_())

But it didn't work. The lines are not showing up in the right spot and connecting the squares created by user clicks in the grid. What can I do?

但它没有用。这些线条没有显示在正确的位置,并且连接了用户在网格中单击所创建的方块。我能做什么?

1 个解决方案

#1


0  

Just as you have built the rectangles to paint in the paintEvent method, you could do it to get the QRect, and then use its center() method, and then get its coordinates.

就像你在paintEvent方法中构建要绘制的矩形一样,你可以用它来获取QRect,然后使用它的center()方法,然后获得它的坐标。

def cell2coord(self, row, col):
    center_pos = QRect(CELL_SIZE * c + GRID_ORIGINX, CELL_SIZE * r + GRID_ORIGINY, CELL_SIZE, CELL_SIZE).center()
    return center_pos.x(), center_pos.y()

#1


0  

Just as you have built the rectangles to paint in the paintEvent method, you could do it to get the QRect, and then use its center() method, and then get its coordinates.

就像你在paintEvent方法中构建要绘制的矩形一样,你可以用它来获取QRect,然后使用它的center()方法,然后获得它的坐标。

def cell2coord(self, row, col):
    center_pos = QRect(CELL_SIZE * c + GRID_ORIGINX, CELL_SIZE * r + GRID_ORIGINY, CELL_SIZE, CELL_SIZE).center()
    return center_pos.x(), center_pos.y()