Pygame中的倒数计时器
问题描述
我开始使用 pygame,我想做简单的游戏.我需要的元素之一是倒数计时器.如何在 PyGame 中进行倒计时(例如 10 秒)?
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?
解决方案
在这个页面你会找到你要找的http://www.pygame.org/docs/ref/time.html#pygame.time.get_ticks
您在开始倒计时之前下载一次刻度(这可能是游戏中的触发器 - 关键事件,无论如何).例如:
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
相关文章