Pygame-mouse事件中的Python时间计数器

5 python time pygame mouseevent python-3.x

我想在Pygame中计算用户鼠标事件的时间,如果用户没有移动他的鼠标大约15秒,那么我想在屏幕上显示文本.我time为此尝试了模块,但它不起作用.

import pygame,time

pygame.init()

#codes
...
...

font = pygame.font.SysFont(None,25)
text = font.render("Move your mouse!", True, red)

FPS = 30

while True:
    #codes
    ... 
    ...
    start = time.time()
    cur = pygame.mouse.get_pos() #catching mouse event 
    end = time.time()
    diff = end-start
    if 15 < diff:
        gameDisplay.blit(text,(10,500))

    pygame.display.update()

    clock.tick(FPS)

pygame.quit()
quit()
Run Code Online (Sandbox Code Playgroud)

输出不是我想要的,如果用户不移动他的鼠标,我不知道如何计算它.

如果我想在用户的鼠标在特殊区域时写一个文本,它的工作方式就像;

if 100 < cur[0] < 200 and 100 < cur[1] < 200:
        gameDisplay.blit(text,(10,500))
Run Code Online (Sandbox Code Playgroud)

但我怎么算?我甚至找不到如何告诉Python,用户的鼠标是否在相同的坐标上.然后我可以说,如果鼠标坐标发生变化,启动计时器,如果它大于15,则打印文本.

编辑:您可以在没有Pygame模块的普通Python中假设它,假设您有一个捕获鼠标事件的函数,然后如果告诉Python如果鼠标的坐标没有改变,启动计时器,如果时间大于15秒,打印文本,然后刷新计时器.

kno*_*dge 0

您应该添加:

start = time.time()
cur = None
Run Code Online (Sandbox Code Playgroud)

在 while 循环之前。

您还应该将start = time.time()while 循环更改为:

if cur != pygame.mouse.get_pos():
    start = time.time()
Run Code Online (Sandbox Code Playgroud)

您也可以使用pygame.time(它类似于time但以毫秒为单位测量时间)