1

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:

Foosic17
  • 146
  • 4
  • Under what situation does `start_clicked` become true? The timer is re-starting whenever this is set, is this how it's supposed to work? – Kingsley Dec 03 '19 at 22:23
  • @Kingsley start_clicked is true when a specific color is clicked on (0,0,255) or blue, when it's clicked once it stays true until the win color is clicked, but I don't know why it would think the variable is set every time the mouse moves, since it appears that nothing else does this – Foosic17 Dec 04 '19 at 22:39

1 Answers1

1

By looking at your full code, your event loop is wrong, because your identation is off.

Currently you have if event.type == pygame.QUIT: in the event loop, and all the other if event.type == whatever outside it because they are not indented enough.

All the ifs checking the events must be inside the event loop, while ifs unrelated to the event loop (like if start_clicked:) must be outside, typically after, the event loop.

I cannot fix all your code, but fixing the event loop will surely help.

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • Okay I somewhat fixed my timer now, it stops resetting every time the mouse moves. I'm not sure exactly what I did, but basically, I copy & pasted the code for a timer from a website, but I must've switched some stuff around at some point which made it not work, but I fixed it now. Now is there any way to reset [pygame.time.get_ticks()] back to zero? Once I progress onto the next stage and start the timer again, it resumes counting the total time the program has been running, instead of restarting the timer from zero – Foosic17 Dec 04 '19 at 23:16
  • No. [pygame.time.get_ticks()](https://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks) does exactly what you have said. It returns the number of milliseconds since `pygame.init()` was called. You can store the previous value and do the math yourself. But to track time I would recommend using [Clock](https://www.pygame.org/docs/ref/time.html#pygame.time.Clock). You still need to do some math, but will be probably easier. – Valentino Dec 05 '19 at 00:58
  • [This question](https://stackoverflow.com/q/45520104/10426037) may be useful to see how to use `Clock`. – Valentino Dec 05 '19 at 01:02