Skip to main content
 首页 » 编程设计

python之事件发生时将矩阵中的元素从 0 更改为 1

2025年02月15日8飞鱼

我最近用列表编写了一个程序,但现在我需要将列表更改为矩阵。我通过绘制矩形来编写网格。现在我想在单击时更改矩形的颜色。在我的列表程序中,一切正常,但现在我必须使用矩阵,因为我的程序的其余部分需要一个矩阵。我已经有了一个全为零的矩阵,但现在我想在单击矩形时将 0 更改为 1。

x = 5 
y = 5 
 
height = 30 
width = 50 
size = 20 
color = (255,255,255) 
new_color = (0,255,0) 
 
screen.fill((0,0,0)) 
 
def draw_grid(): 
    for y in range(height): 
        for x in range(width): 
            rect = pygame.Rect(x * (size + 1),y * (size + 1),size,size) 
            pygame.draw.rect(screen,color,rect) 
            x += 20 
        y += 20 
 
rects = [[0 for i in range(width)] for j in range(height)] 
draw_grid() 
 
 
while 1: 
    clock.tick(30) 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
            sys.exit() 
 
 
    if menu == 'start':    
 
        if pygame.mouse.get_pressed()[0]: 
            mouse_pos = pygame.mouse.get_pos() 
            for i,(rect,color) in enumerate(rects): 
                if rect.collidepoint(mouse_pos): 
                    rects[i] = (rect,new_color) 
 
        for rect,color in rects: 
            pygame.draw.rect(screen,color,rect) 
 
        pygame.display.flip() 

这是我对列表使用的代码,但我已经用矩阵替换了列表。当我运行这段代码时,它给出了一个错误:

ValueError: too many values to unpack 

解决这个问题的最佳方法是什么?

请您参考如下方法:

要绘制矩形,您可以遍历矩阵并根据值(0 或 1)绘制白色矩形或绿色矩形。 (您也可以将颜色直接存储在矩阵中,但我不知道您是否想用它做其他事情。)

要更改单击的单元格的颜色,您可以通过将鼠标坐标除以 (size+1) 来轻松计算单元格的索引,例如x = mouse_x//(size+1)。然后只需设置 matrix[y][x] = 1。

import sys 
import pygame 
 
 
WHITE = pygame.Color('white') 
GREEN = pygame.Color('green') 
 
 
def draw_grid(screen, matrix, size): 
    """Draw rectangles onto the screen to create a grid.""" 
    # Iterate over the matrix. First rows then columns. 
    for y, row in enumerate(matrix): 
        for x, color in enumerate(row): 
            rect = pygame.Rect(x*(size+1), y*(size+1), size, size) 
            # If the color is white ... 
            if color == 0: 
                pygame.draw.rect(screen, WHITE, rect) 
            # If the color is green ... 
            elif color == 1: 
                pygame.draw.rect(screen, GREEN, rect) 
 
 
def main(): 
    screen = pygame.display.set_mode((800, 600)) 
    clock = pygame.time.Clock() 
 
    height = 30 
    width = 50 
    size = 20  # Cell size. 
    matrix = [[0 for i in range(width)] for j in range(height)] 
 
    done = False 
 
    while not done: 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                done = True 
 
        if pygame.mouse.get_pressed()[0]: 
            # To change the color, calculate the indexes 
            # of the clicked cell like so: 
            mouse_x, mouse_y = pygame.mouse.get_pos() 
            x = mouse_x // (size+1) 
            y = mouse_y // (size+1) 
            matrix[y][x] = 1 
 
        screen.fill((30, 30, 30)) 
        # Now draw the grid. Pass all needed values to the function. 
        draw_grid(screen, matrix, size) 
 
        pygame.display.flip() 
        clock.tick(30) 
 
 
if __name__ == '__main__': 
    pygame.init() 
    main() 
    pygame.quit() 
    sys.exit()