I am working on a turtle project where the user can draw their own avatar.
The thing is, I want the user to be able to decide whether the turtle they drew will be filled, or not.
The filled part is simple, as, by default, the polies will get filled, but the only way I know how to avoid filling is to retrace the lines so that the end of the line meets the start of the line.
Is there a built-in method or a more efficient way to keep the polies empty?
The problem with my current method is that the more lines (parameter) the lines there are for each turtle, the more stucky the program runs (the area doesn't influence the smoothness of the program).
import turtle
wn = turtle.Screen()
pen = turtle.Turtle('circle')
pen.shapesize(0.1, 0.1)
cor = []
# function to draw with the pen
def drag(x, y):
wn.tracer(0)
pen.goto(x, y)
cor.append(pen.pos())
# function to break out of while loop
def fill():
global done
done = True
# function to break out of while loop and set fill to False
def nofill():
global done, fill
done = True
fill = False
wn.listen()
wn.onkeypress(fill, 'f')
wn.onkeypress(nofill, 'n')
pen.ondrag(drag)
done = False
fill = True
while not done:
wn.update()
pen.begin_poly()
if fill:
for c in cor:
pen.goto(c)
else:
for c in cor[::-1]: # first go backards, then forward to avoid fill
pen.goto(c)
for c in cor:
pen.goto(c)
pen.end_poly()
wn.register_shape("mypen", pen.get_poly())
wn.clear()
example = turtle.Turtle('mypen')
Example with fill:
Run the above code.
Draw a shape.
Press f
Example without fill:
Run the above code.
Draw a shape.
Press n