Problem swapping colors in an array backed grid in pygame

Viewed 145

so I'm basically trying to make a simple base game for a project. It's an array-backed grid in pygame which starts with a black background and a 10x10 array of green grid squares. what i'm trying to do is swap colors when a square is clicked, if the square was green, it turns to white and if the square was white, it turns to green. I am successfully able to change the color of a square once but I can't seem to swap it back. What am I doing wrong? Here's my code

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)

WIDTH = 20
HEIGHT = 20
MARGIN = 5

grid = []
for row in range(10):
    grid.append([])
    for column in range(10):
        grid[row].append(0)  
pygame.init()

WINDOW_SIZE = [255, 255]
screen = pygame.display.set_mode(WINDOW_SIZE)

pygame.display.set_caption("ColorSwitch")

done = False

clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True 
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            column = pos[0] // (WIDTH + MARGIN)
            row = pos[1] // (HEIGHT + MARGIN)
            if grid[row][column]==0:
                grid[row][column] = 1
            if grid[row][column]==1:
                grid[row][column]==0
    screen.fill(BLACK)
    for row in range(10):
        for column in range(10):
            if grid[row][column] == 1:
                color = WHITE
            elif grid[row][column]==0:
                color = GREEN
            pygame.draw.rect(screen,
                             color,
                             [(MARGIN + WIDTH) * column + MARGIN,
                              (MARGIN + HEIGHT) * row + MARGIN,
                              WIDTH,
                              HEIGHT])
    clock.tick(60)
    pygame.display.flip()
pygame.quit()

I really can't see where I went wrong.

1 Answers

You accidently used the (==) compare equality operator instead of the (=) assignment operator:

grid[row][column]==0

grid[row][column]=0
Related