嵌套循环棋盘着色无法正常工作的Python [英] Nested for loop chess board coloring not working Python

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

问题描述

我正在尝试用python制作国际象棋游戏并画出棋盘,我正在使用嵌套的for循环.我遇到的问题是,在给电路板着色时,我所使用的逻辑只是对行1、5进行着色.我不知道我是否在正确地做mod东西.谢谢

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:

如果j%2 == 0:

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

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

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