Python 倒计时计时器以延迟方式递减?
我一直在做一个游戏;其中一个主要组件是倒计时计时器-但是这个计时器是延迟的,我无法推断原因。我希望它每秒递减一次,但是它似乎每6秒递减一次 下面是我如何设置计时器的:Python 倒计时计时器以延迟方式递减?,python,python-3.x,timer,pygame,clock,Python,Python 3.x,Timer,Pygame,Clock,我一直在做一个游戏;其中一个主要组件是倒计时计时器-但是这个计时器是延迟的,我无法推断原因。我希望它每秒递减一次,但是它似乎每6秒递减一次 下面是我如何设置计时器的: loops = 0 minute = 1 tens = 0 ones = 0 #Timer Calculation screen.blit(cloudSky, (0,0)) if go == True: loops = loops + 1 if (loops % 60)== 0:
loops = 0
minute = 1
tens = 0
ones = 0
#Timer Calculation
screen.blit(cloudSky, (0,0))
if go == True:
loops = loops + 1
if (loops % 60)== 0:
if ones == 0 and tens == 0 and minute != 0:
tens = 6
ones = 0
minute = minute - 1
if ones == 0 and tens != 0:
ones = 9
tens = tens - 1
elif ones != 0:
ones = ones - 1
elif tens == 0 and ones == 0:
tens = 5
minute = minute - 1
elif ones == 0 and tens != 0:
tens = tens - 1
if minute <= 0 and tens == 0 and ones == 0:
go = False
非常感谢您的帮助 这可能是因为您依赖于以每秒60次的速度完成计时器计算。如果每秒更新10次,则每6秒的实时更新时间将增加1秒。您可能应该使用
time
module之类的工具来更精确地计时
import time
# To remember current time
timer = time.time() # timer = 1497737106.913825
# To read time since the clock started
seconds_passed = time.time() - timer # seconds_passed = 11.117798089981079
# To get a tuple containing calculated time (minutes, seconds, etc.)
time_passed = time.gmtime(seconds_passed)
# time_passed = time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1,
# tm_hour=0, tm_min=0, tm_sec=11, tm_wday=3, tm_yday=1, tm_isdst=0)
# As you can see year returned is 1970 because 0 means start of Unix Epoch time
# but you don't use it anyway
由于它名为tuple,因此可以使用索引:
time_passed[0] # 1970
或姓名:
time_passed.tm_year # 1970
time_passed.tm_year # 1970