0

我试图计算一个人按下按钮一定次数的次数。

import turtle         

if random.randint(1,2) == 2:
     turtle.listen()
     turtle.onkey(number() ,'s')

else:
    pass

def number():
   global shots
   shots += 1 

枪声是早些时候宣布的。

这就是我所做的,但我需要设置某种类型的时间限制,所以用户只能按下它说 4 秒,然后如果镜头大于一个数字,它就会做一些事情。

有什么办法可以吗,谢谢

4

1 回答 1

0

您可以使用该turtle.ontimer函数来实现计时器。在timer函数中,我增加了全局时间变量并调用它在指定的 timer 之后再次turtle.ontimer(timer, t=100)自动调用。timert

import turtle


turtle.listen()

def number():
   global shots
   shots += 1
   print('Shots', shots)


def timer():
    global time
    time += .1  # Increase the global time variable.
    print(round(time, 1))
    if time < 2:  # 2 seconds.
       # Call `timer` function again after 100 ms.
       turtle.ontimer(timer, t=100)
    else:
       print('Time is up.')
       # Do something.

shots = 0
time = -0.1  # -0.1 because the `timer` adds .1 immediately.

timer()
turtle.onkey(number, 's')
turtle.mainloop()
于 2017-12-11T02:54:59.657 回答