如何用 Turtle 填充 Python 中的形状?

How can I fill in my Shape in Python with Turtle?

我想填写我在 python 中使用 turtle 模块创建的形状。 我已经弄清楚了轮廓和海龟本身,但我不知道如何为完成的形状上色。到目前为止,这是我的代码:

    trtl.fillcolor("green")
    trtl.shape("turtle")
    
    if currentdepth == depth:
        trtl.forward(length)
    else:
        currentlength = length/3.0
        trtl.pencolor("Blue")
        koch_segment(trtl, currentlength,currentdepth + 1)
        trtl.left(60)
        trtl.pencolor("Red")
        koch_segment(trtl, currentlength, currentdepth + 1)
        trtl.right(120)
        trtl.pencolor("Green")
        koch_segment(trtl, currentlength, currentdepth + 1)
        trtl.left(60)
        trtl.pencolor("Orange")
        koch_segment(trtl, currentlength, currentdepth + 1)



    wn = turtle.Screen()
    wx = wn.window_width() * .5
    wh = wn.window_height() * .5
    base_lgth = 2.0 / math.sqrt(3.0) * wh       # is the base length dependant on the screen size
    myturtle = turtle.Turtle()
    myturtle.speed(0.5 * (depth + 9))           # value between 1 and 10 (fast)
    myturtle.penup()
    myturtle.setposition((-wx / 2, -wh / 2))    # start in the lower left quadrant middle point
    myturtle.pendown()
    myturtle.left(60)

    return myturtle, base_lgth```

您可以使用 begin_fill(),画出您的形状,然后 end_fill():

from turtle import *

color('red', 'yellow')

begin_fill()

# drawing a square
while True:
    forward(200)
    left(90)
    if abs(pos()) < 1:
        break

end_fill()

done()

在您的代码中,它看起来像这样:

trtl.begin_fill()
trtl.fillcolor("green")
trtl.shape("turtle")

if currentdepth == depth:
    trtl.forward(length)
else:
    currentlength = length/3.0
    trtl.pencolor("Blue")
    koch_segment(trtl, currentlength,currentdepth + 1)
    trtl.left(60)
    trtl.pencolor("Red")
    koch_segment(trtl, currentlength, currentdepth + 1)
    trtl.right(120)
    trtl.pencolor("Green")
    koch_segment(trtl, currentlength, currentdepth + 1)
    trtl.left(60)
    trtl.pencolor("Orange")
    koch_segment(trtl, currentlength, currentdepth + 1)



wn = turtle.Screen()
wx = wn.window_width() * .5
wh = wn.window_height() * .5
base_lgth = 2.0 / math.sqrt(3.0) * wh       # is the base length dependant on the screen size
myturtle = turtle.Turtle()
myturtle.speed(0.5 * (depth + 9))           # value between 1 and 10 (fast)
myturtle.penup()
myturtle.setposition((-wx / 2, -wh / 2))    # start in the lower left quadrant middle point
myturtle.pendown()
myturtle.left(60)
trtl.end_fill()

return myturtle, base_lgth

由于您正在尝试填充递归绘图函数,我们可能需要围绕对该函数的初始调用执行填充命令。使用您发布的代码,它会是这样的:

from turtle import Screen, Turtle

DEPTH = 4
LENGTH = 300

def koch_segment(turtle, length, currentdepth):

    if currentdepth == 1:
        turtle.forward(length)
    else:
        currentlength = length/3

        turtle.pencolor('blue')
        koch_segment(turtle, currentlength, currentdepth - 1)
        turtle.left(60)

        turtle.pencolor('red')
        koch_segment(turtle, currentlength, currentdepth - 1)
        turtle.right(120)

        turtle.pencolor('green')
        koch_segment(turtle, currentlength, currentdepth - 1)
        turtle.left(60)

        turtle.pencolor('orange')
        koch_segment(turtle, currentlength, currentdepth - 1)

screen = Screen()
wx = screen.window_width()/2
wh = screen.window_height()/2

base_length = wh * 2 / 3.0**0.5  # the base length is dependent on the screen size

turtle = Turtle()
turtle.fillcolor('grey')  # some color we haven't used as a pen color
turtle.shape('turtle')
turtle.speed('fastest')

turtle.penup()
turtle.setposition(-wx/2, -wh/2)  # start in the lower left quadrant middle point
turtle.pendown()
turtle.left(60)

turtle.begin_fill()
koch_segment(turtle, LENGTH, DEPTH)
turtle.end_fill()

screen.exitonclick()

我对您的程序进行了细微的更改,例如递归深度倒计时而不是递归深度,以简化我的工作。