我正在接近 PyGame,但我遇到了一个问题:我正在尝试绘制一个由单个矩形图形组成的网格,我希望当在所述矩形上释放鼠标左键时,这些矩形中的每一个都被填充或清空。我用一个元组矩阵表示的网格,每个元组代表每个矩形,格式如下(列,行)以符合pygame像素的坐标,然后我创建另一个元组矩阵以从鼠标位置元组(colpixel,rowpixel)转换为矩形元组,我还创建了一个字典,将矩形元组作为键与值可以是 1 或 0 配对,该值表示矩形是否被填充,这是我在释放鼠标按钮时更改的值。这是代码:
import pygame
pygame.init()
SCREEN_WIDTH = 40
SCREEN_HEIGHT = 20
DIM = 20
ROWS = int(SCREEN_HEIGHT / DIM)
COLUMNS = int(SCREEN_WIDTH / DIM)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Game of Life')
# filling rect_matrix with tuple, each one representing a rectangle of the grid (columns, rows).
# filling rect_map by pairing the tuples from rect_matrix with a value that can be 0 or 1, default 1.
rect_matrix = []
rect_map = {}
for i in range(ROWS):
temp = []
for j in range(COLUMNS):
temp.append(pygame.Rect((j * DIM, i * DIM), (DIM, DIM)))
rect_map[(j, i)] = 1
rect_matrix.append(temp)
# filling mouse_to_rectangle, sort of a decoder to convert from groups of tuples
# (representing mouse coordinates in pixels) to a single tuple (representing single rectangles)
mouse_to_rectangle = []
ii = 0
for i in range(SCREEN_WIDTH):
jj = 0
temp = []
for j in range(SCREEN_HEIGHT):
temp.append((ii, jj))
if ((j + 1) % DIM == 0):
jj += 1
if ((i + 1) % DIM == 0):
ii += 1
mouse_to_rectangle.append(temp)
is_running = True
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
if rect_map[mouse_to_rectangle[pygame.mouse.get_pos()[0]][pygame.mouse.get_pos()[1]]] == 1:
rect_map[mouse_to_rectangle[pygame.mouse.get_pos()[0]][pygame.mouse.get_pos()[1]]] = 0
else:
rect_map[mouse_to_rectangle[pygame.mouse.get_pos()[0]][pygame.mouse.get_pos()[1]]] = 1
for i, ii in zip(rect_matrix, range(len(rect_matrix))):
for j, jj in zip(i, range(len(i))):
pygame.draw.rect(screen, (100, 100, 100), j, rect_map[(jj, ii)])
pygame.display.update()
我设法填充(rect_map 中的相对值变为 1)一个矩形,当指向它时松开左键,但是当我尝试再次单击现在已填充的矩形时,它不会为空。我用 1 或 0 表示“是否填充”属性,因为当
width
中的 pygame.draw.rect
参数设置为 0 时,矩形被填充,如果使用 >0
值,则矩形的边框将一样粗作为以像素为单位的指定值。
您错过了在绘制网格之前清除显示。因此,所有绘制的矩形都永远留在那里:
screen.fill((0, 0, 0))
不管怎样,你已经把算法复杂化了。可以通过将鼠标位置的分量除以
DIM
(//
(地板除法)运算符)来计算研磨中的位置:
mousepos = pygame.mouse.get_pos()
gridpos = mousepos[0] // DIM, mousepos[1] // DIM
最后,网格状态可以轻松更改,
mouse_to_rectangle
根本不需要:
if rect_map[gridpos] == 1:
rect_map[gridpos] = 0
else:
rect_map[gridpos] = 1
看例子:
import pygame
pygame.init()
SCREEN_WIDTH = 200
SCREEN_HEIGHT = 200
DIM = 20
ROWS = int(SCREEN_HEIGHT / DIM)
COLUMNS = int(SCREEN_WIDTH / DIM)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Game of Life')
# filling rect_matrix with tuple, each one representing a rectangle of the grid (columns, rows).
# filling rect_map by pairing the tuples from rect_matrix with a value that can be 0 or 1, default 1.
rect_matrix = []
rect_map = {}
for i in range(ROWS):
temp = []
for j in range(COLUMNS):
temp.append(pygame.Rect((j * DIM, i * DIM), (DIM, DIM)))
rect_map[(j, i)] = 1
rect_matrix.append(temp)
is_running = True
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
mousepos = pygame.mouse.get_pos()
gridpos = mousepos[0] // DIM, mousepos[1] // DIM
if rect_map[gridpos] == 1:
rect_map[gridpos] = 0
else:
rect_map[gridpos] = 1
screen.fill((0, 0, 0))
for i, ii in zip(rect_matrix, range(len(rect_matrix))):
for j, jj in zip(i, range(len(i))):
pygame.draw.rect(screen, (100, 100, 100), j, rect_map[(jj, ii)])
pygame.display.update()