-2

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

enter image description here

Example without fill:

  • Run the above code.

  • Draw a shape.

  • Press n

enter image description here

0

1 Answer 1

1

Is there a built-in method or a more efficient way to keep the polies empty?

I'm not aware of any functionality to avoid closed polygons on turtle cursors -- your solution is clever! Below is my rework of your code to simplify this approach:

from turtle import Screen, Turtle

def drag(x, y):
    ''' function to draw with the pen '''

    turtle.goto(x, y)

done = False
filled = True

def fill():
    ''' function to break out of while loop '''

    global done

    done = True

def nofill():
    ''' function to break out of while loop and set fill to False '''

    global done, filled

    done = True
    filled = False

screen = Screen()
screen.tracer(False)
screen.onkeypress(fill, 'f')
screen.onkeypress(nofill, 'n')
screen.listen()

turtle = Turtle('circle')
turtle.shapesize(0.1)
turtle.ondrag(drag)
turtle.begin_poly()

while not done:
    screen.update()

turtle.end_poly()

screen.clear()

polygon = turtle.get_poly()

if not filled:
    polygon = (*polygon, *polygon[::-1])

screen.register_shape("mypen", polygon)

example = Turtle('mypen')

screen.tracer(True)
screen.mainloop()

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

Unfortunately, the above code doesn't reduce the extra/excessive lines problem nor the general stuckiness of it (whatever stucky means, @Nick.)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.