[英]How can I convert grid coordinates back to (x,y)?
I am creating a game in PyQt5 where the user can click inside a grid to form squares. 我正在PyQt5中创建一个游戏,用户可以在其中单击网格以形成正方形。 If the user chooses four grid spaces that form a square, the corners are connected with lines. 如果用户选择形成正方形的四个网格空间,则角线将连接在一起。 My code currently looks like this: 我的代码当前如下所示:
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). 对于另一种方法,我想将(row,col)转换回(x,y)。 I tried reversing the process and doing it like this: 我尝试逆转该过程,并按照以下方式进行操作:
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? 我能做什么?
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.