嵌套循环棋盘着色不起作用 Python [英] Nested for loop chess board coloring not working Python

查看:37
本文介绍了嵌套循环棋盘着色不起作用 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 python 制作国际象棋游戏并绘制棋盘,我使用嵌套的 for 循环.我遇到的问题是,在给板子上色时,我使用的逻辑只是给第 1、5 行上色.我不知道我做的模组是否正确.谢谢

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

推荐答案

需要改变偶数行中偶数单元格的颜色和偶数奇数行中奇数单元格的颜色:

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

这篇关于嵌套循环棋盘着色不起作用 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆