Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

colors - Nested for loop chess board coloring not working Python

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))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...