Python time counter in Pygame-mouse events

匆匆过客 提交于 2019-12-05 11:45:26

To display a text on the screen if there is no mouse movement within the pygame window for 3 seconds:

#!/usr/bin/python
import sys
import pygame

WHITE, RED = (255,255,255), (255,0,0)
pygame.init()
screen = pygame.display.set_mode((300,200))
pygame.display.set_caption('Warn on no movement')

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

clock = pygame.time.Clock()
timer = pygame.time.get_ticks
timeout = 3000 # milliseconds
deadline = timer() + timeout
while True:
    now = timer()
    if pygame.mouse.get_rel() != (0, 0): # mouse moved within the pygame screen
        deadline = now + timeout # reset the deadline

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(WHITE)
    if now > deadline: # no movement for too long
        screen.blit(text, (10, 50))

    pygame.display.flip()
    clock.tick(60) # set fps

You should add:

start = time.time()
cur = None

before while loop.

You should also change start = time.time() in while loop to:

if cur != pygame.mouse.get_pos():
    start = time.time()

Also you could use pygame.time (it's similar to time but measure time in milliseconds)

In your code, the while True: code block is continuously running. The cur = pygame.mouse.get_pos() function is non blocking. This means it does not wait for mouse input - it will return straight away. So you need to initialize the start and cur variables before your while True: code block and then check the mouse position constantly in your loop.

If cur has changed since the last time the loop ran, then reset the start variable to the current time, and if the difference between the current time and start becomes larger than your 15 seconds, you can display the text.

You can also do that even without getting time, since you can calculate the pause as an integer counter through your FPS. Consider following example. Note that if the cursor is out of the window, the values of its positon will not change even if you move the cursor.

import pygame

pygame.init()
clock = pygame.time.Clock( ) 
DISP = pygame.display.set_mode((600, 400))

FPS = 25
Timeout = 15
Ticks = FPS*Timeout     # your pause but as an integer value
count = 0               # counter
MC  = pygame.mouse.get_pos()
MC_old = MC

MainLoop = True
while MainLoop :
    clock.tick(FPS)
    pygame.event.pump()
    Keys = pygame.key.get_pressed()
    if Keys[pygame.K_ESCAPE]:
        MainLoop = False

    MC  = pygame.mouse.get_pos()        # get mouse position
    if (MC[0]-MC_old[0] == 0) and (MC[1]-MC_old[1] == 0) :
        count = count + 1
    else : count = 0
    if count > Ticks :
        print "What are you waiting for"
        count = 0
    MC_old = MC             # save mouse position

    pygame.display.flip( )

pygame.quit( )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!