Nested for loop chess board coloring not working Python

Viewed 179

I'm trying to make a chess game in python and to draw the boards, I'm using nested for loops. the problem I'm encountering is that when it comes to coloring the board, the logic I'm using just colors the rows 1, 5. I don't know if I'm doing the mod stuff right. Thanks

    def draw_board(self, screen):

        for i in range(0, 8):
            i *= screen.get_width() / 8
            for j in range(0, 8):
                # j - x
                # i - y

                print(j, i)

                j *= screen.get_height() / 8

                square = pygame.Surface((screen.get_width() / 9, screen.get_height() / 9))

                if j % 2 == 0:
                    square.fill((238, 238, 210))
                else:
                    square.fill((118, 150, 86))

                screen.blit(square, (i, j))
1 Answers

You need to change the color of the even cells in the even lines and the color of the odd cells in the even odd lines:

if j % 2 == 0:

if (i+j) % 2 == 0:
    square.fill((238, 238, 210))
else:
    square.fill((118, 150, 86))
Related