I am currently writing a game that requires a timer. I have written some code that appears to work, as in it counts upward in seconds, but when I move the mouse, the timer resets back to zero and starts counting up again. Obviously, this is not what I want to happen, and I've tried figuring out why this happens instead of counting up continuously, but I could not solve it.
My code is:
passed_time = 0
timer_started = False
(^outside the while loop)
(/ inside while loop)
for event in ev:
if event.type == pygame.QUIT:
is_alive = False
if start_clicked:
timer_started = True
if timer_started:
start_time = pygame.time.get_ticks()
if won:
timer_started = False
if timer_started:
passed_time = pygame.time.get_ticks() - start_time
text = font.render(str(passed_time / 1000), True, font_color)
screen.blit(text, (20, 450))
as well as mouse events that set [start_clicked] or [won] to True when their respective colors are clicked.
From my basic knowledge of timers in PyGame, this should keep counting up once start_clicked is set to true, however it just resets every time the mouse is moved, which completely defeats the point of a timer on a mouse-movement based avoider game. What can I do to fix this and make it run continuously, until the won variable is true?
And if for whatever reason I didn't include enough to figure out the issue, the full code is here: