Measure The Time With Time.time() And Keydown
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
Solution 1:
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()
Post a Comment for "Measure The Time With Time.time() And Keydown"