Python time counter in Pygame-mouse events

房东的猫 提交于 2020-01-02 04:16:05

问题


I want to calculate the time of user's mouse events in Pygame, if user doesn't move his mouse about 15 seconds, then I want to display a text to the screen. I tried time module for that, but it's not working.

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

Well output is not what I want, I don't know how to calculate it if user doesn't move his mouse.

If I want to write a text when user's mouse in a special area, it's working like;

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

But how can I calculate? I even couldn't find how to tell Python, user's mouse is on the same coordinates or not.Then I can say, if mouse coordinates changes, start the timer, and if it's bigger than 15, print the text.

Edit: You can assume it in normal Python without Pygame module, assume you have a function that catching the mouse events, then how to tell Python if coordinates of mouse doesn't change, start the timer, if the time is bigger than 15 seconds,print a text, then refresh the timer.


回答1:


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



回答2:


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)




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/27852757/python-time-counter-in-pygame-mouse-events

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