Turtle鼠标事件仅在超时后响应,而不是单击时响应

时间:2018-03-19 22:37:19

标签: python while-loop onclick turtle-graphics

在随机方块中点击时,它会显示"您点击了!"仅当它绘制下一个方格时,而不是在您最初点击时。

import turtle
import random
import time
t1 = turtle.Turtle()
t2 = turtle.Turtle()
s = turtle.Screen()

def turtle_set_up():
    t1.hideturtle()
    t2.hideturtle()
    t2.penup()
    s.tracer(1, 0)

def draw():
    for i in range(2):
        t1.fd(400)
        t1.lt(90)
        t1.fd(400)
        t1.lt(90)

def drawbox():
    t2.pendown()
    for i in range(2):
        t2.fd(50)
        t2.lt(90)
        t2.fd(50)
        t2.lt(90)
    t2.penup()

def box1():
    X = random.randint(0, 350)
    Y = random.randint(0, 350)
    Xleft = X
    Xright = X + 50
    Ytop = Y + 50
    Ybottom = Y
    t2.goto(X, Y)
    drawbox()

这是检查您的鼠标是否在绘制的框内的位置。如果是,它会删除旧框,每隔3秒就会绘制一个新框。

我不明白为什么它等着说'#34;你点击了!"直到3秒钟完成循环,而不是在你最初点击时说出来。

我无法在打印出来的打印语句后放置一个break命令"你点击了!"因为它在循环之外。

之间:

    t_end = time.time() + 60 * 0.05
    while time.time() < t_end:
        def get_mouse_click_coor(x, y):
            X_click = x
            Y_click = y
            if Xright > X_click > Xleft and Ytop > Y_click > Ybottom:
                print('You clicked!')
        s.onclick(get_mouse_click_coor)
    t2.clear()

在这里:

def starter():
    turtle_set_up()
    draw()
    while 1:
        box1()
starter()

3 个答案:

答案 0 :(得分:0)

计算机必须按顺序运行,因此它一次只能处理一行,所以,除非我弄错了,你的程序会被定时器“捕获”,然后运行程序并返回到开始。 你可以开发一个带有嵌套if语句的while循环,该语句从设备获取datetime,如果turtles有datetime

答案 1 :(得分:0)

具有交互式GUI(图形用户界面)的应用程序是基于事件的,这意味着它们在某些事件发生时执行其操作。对于此类应用程序,如果在给定时间内创建等待循环(代码也是如此),整个应用程序将被阻塞这段时间。这就是print仅在3秒延迟后执行的原因。

所有GUI库都包含激活某些计时器事件的方案。对于turtle API,有一个on_timer(func, delay)方法在一些func之后调用函数delay(以毫秒表示)。我们的想法是每3000毫秒重复调用drawbox函数。因此,您的代码将基于两个主要的回调函数:get_mouse_click调用click事件,drawbox调用timer事件。这是修改后的代码,我建议:

import turtle
import random

t1 = turtle.Turtle()
t2 = turtle.Turtle()
s = turtle.Screen()

def turtle_set_up():
    t1.hideturtle()
    t2.hideturtle()
    s.tracer(1, 0)
    s.onclick(get_mouse_click)   # define the 'click' event callback

def draw():
    for i in range(2):
        t1.fd(400)
        t1.lt(90)
        t1.fd(400)
        t1.lt(90)

def drawbox():
    global box                   # store box coordinates as global variable
    X = random.randint(0, 350)   # so that they are available for testing
    Y = random.randint(0, 350)   # in the mouse click callback
    box = (X, Y, X+50, Y+50)
    t2.clear()                   # clear previous box before drawing new one
    t2.penup()
    t2.goto(X, Y)
    t2.pendown()
    for i in range(2):
        t2.fd(50)
        t2.lt(90)
        t2.fd(50)
        t2.lt(90)
    s.ontimer(drawbox, 3000)     # define timer event callback

def get_mouse_click(x, y):
    if box[0] <= x <= box[2] and box[1] <= y <= box[3]:
        print('You clicked!')

def starter():
    turtle_set_up()
    draw()
    drawbox()

starter()

答案 2 :(得分:0)

我相信你可以简化这个问题。主要是通过制作乌龟 内部框而不是 绘制 内部框。这简化了绘图,擦除和事件处理。避免调用tracer()方法,直到有一个工作程序,因为它只会使调试复杂化。我们还可以 盖章 ,而不是 绘制 边框。

如果我们只是希望能够点击内框并让它随机移动到新位置:

from turtle import Turtle, Screen
from random import randint

BORDER_SIZE = 400
BOX_SIZE = 50
CURSOR_SIZE = 20

def move_box():
    x = randint(BOX_SIZE/2 - BORDER_SIZE/2, BORDER_SIZE/2 - BOX_SIZE/2)
    y = randint(BOX_SIZE/2 - BORDER_SIZE/2, BORDER_SIZE/2 - BOX_SIZE/2)

    turtle.goto(x, y)

def on_mouse_click(x, y):
    print("You clicked!")
    move_box()

screen = Screen()

turtle = Turtle('square', visible=False)
turtle.shapesize(BORDER_SIZE / CURSOR_SIZE)
turtle.color('black', 'white')
turtle.stamp()

turtle.shapesize(BOX_SIZE / CURSOR_SIZE)
turtle.onclick(on_mouse_click)
turtle.penup()
turtle.showturtle()

move_box()

screen.mainloop()

如果我们想让程序更像游戏,并且要求用户在每次移动后的3秒内点击内框,或者输掉游戏,那么我们可以将ontimer()事件作为@sciroccorics引入表明:

from turtle import Turtle, Screen
from random import randint

BORDER_SIZE = 400
BOX_SIZE = 50
CURSOR_SIZE = 20
CLICK_TIMEOUT = 3000  # milliseconds

def move_box():
    x = randint(BOX_SIZE/2 - BORDER_SIZE/2, BORDER_SIZE/2 - BOX_SIZE/2)
    y = randint(BOX_SIZE/2 - BORDER_SIZE/2, BORDER_SIZE/2 - BOX_SIZE/2)

    turtle.goto(x, y)

    screen.ontimer(non_mouse_click, CLICK_TIMEOUT)

def on_mouse_click(x, y):
    global semaphore

    print("You clicked!")
    semaphore += 1
    move_box()

def non_mouse_click():
    global semaphore

    semaphore -= 1

    if semaphore < 1:
        turtle.onclick(None)
        turtle.color('black')
        print("Game Over!")

screen = Screen()

semaphore = 1

turtle = Turtle('square', visible=False)
turtle.shapesize(BORDER_SIZE / CURSOR_SIZE)
turtle.color('black', 'white')
turtle.stamp()

turtle.shapesize(BOX_SIZE / CURSOR_SIZE)
turtle.onclick(on_mouse_click)
turtle.penup()
turtle.showturtle()

move_box()

screen.mainloop()