Python3-乌龟:笔颜色不变

Python3-Turtle: Pen color not changing

我有一个画砖墙的功能,可以选择在画墙之前设置某种笔的颜色,但是无论我做什么,砖似乎都画成绿色:

def draw_brick(length, width):

    t.color("green")
    t.begin_fill()
    for i in range(2):
        t.forward(length)
        t.right(90)
        t.forward(width)
        t.right(90)
    t.end_fill()

def draw_row(row_type, bricks):
    if row_type == "A":
        for i in range(bricks):
            draw_brick(50, 30)
            t.penup()
            t.forward(70)
            t.pendown()
    elif row_type == "B":
        draw_brick(12, 30)
        t.penup()
        t.forward(35)
        t.pendown()

        for i in range(bricks - 1):
            draw_brick(50, 30)
            t.penup()
            t.forward(70)
            t.pendown()
            
        t.penup()
        t.pendown()
        draw_brick(12, 30)


def draw_brick_wall(rows, brick, top_row, new_color):
    t.pencolor(new_color)

    if top_row == "A":
        drawing_A = True
    else:
        drawing_A = False
    for i in range(rows):
        next_position = (t.xcor(), t.ycor() - 40)

        if drawing_A:
            draw_row("A", brick)
        else:
            draw_row("B", brick)

        drawing_A = not (drawing_A)
        move_no_trails(next_position[0], next_position[1])


# resets turtle to postion (x, y)
def move_no_trails(x, y):
    t.penup()
    t.goto(x, y)
    t.pendown()

但是,由于某种原因,乌龟的笔颜色没有改变。我可以做些什么来解决这个问题?

一旦我添加了兼容的 draw_brick() 函数,您的代码对我来说就可以正常工作了:

from turtle import Screen, Turtle

def draw_brick(width, height):
    for _ in range(2):
        turtle.forward(width)
        turtle.left(90)
        turtle.forward(height)
        turtle.left(90)

def draw_row(row_type, bricks):
    if row_type == "A":
        for _ in range(bricks):
            draw_brick(50, 30)
            turtle.penup()
            turtle.forward(70)
            turtle.pendown()
    elif row_type == "B":
        draw_brick(12, 30)
        turtle.penup()
        turtle.forward(35)
        turtle.pendown()

        for _ in range(bricks-1):
            draw_brick(50, 30)
            turtle.penup()
            turtle.forward(70)
            turtle.pendown()

        draw_brick(12, 30)

# resets turtle to postion (x, y)
def move_no_trails(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def draw_brick_wall(rows, brick, top_row, new_color):
    turtle.pencolor(new_color)

    drawing_A = top_row == "A"

    for _ in range(rows):
        next_x, next_y = turtle.xcor(), turtle.ycor() - 40

        if drawing_A:
            draw_row("A", brick)
        else:
            draw_row("B", brick)

        drawing_A = not drawing_A
        move_no_trails(next_x, next_y)

screen = Screen()

turtle = Turtle()
turtle.speed('fastest')  # because I have no patience

draw_brick_wall(1, 5, "A", "red")
draw_brick_wall(1, 5, "B", "blue")
draw_brick_wall(1, 5, "A", "orange")
draw_brick_wall(1, 5, "B", "green")

screen.exitonclick()

但是,如果您的 draw_brick() 函数正在绘制一块 填充的 砖块,那么您需要将 pencolor(new_color) 中的调用更改为 draw_brick_wall()fillcolor(new_color) 设置填充颜色或 color(new_color) 设置笔和填充颜色。