我从名为sounds
的列表中播放声音。它播放声音,在start
中存储播放声音的时间,等待6秒,然后播放列表中的下一个声音。现在,我想通过按下按键来捕获这6秒之间的反应时间。如果条件成立,那么我单击按钮,它将捕获时间并将其存储在end
中。然后,end
和start
之间的差异应该给我结果。问题是,它无法正确地衡量时间。即使单击之前我走了更长的路,它也总是给我千分之一。我想知道我在这里做错了吗?
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)
答案 0 :(得分:0)
我认为最简单的解决方案是在下一个声音开始播放时重置开始时间。
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()