Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 事件发生时,将矩阵中的元素从0更改为1_Python_Matrix_Pygame - Fatal编程技术网

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

Python 事件发生时,将矩阵中的元素从0更改为1,python,matrix,pygame,Python,Matrix,Pygame,我最近编写了一个带有列表的程序,但现在我需要将列表更改为矩阵。我通过画矩形来编程网格。现在我想在单击矩形时更改其颜色。在我的列表程序中,一切都很好,但现在我必须使用一个矩阵,因为我的程序的其余部分需要一个矩阵。我已经得到了一个全为零的矩阵,但现在我想在单击矩形时将中的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))

我最近编写了一个带有列表的程序,但现在我需要将列表更改为矩阵。我通过画矩形来编程网格。现在我想在单击矩形时更改其颜色。在我的列表程序中,一切都很好,但现在我必须使用一个矩阵,因为我的程序的其余部分需要一个矩阵。我已经得到了一个全为零的矩阵,但现在我想在单击矩形时将中的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)绘制白色矩形或绿色矩形。(您也可以将颜色直接存储在矩阵中,但我不知道您是否想用它做其他事情。)

要更改单击单元格的颜色,您可以通过将鼠标坐标除以
(大小+1)
,例如
x=mouse_x//(大小+1)
,轻松计算单元格的索引。然后只需设置
矩阵[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()

rect是一个列表列表,但您将其视为一个包含两个元组的列表,对于rect,请使用
,rect中的颜色:
。那不行。我应该怎么做呢?
rects
列表中的0和1应该代表rects的颜色吗?如果您不想再在列表中存储
pygame.Rect
s,您需要更改碰撞检测和绘图代码。@AV13,这取决于您想要哪一个。
Rects
应该是什么?您可以在代码的早期用零列表填充它,但稍后尝试提取
rect、color
元组。你必须决定你想要哪一个。如果需要元组,请用元组而不是列表填充它。如果需要零的列表,请更改for循环以处理这些。我不能告诉你你想要什么,因为我不知道你想要什么。0和1应该代表颜色:0=颜色,1=新颜色。如果我按下一个矩形,0应该变成1。之后,我想画一个新的矩阵,所有有一个矩阵的矩形都应该是新的颜色。谢谢!这正是我想要的!我还有一个问题:在你首先做的draw_网格函数中:对于y,枚举(矩阵)中的行。你为什么使用row?下一行color?
是包含0和1的矩阵的子列表。在下一行中,我迭代了
(子列表)以获得代表颜色的0和1,因此我将变量命名为
颜色
(想不出更好的名称;))。