简体   繁体   English

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

[英]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.

相关问题 如何在Kivy中设置网格布局的位置(x,y坐标)? - How can I set the position (x,y coordinates) of a grid layout in kivy? 如何获得 X、Y 和 Z 轴上特定坐标的坐标网格? - How can I get a coordinate grid for certain coordinates on the X,Y and Z axis? 如何将道路上某个点在其图像坐标中的 (x,y) 坐标转换为其在世界坐标中的 (x,y) 坐标? - How do I convert the (x,y) coordinates of a point on the road in its image coordinates, to its (x,y) coordinates in the world coordinates? X,Y坐标转换 - X,Y Coordinates Convert 如何更改精灵的 X 和 Y 坐标? - How can I change the X and Y coordinates of a sprite? 如何在四叉树地图系统中将地图图块的x,y和缩放坐标转换为相应的坐标? - How do I convert the x, y, and zoom coordinates of a map tile to the corresponding coordinates in a quadtree map system? 可以在Pygame中将网格数组转换为(x,y)坐标吗? - Can grid arrays be translate to (x,y) coordinates in Pygame? 如何绘制六边形网格形状的(x,y,z)坐标? - How to plot (x,y,z) coordinates in the shape of a hexagonal grid? 如何使用Python在网格中创建10个随机的x,y坐标 - How to create 10 random x, y coordinates in a grid using Python 如何将索引列表转换为 python 中的 x、y 坐标 - how to convert list of indices into x , y coordinates in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM