AV13 - 2 years ago 94
Python Question

# Change element in matrix from 0 to 1 when event takes place

I've recently written a program with a list, but now I need to change the list into a matrix. I programmed a grid by drawing rectangles. Now I want to change the color of the rectangle when I click on it. In my program with the list everything worked just fine, but now I have to use a matrix, because I need a matrix for the rest of my program. I've already got a matrix with all zeros, but now I want to change the 0 in to a 1 when I click on a rectangle.

``````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 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()
``````

This is the code I used with the list, but I've already replaced the list with a matrix. When I run this code it gives an error:

``````ValueError: too many values to unpack
``````

What is the best way to solve this problem?

To draw the rects you can iterate over the matrix and depending on the value (0 or 1) draw a white rect or a green rect. (You could also store the colors directly in the matrix, but I don't know if you want to do something else with it.)

To change the color of the clicked cells, you can easily calculate the indexes of the cells by floor dividing the mouse coords by `(size+1)`, e.g. `x = mouse_x // (size+1)`. Then just set `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()
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download