在Python龟中转到鼠标

时间:2017-02-27 18:59:17

标签: python python-3.x turtle-graphics

我一直试图让我的乌龟永远地按下我的鼠标(并在后面画出来),但每次只是崩溃。我知道我的编程是垃圾,但请帮忙。这是我的编程:

    import turtle as t
    from turtle import *

    if input("Would you like to play the game (1), or draw shapes (2)? ") == "2":

        def k101():
            mousepen = 1
            screen = Screen()
            global screen
            while mousepen == 1:
                while screen.onscreenclick() == True:
                    screen.onscreenclick(t.goto)

        t.onkey(k101, " ") #mouse drawing

1 个答案:

答案 0 :(得分:0)

以下内容应该大致按照您的要求进行。直到你点击空格键才会做任何事情,然后当你点击屏幕时,乌龟将朝向并转到你点击的地方:

from turtle import Screen, Turtle

screen = Screen()
yertle = Turtle()

def k101():
    screen.onscreenclick(click_handler)

def click_handler(x, y):
    screen.onscreenclick(None)  # disable event inside event handler
    yertle.setheading(yertle.towards(x, y))
    yertle.goto(x, y)
    screen.onscreenclick(click_handler)  # reenable event on event handler exit

screen.onkey(k101, " ")  # space turns on mouse drawing

screen.listen()

screen.mainloop()