I would like to fill in my Shape that I have created with the turtle module in python. I have figured out the outlines and the Turtle itself, but I don`t know how to color in the finished shape. This is my code so far:
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```
Since you are trying to fill a recursive drawing function, we probably need to do the fill commands around the initial call to that function. Working with the code you posted, it would be something like:
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()
I've made slight changes to your program, like counting down on the recursion depth instead of up, to make it simpler for me to work with.