Hello,
I am creating a speedcubing timer with Python and Pygame. I need some help with making the timer portion of it. This is what I have so far. My idea is to record the time.time() when space is first pressed. Then, when space is pressed again, it records the time.time() again. By finding the elapsed time, I can see how long a solve took. However, my code doesn't seem to work. Any tips? Are there any other ways to record the time?
Thanks in advance for any help!
Sincerely,
DTCuber
I am creating a speedcubing timer with Python and Pygame. I need some help with making the timer portion of it. This is what I have so far. My idea is to record the time.time() when space is first pressed. Then, when space is pressed again, it records the time.time() again. By finding the elapsed time, I can see how long a solve took. However, my code doesn't seem to work. Any tips? Are there any other ways to record the time?
Code:
def timer():
timer_font = pygame.font.SysFont("monospace", 48)
ready = timer_font.render("ready", 1, (255, 255, 0))
running = timer_font.render("running", 1, (255, 255, 0))
screen.fill((0, 0, 0))
screen.blit(ready, (175, 200))
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
start_time = time.time()
screen.fill((0, 0, 0))
screen.blit(running, (150, 200))
if key[pygame.K_SPACE]:
stop_time = time.time()
elapsed_time = stop_time - start_time
solve_time = timer_font.render(str(elapsed_time), 1, (255, 255, 0))
screen.fill((0, 0, 0))
screen.blit(solve_time, (175, 200))
Thanks in advance for any help!
Sincerely,
DTCuber