Countdown timer in Pygame

adamo94 picture adamo94 · Jun 9, 2015 · Viewed 35.9k times · Source

I started using pygame and I want to do simple game. One of the elements which I need is countdown timer. How can I do the countdown time (eg 10 seconds) in PyGame?

Answer

DanteVoronoi picture DanteVoronoi · Jun 9, 2015

On this page you will find what you are looking for http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
You download ticks once before beginning the countdown (which can be a trigger in the game - the key event, whatever). For example:

start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
    seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
    if seconds>10: # if more than 10 seconds close the game
        break
    print (seconds) #print how many seconds