【发布时间】:2021-01-04 15:43:01
【问题描述】:
这是我在 pygame 中创建 22x16 网格的代码:
import pygame
pygame.init()
screen = pygame.display.set_mode((440, 320))
class Grid:
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
pygame.draw.rect(screen, GREY, [self.x * WIDTH, self.y * HEIGHT, WIDTH, HEIGHT], 1)
pygame.display.update()
cols = 22
rows = 16
WIDTH = 20
HEIGHT = 20
BLACK = (0, 0, 0)
GREY = (127,127,127)
grid = [0 for i in range(cols)]
for node in range(cols):
grid[node] = [0 for node in range(rows)]
for x in range(cols):
for y in range(rows):
grid[x][y] = Grid(x, y)
for x in range(cols):
for y in range(rows):
grid[x][y].draw()
while True:
event = pygame.event.get()
if event == pygame.QUIT:
pygame.quit()
我如何找到这个网格中每个正方形的顶点并绘制它们? 画出verices并不重要,但很高兴看到它们以便以后更好地理解。
【问题讨论】:
-
我不明白你的问题。你想知道角点(顶点)的坐标吗? -
column * WIDTH, row * HEIGHT -
好吧,既然你有一个网格,你可以生成两个 for 循环。一个用于行(x 轴)上的行,一个用于列(y 轴)。其中间隙大小分别为
height和width。
标签: python-3.x pygame