0

I wanted to make a countdown timer that would be displayed onto a screen using pygame. I've tried using this:

while secs !=0:
    timer = ("time left: {}".format(secs))
    timer_ao = ("time left: {}".format(secs+1))
    display_text((timer_ao),white,(20),(20),medText)
    display_text((timer),black,(20),(20),medText)
    #pygame.display.update()
    time.sleep(1)
    secs = secs-1

However, it keeps interfering with other parts of my code.

Is there a better way to make a timer?

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
l4dxd
  • 1
  • 1
  • you can't use `while` and `sleep` because it blocks main loop. You have to run it in main loop with `if secs != 0` instead of `while secs != 0`, and use `time` or `pygame.time` inside `if` to check if 1 second left and change time. – furas Jan 07 '18 at 17:06
  • Possible duplicate of [Countdown timer in Pygame](https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame) – skrx Jan 08 '18 at 01:39
  • @skrx I'll check it out. Thanks for the link – l4dxd Jan 08 '18 at 07:53

1 Answers1

1

You can't use while and sleep because it blocks main loop.

You have to run it in main loop with if secs != 0 instead of while secs != 0 - mainloop will works as while, and use time or pygame.time inside if to check if 1 second left (1000ms) and change displayed time.

Something like this

secs = 10

timer = "time left: {}".format(secs)
timer_ao = "time left: {}".format(secs+1)
previouse_time = pygame.time.get_ticks()

while True: # main loop

    # ... other elements ...

    current_time = pygame.time.get_ticks()

    if secs > 0:
        if previouse_time - current_time == 1000: # 1000ms = 1s
            secs -= 1
            timer = "time left: {}".format(secs)
            timer_ao = "time left: {}".format(secs+1)
            previouse_time = current_time

    display_text(timer_ao, white, 20, 20, medText)
    display_text(timer, black, 20, 20, medText)
    pygame.display.update()

You can also use pygame.time.set_timer to create own event every 1000ms and use it to change displayed text.

SECOND = pygame.USEREVENT

secs = 10

timer = "time left: {}".format(secs)
timer_ao = "time left: {}".format(secs+1)

# start timer
pygame.time.set_timer(SECOND, 1000)

while True: # main loop

    for event in pygame.event.get():

        if event.type == SECOND:
            if secs > 0:
                secs -= 1
                timer = "time left: {}".format(secs)
                timer_ao = "time left: {}".format(secs+1)
                previouse_time = current_time
            else:
                # stop timer
                pygame.time.set_timer(SECOND, 0)

    display_text(timer_ao, white, 20, 20, medText)
    display_text(timer, black, 20, 20, medText)
    pygame.display.update()
furas
  • 134,197
  • 12
  • 106
  • 148
  • My code seems to get stuck at ` current_time = pygame.time.get_ticks()` as in it remains the same throughout, time doesn't seem to carry on – l4dxd Jan 07 '18 at 22:18
  • Don't worry! I'm an idiot :( it's all working now :P – l4dxd Jan 08 '18 at 21:54