【问题标题】:Python3-Turtle: Pen color not changingPython3-Turtle:笔颜色不变
【发布时间】:2021-12-30 20:16:54
【问题描述】:

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

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

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

【问题讨论】:

  • 里面有很多缺失的代码。这些函数之外的一些事情可能会影响结果(例如,对全局 tvariable 的操作,draw_row 中的代码)

标签: python turtle-graphics python-turtle


【解决方案1】:

一旦我添加了兼容的 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() 函数正在绘制一个填充砖块,那么您需要将 draw_brick_wall() 中对 pencolor(new_color) 的调用更改为 fillcolor(new_color) 以设置填充颜​​色或color(new_color) 设置笔和填充颜色。

【讨论】:

  • 这实际上让我检查了我的draw_brick 函数并找到了解决方案。感谢您的帮助!
猜你喜欢
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 2015-04-10
  • 2020-01-21
  • 2014-07-20
  • 2013-09-18
  • 1970-01-01
  • 2019-01-05
相关资源
最近更新 更多