3

I want to start my program, measure the time when the program starts and then wait some seconds, push a button (K_RIGHT) and messure the time when I push the button. I am using Pygame to registrate the Keydown. But in my code below it does not registrate my Keydown. What I am doing wrong here?

start = time.time()
for e in pygame.event.get():
    if e.type == pygame.KEYDOWN:
        if e.key == pygame.K_RIGHT:
           end= time.time()
           diff = end-start 
skrx
  • 19,980
  • 5
  • 34
  • 48
ddd
  • 167
  • 1
  • 7
  • The user shouldn't be able to push the button during the waiting time? – skrx Dec 16 '18 at 13:52
  • The user should be able to, i am just not sure how to solve it. Tried time.sleep(), but that was wrong. – ddd Dec 16 '18 at 13:53
  • instead of using time module try using pygame inbuilt time module ie. pygame.time.clock() equivalent to time.time(). try using pygame inbuild functions. – sahasrara62 Dec 16 '18 at 13:58
  • Thanks for the hint, but does it makes any difference if the function is a equivalent? – ddd Dec 16 '18 at 14:01
  • @ddd functionality level not much, coding level too much (more pythonic way) but you are using pygame as core component then it is usual practice to use that package inbuilt function as more as you can . more clean code. – sahasrara62 Dec 16 '18 at 14:05
  • Thanks man, do you have maybe an idead for my problem? – ddd Dec 16 '18 at 14:07
  • @ddd Please post a [minimal, runnable example](https://stackoverflow.com/help/mcve) (and make sure that the code is indented correctly). It looks like your program should actually compute the correct time difference. – skrx Dec 16 '18 at 14:10
  • @prashantrana I think you meant [pygame.time.get_ticks](http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks). – skrx Dec 16 '18 at 14:11
  • I've posted a working example, but as far as I can see, your code should work as well. – skrx Dec 16 '18 at 14:34
  • @skrx yeah i meant the pygame.time.get_ticks() – sahasrara62 Dec 16 '18 at 14:44

1 Answers1

2

Here's a minimal, complete example that prints the correct time difference. The passed time is just the difference between time.time() (now) and the start time.

You could use pygame.time.get_ticks instead of time.time as well (it returns the time in milliseconds instead of seconds).

import time
import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')

start = time.time()

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_RIGHT:
                diff = time.time() - start
                print(diff)

    screen.fill(BG_COLOR)
    pg.display.flip()
    clock.tick(60)

pg.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • Wow, it works!! I would like to ask you: For what you use time.clock() in this case? And while using "while not done" loop? Is itneeded in this case? – ddd Dec 16 '18 at 14:41
  • The pygame.time.Clock is used to limit the framerate, otherwise the program would run as fast as possible. `clock.tick(60)` limits it to 60 frames per second in this case. -- Yes, the while loop is necessary. – skrx Dec 16 '18 at 14:43
  • For what reason? If I understand right, it only looks if Pygame is open or not? And also pg.display.flip() is not really needed, I am right? – ddd Dec 16 '18 at 14:49
  • Without the loop the program would be done immediately and the window would be closed. -- Yes, if you don't want to draw anything you can remove `pg.display.flip()` and `screen.fill(BG_COLOR)`. – skrx Dec 16 '18 at 14:54