Python time counter in pygame-mouse events

I want to calculate the time of user mouse events in Pygame, if the user does not move the mouse for about 15 seconds, then I want to display the text on the screen. I tried the time module for this, but it does not work.

 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() 

The output of the output is not what I want, I don’t know how to calculate it if the user does not move the mouse.

If I want to write text when the user mouse is in a special area, it works like:

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

But how can I calculate? I could not even find how to say Python, the user mouse is in the same coordinates or not. Then I can say that if the changes in the coordinates of the mouse begin the timer, and if it is more than 15, print the text.

Edit: You can assume this in normal Python without the Pygame module, suppose you have a function that captures mouse events, and then how to tell Python if the coordinates of the mouse do not change, start the timer, if the time is more than 15 seconds, print the text then update the timer.

+5
source share
4 answers

To display text on the screen if there is no mouse movement in 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 
+4
source

You must add:

 start = time.time() cur = None 

before the while loop.

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

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

You can also use pygame.time (it is similar to time , but measures time in milliseconds)

0
source

In your code, the code block while True: constantly working. The cur = pygame.mouse.get_pos() function is not blocked. This means that he is not waiting for mouse input - he will immediately return. Therefore, you need to initialize the start and cur variables in front of your while True: code block, and then constantly check the mouse position in your loop.

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

0
source

You can also do this without even getting time, since you can calculate the pause as an integer counter through your FPS. Consider the following example. Please note: if the cursor is outside the window, the values ​​of its position 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( ) 
0
source

Source: https://habr.com/ru/post/1210711/


All Articles