我試圖用pygame創建一個Tabata或HIIT計時器。本質上,我想在秒鐘內上下數,並顯示時間或剩餘時間。我修改了我在這裏找到的代碼http://www.pygame.org/project-Countdown-1237-.html在pygame和倒計時中顯示已用時間。 Tabata計時器與pygame
我遇到了上面鏈接的標準代碼具有準確時間顯示的問題。這根本不準確。在屏幕上更新每秒可能會經過2秒鐘。所以倒計時將會延長兩倍。所以我修改了代碼以獲得更準確的時間計數。
首先,我不確定這是最好的方式去做這件事。最終,我會希望在增減計數器重複的地方進行輪巡。我最大的問題是大約2分鐘後關閉。所以真正的經過時間可能是2:00分鐘,但間隔計時器是在後面一秒鐘或兩秒鐘。隨着計時器繼續運行,顯示的時間比實際經過的時間延遲n秒,情況繼續惡化。任何幫助將不勝感激。我見過的人使用pygame.time,但不知道該讓我的任何接近我需要完成
感謝
#!/usr/bin/env python
import time
import pygame
from pygame.locals import *
import sys, os
if sys.platform == 'win32' or sys.platform == 'win64':
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
Screen = max(pygame.display.list_modes())
icon = pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
pygame.display.set_caption("[Program] - [Author] - [Version] - [Date]")
Surface = pygame.display.set_mode(Screen,FULLSCREEN)
black = 0,0,0
red = 255,0,0
white = 255,255,255
green = 0,75,0
orange = 175,75,0
Font = pygame.font.Font("font.ttf",1000)
pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
test = Font.render("0",True,(255,255,255))
width = test.get_width()
height = test.get_height()
totalwidth = 4.5 * width
timerCountDown = 1
timerCountUp = 1
preTimerCountdown = 1
def quit():
pygame.mouse.set_visible(True)
pygame.event.set_grab(False)
pygame.quit(); sys.exit()
def GetInput():
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE: quit()
def CountUp(startTime,timeDuration,backgroundColor):
Surface.fill(backgroundColor)
start_pos = (Screen[0]/2)-(totalwidth/2)
currentTime = time.time()
elapsedTime = currentTime - startTime
displayTime = time.strftime('%M:%S', time.gmtime(elapsedTime)) #'%H:%M:%S'
pos = [start_pos,(Screen[1]/2)-(height/2)]
timeDuration = time.strftime('%M:%S', time.gmtime(timeDuration))
Surface.blit(Font.render(displayTime,True,(white)),pos)
pygame.display.flip()
if displayTime == timeDuration:
time.sleep(0)
#quit()
global timerCountUp
timerCountUp = 0
startTime = currentTime
def CountDown(startTime,timeDuration,backgroundColor):
Surface.fill(backgroundColor)
startTime = startTime +1
start_pos = (Screen[0]/2)-(totalwidth/2)
currentTime = time.time()
elapsedTime = currentTime - startTime
displayTime = timeDuration - elapsedTime
displayTime = time.strftime('%M:%S', time.gmtime(displayTime)) #'%H:%M:%S'
pos = [start_pos,(Screen[1]/2)-(height/2)]
timeDuration = time.strftime('%M:%S', time.gmtime(timeDuration + 1))
Surface.blit(Font.render(displayTime,True,(white)),pos)
pygame.display.flip()
if displayTime == "00:00":
global timerCountDown
timerCountDown = 0
startTime = currentTime
def main():
startTime = time.time()
Clock = pygame.time.Clock()
global timerCountUp
timerCountUp = 1
global timerCountDown
timerCountDown = 1
global preTimerCountdown
preTimerCountdown = 1
while True:
GetInput()
Clock.tick(60)
while timerCountUp != 0:
CountUp(startTime,7,green)
GetInput()
global timerCountDown
timerCountDown = 1
startTime = time.time()
while timerCountDown != 0:
CountDown(startTime,3,orange)
GetInput()
global timerCountUp
timerCountUp = 1
startTime = time.time()
if __name__ == '__main__': main()
你是如何檢查間隔計時器落後的? –