我怎样才能让我的乌龟移动到我的光标?

How can I make my turtle move to my cursor?

我正在尝试将乌龟移动到我的光标处以绘制一些东西,以便每次单击时,乌龟都会移动到那里并绘制一些东西。

我已经尝试过onscreenclick()、onclick以及两者的许多组合,我觉得我做错了什么,但我不知道是什么。

from turtle import*
import random
turtle = Turtle()
turtle.speed(0)
col  = ["red","green","blue","orange","purple","pink","yellow"]
a = random.randint(0,4)
siz = random.randint(100,300)
def draw():
    for i in range(75):
        turtle.color(col[a])
        turtle.forward(siz)
        turtle.left(175)

TurtleScreen.onclick(turtle.goto)

任何帮助都会很棒,谢谢你抽出时间(如果你帮助我!;)

重要的不是调用什么方法,而是调用什么对象:

TurtleScreen.onclick(turtle.goto)

TurtleScreen是一个class,需要在screen实例上调用。并且由于除了 turtle.goto 之外还想调用 draw ,因此您需要定义自己的函数来调用两者:

screen = Screen()
screen.onclick(my_event_handler)

这是对您的代码进行的修改,其中包含上述修复和其他调整:

from turtle import Screen, Turtle, mainloop
from random import choice, randint

COLORS = ["red", "green", "blue", "orange", "purple", "pink", "yellow"]

def draw():
    size = randint(100, 300)

    # make turtle position center of drawing
    turtle.setheading(0)
    turtle.setx(turtle.xcor() - size/2)

    turtle.pendown()

    for _ in range(75):
        turtle.color(choice(COLORS))
        turtle.forward(size)
        turtle.left(175)

    turtle.penup()

def event_handler(x, y):
    screen.onclick(None)  # disable event handler inside event handler

    turtle.goto(x, y)

    draw()

    screen.onclick(event_handler)

turtle = Turtle()
turtle.speed('fastest')
turtle.penup()

screen = Screen()
screen.onclick(event_handler)

mainloop()