Countdown timer in Pygame

后端 未结 7 1040
眼角桃花
眼角桃花 2020-11-22 03:31

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?

7条回答
  •  长情又很酷
    2020-11-22 04:17

    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
    

提交回复
热议问题