在 Python 海龟中用色块和数字画板

Draw board with color blocks and numbers in Python turtle

我有一个 listlist 叫做 ListOfValues。我想根据这个列表的大小画一个板,用白色填充一些黑色的块,用黑色填充其他块。我尝试并得到以下信息:

还有我的代码:

import turtle

#sets screen characteristics
wn=turtle.Screen()
wn.bgcolor('lightblue')
wn.title('Chessboard')

#sets turtle characteristics
greg = turtle.Turtle()
greg.speed(0)

#draws and fills one square
def square(size,color):
    greg.color(color)
    greg.begin_fill()
    for i in range(4):
        greg.fd(size)
        greg.lt(90)

    greg.end_fill()
    greg.fd(size)    

#draws the whole chessboard  
def chessboard(size):
    greg.pu()
    greg.goto(-(size*4),(size*4))
    LR = len(listOfValues);
    for i in range(LR):
        for j in range(LR):
            if(SomeValue == 1 ):
                square(size,'white')
            else:
                square(size,'black')
        greg.rt(90)
        greg.lt(90)
        greg.goto(-(size*(4)),(size*(4 - i - 1)))
chessboard(50)

我遗漏了两件事:

我怎样才能做到这一点?

I need to place lines in each row and column so it looks like a board

这个很简单。不是通过 color() 设置颜色,它同时设置 penfill 颜色,而是使用 fillcolor() 只设置 fill 颜色,将 pen 颜色保留为默认黑色。您还需要在绘图过程中放下笔,但在画完每一行后,在笔回到左边缘的过程中将其抬起。

I need to put the value from my array in the middle of the block

这个问题比较棘手,但基本上你需要将 listOfValues 定义为 list of list of int,其中每个 int 为零或一个,或一些这样的。然后在嵌套循环中,在测试其值之前执行 SomeValue = listOfValues[i][j]

如果你想在块中写数字,我会分配一个单独的 marker 乌龟来写数字。并定义合适的字体。

下面是我对你的代码的返工,我在其中实现了上述内容并尝试将板正确居中:

from turtle import Screen, Turtle

FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'normal')

listOfValues = [
    [0, 1, 1, 2, 1, 1, 0],
    [1, 2, 1, 1, 0, 1, 1],
    [1, 1, 0, 1, 1, 2, 1],
    [2, 1, 1, 0, 1, 1, 1],
    [1, 2, 1, 1, 0, 1, 2],
    [1, 1, 1, 1, 1, 1, 1],
    [1, 0, 1, 2, 1, 0, 1]
]

def square(size, color):

    ''' draw and fill one square '''

    greg.fillcolor(color)
    greg.pendown()
    greg.begin_fill()

    for _ in range(4):
        greg.forward(size)
        greg.left(90)

    greg.end_fill()

    greg.penup()
    greg.forward(size)

def chessboard(size):

    ''' draw the whole chessboard '''

    side = len(listOfValues)

    greg.penup()
    greg.goto(-size * side/2, size * side/2)

    for i in range(side):
        for j in range(side):
            someValue = listOfValues[i][j]
            text, color = ('black', 'white') if someValue == 1 else ('white', 'black')
            square(size, color)

            marker.goto(greg.xcor() - size/2, greg.ycor() + size/2 - FONT_SIZE/2)
            marker.pencolor(text)
            marker.write(someValue, align='center', font=FONT)

        greg.goto(-size * side/2, size * side/2 - size * (i + 1))

# set screen characteristics
screen = Screen()
screen.title('Chessboard')
screen.bgcolor('lightblue')

# set turtle characteristics
greg = Turtle()
greg.hideturtle()

# set marker characteristics
marker = Turtle()
marker.penup()
marker.hideturtle()

screen.tracer(False)  # because I have no patience
chessboard(50)
screen.tracer(True)

screen.mainloop()