79556703

Date: 2025-04-05 08:16:02
Score: 0.5
Natty:
Report link

I have mooved begin_fill() and end_fill() similariy to @ggorlen's but have cept the global aspect of the code similar to yours.

import turtle


class TrackingTurtle(turtle.Turtle):
    """ A custom turtle class with the ability to go to a mouse
    click location and keep track of the number of clicks """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.count = 0
        self.begin_fill()

    def goto_mouse(self, x, y):
        """ Go to the given (x, y) coordinates, or go back
        to the starting place after the 4th click """
        if self.count <= 4:
            self.goto(x, y)
            self.count += 1
            if self.count == 4:
                self.goto(0, 0)
                self.end_fill()
                turtle.done()


if __name__ == "__main__":
    turtle.setup(1080, 720)

    wn = turtle.Screen()
    wn.title("Polygon Fun")

    turt = TrackingTurtle()
    turt.hideturtle()

    turt.fillcolor("#0000ff")
    turtle.onscreenclick(turt.goto_mouse)
    
    wn.mainloop()

I hopr this solves your ichues. Now bebin_fill() is in __init__() and end_fill() hapens when we finish drawing th polygone.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @ggorlen's
  • Low reputation (1):
Posted by: rnd_name