1

I got sounds that I play from a list called sounds. It plays a sound, store the time when the sound is played in start, waits 6 seconds and plays the next sound from the list. Now I want to capture a reaction time between these 6 seconds with a keydown. If the condition is true then I click the button and it captures the time and store it in end. Then, the difference between end and start should give me the result. The problem is, that it does not measure the time right. It always gives me millisconds, even if I way longer bfore I click. I wonder what I am doing wrong here?

start = time.time()

    for i in range(len(arr)):
        pygame.mixer.music.load(sounds[i])
        pygame.mixer.music.play()

            for e in pygame.event.get():
                if e.type == pygame.KEYDOWN:
                    if e.key == pygame.K_RIGHT:

                        if condition:
                            end = time.time()
                            diff = end - start

            while pygame.mixer.music.get_busy():
                  time.sleep(6)
ddd
  • 167
  • 1
  • 7
  • nobody can help? – ddd Dec 16 '18 at 12:37
  • Should the user press the button as soon as the sound plays? – skrx Dec 16 '18 at 14:20
  • yep, while these 6 seconds. But i think that time.sleep(6) is the problem, because it seems this will pause the whole program and you cannot do anything while this time – ddd Dec 16 '18 at 14:23
  • Is this question a duplicate of your new question [here](https://stackoverflow.com/questions/53802710/how-to-meassure-time-in-python)? Then I think it would be better to delete one of them. – skrx Dec 16 '18 at 14:24
  • Yes, `time.sleep` will pause the whole program, so the user can't press buttons/keys anymore during that time. – skrx Dec 16 '18 at 14:26
  • Yeah, it is similiar, but different problem. – ddd Dec 16 '18 at 14:27
  • Is there a solution to interact while the sleeping time? – ddd Dec 16 '18 at 14:27

1 Answers1

0

I think the simplest solution would be to reset the start time when the next sound starts playing.

import time
import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
SOUND = pg.mixer.Sound('a_sound.wav')

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)

    passed_time = time.time() - start
    pg.display.set_caption(str(passed_time))
    if passed_time > 6:
        start = time.time()  # Reset the start time.
        SOUND.play()

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

pg.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • hey, `SOUND = pg.mixer.Sound('a_sound.wav')` is not working. I think you have to load the sound first ? – ddd Dec 16 '18 at 15:09
  • 1
    It loads the sound file and creates a sound object which you can play by calling its `play` method. Is your filename/path correct? Does it print an error message? – skrx Dec 16 '18 at 15:13
  • I just put the sound file into the same folder like the program is. And it gives me an error. Usually i use `pygame.mixer.music.load(sounds[i]), pygame.mixer.music.play()`. Which works to play sounds, but in this case it is not the right solution? – ddd Dec 16 '18 at 15:27
  • 1
    Please post the error message/traceback. I guess the filename or path is not correct. And print `os.listdir()` to check if the file is there. – skrx Dec 16 '18 at 16:14
  • `Traceback (most recent call last): File "C:/Users/User/PycharmProjects/untitled/test2.py", line 9, in SOUND = pg.mixer.Sound('a.mp3') pygame.error: Unable to open file 'a.mp3'` – ddd Dec 16 '18 at 16:35
  • 1
    Pygame has sometimes problems with mp3 files. Check out if a different format (for example .ogg or .wav) works. – skrx Dec 16 '18 at 16:38
  • Yep, i just converted the mp3 into a wav and it worked!! – ddd Dec 16 '18 at 16:46
  • Hey skrx, your help has been incredible. I wish to ask you one last question. In my code from the beginning I loaded the sounds form a list called `sounds`. How can i modify your solution to play `sounds[0]`, after 6 seconds `sounds[1]`, after 6 seconds `sounds[2]`ect? – ddd Dec 16 '18 at 16:50
  • 1
    You just need an index variable which has to be incremented after the 6 seconds (use modulo to create a cycle `index %= len(sounds)`). A nice alternative would be [`itertools.cycle`](https://docs.python.org/3/library/itertools.html#itertools.cycle). – skrx Dec 16 '18 at 17:44
  • Thanks. Could you help me one last time? I tried it with a for loop which includes the index but it does not work – ddd Dec 16 '18 at 18:00
  • You don't need a for loop just increment the index after `if passed_time > 6:`. – skrx Dec 16 '18 at 18:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185325/discussion-between-skrx-and-ddd). – skrx Dec 16 '18 at 18:03