简体   繁体   English

Python 乌龟根据用户点击绘制填充不规则多边形

[英]Python turtle draw filled irregular polygon based on user clicks

I would like to make a program that creates a turtle window that the user can click 4 times to create an irregular polygon.我想制作一个程序来创建一个海龟 window,用户可以单击 4 次来创建一个不规则多边形。 It will automatically go back to the starting point after the 4th click to make sure that it is properly closed.第 4 次点击后,它会自动 go 回到起点,以确保它正确关闭。 That much works great, but the problem is that I would like to have it filled in as well, which I can't get to work.这么多效果很好,但问题是我也想把它填满,我无法开始工作。

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

    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)
                turtle.done()


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

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

    turt = TrackingTurtle()
    turt.hideturtle()

    turt.fillcolor("#0000ff")
    turt.begin_fill()
    turtle.onscreenclick(alex.goto_mouse)
    
    turt.end_fill()

    wn.mainloop()

Example output示例 output

I would like the above output to be filled in blue, but as you can see it's not.我希望将上面的 output 填充为蓝色,但如您所见,事实并非如此。 Is this possible with the turtle module?龟模块可以做到这一点吗? If so what might I change to fix it?如果是这样,我可以改变什么来解决它? Thanks in advance for your time and help!提前感谢您的时间和帮助!

You're pretty close.你很接近。 The misunderstanding seems to be thinking that onscreenclick blocks until the shape is done, then end_fill() runs.误解似乎是认为onscreenclick阻塞直到形状完成,然后end_fill()运行。 Actually, onscreenclick returns immediately after registering the click handler, then .end_fill() runs before any clicks have occurred or the turtle main loop runs.实际上, onscreenclick在注册点击处理程序后立即返回,然后.end_fill()在任何点击发生或龟主循环运行之前运行。 By the time the user starts clicking, the fill has long been closed.当用户开始点击时,填充早已关闭。

The solution is to move the .end_fill() call to the if self.count == 4: block.解决方案是将.end_fill()调用移至if self.count == 4:块。

Since I'm not a fan of inheriting Turtle , here's a similar, minimal example that uses a closure but should be readily adaptable to your use case.由于我不喜欢继承Turtle ,因此这里有一个类似的最小示例,它使用了闭包,但应该很容易适应您的用例。

import turtle

def track_polygon(sides=5):
    def goto_mouse(x, y):
        nonlocal clicks

        if clicks < sides - 1:
            turtle.goto(x, y)
            clicks += 1

            if clicks >= sides - 1:
                turtle.goto(0, 0)
                turtle.end_fill()
                turtle.exitonclick()

    clicks = 0
    turtle.begin_fill()
    turtle.onscreenclick(goto_mouse)

track_polygon()
turtle.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM