如何在棋盘的特定区域中着色?

时间:2018-01-06 17:01:38

标签: python python-3.x pygame

我制作了一个国际象棋棋盘,但我需要帮助为不同的方块着色。我只有白色方格,但我需要 黑色 方块。这是我的代码。

import pygame
def board():   
    width=480         # measurements for the window
    height=480
    block_size=59
    window = pygame.display.set_mode((width,height))
    background_color = (0,0,0)     # This is how I make the lines
    window.fill(background_color)
    for y in range(height):
        for x in range(width):
            rect = pygame.Rect(x*(block_size+1), y*(block_size+1), block_size, block_size)
            pygame.draw.rect(window, (255,255,255), rect)   # Leaves space for lines to be visible.
    pygame.display.flip()
board()

# It's just the board.

我已经知道我违反了PEP 8.

3 个答案:

答案 0 :(得分:3)

你可以这样做:我改变你的coord操作直接受益于给定的范围值而不是在其中使用mult。每次绘制rect时都会翻转颜色,也会在换行时翻转颜色以获得交替的行颜色:

import pygame
def board(): 
    def flipColor(color):
        white = (240,240,240)
        black = (30,30,30)
        if not color or color == white:
            color = black
        else: 
            color = white
        return color

    width=480         # measurements for the window
    height=480
    block_size= 60  
    window = pygame.display.set_mode((width,height))
    background_color = (0,0,0)     # This is how I make the lines
    window.fill(background_color)
    c = None
    pygame.draw.rect(window,(255,0,0),pygame.Rect(0,0,width,height)) # red background
    for y in range(0,height,block_size): 
        c = flipColor(c)
        for x in range(0,width,block_size):
            c = flipColor(c)
            rect = pygame.Rect(x , y , x+block_size , y+block_size ) 
            pygame.draw.rect(window, c , rect, 0)   # Leaves space for lines to be visible.

    for i in range(0,height+1,block_size):  
        pygame.draw.line(window,(233,33,187),(i,0),(i,width),2)
        pygame.draw.line(window,(233,33,187),(0,i),(height,i),2)

    pygame.draw.line(window,(233,33,187),(height-2,0),(height-2,width),2) # fix for out of window line
    pygame.draw.line(window,(233,33,187),(0,width-2),(height,width-2),2) # fix for out of wondow line

    pygame.display.flip()


board()

chessboard chessboard with lines

答案 1 :(得分:2)

你也可以使用itertools.cycle,传递一个带有颜色的iterable,然后只需调用next来循环它们。我在程序启动时创建背景曲面,然后在while循环中将其blit。如果您需要带有额外线条的背景,只需在同一背景表面上绘制它们或创建副本。

import itertools
import pygame as pg


pg.init()
screen = pg.display.set_mode((480, 480))
clock = pg.time.Clock()

width, height = screen.get_size()
block_size = 60
# Create a surface onto which we'll blit the rectangles.
background = pg.Surface((width, height))
colors = itertools.cycle((pg.Color('white'), pg.Color('black')))

for y in range(0, height, block_size):
    for x in range(0, width, block_size):
        rect = (x, y, block_size, block_size)
        pg.draw.rect(background, next(colors), rect)
    next(colors)  # Skip the next color.


# Then you can just blit the background in the while loop.
screen.blit(background, (0, 0))
pg.display.flip()

答案 2 :(得分:0)

您还可以计算颜色。

如果列和行的索引都相等或不相等,则颜色为白色,否则为黑色。

例如字段 A8:

<块引用>

A 的索引为 0 --> 相等
8 --> 等于
==> 字段是白色的

示例字段 A1:

<块引用>

A 的索引为 0 --> 相等
1 --> 不平等
==> 字段是黑色的