2014-02-09 80 views
0

我試圖用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() 
+0

你是如何檢查間隔計時器落後的? –

回答

2

你延遲來自2個地方。

Clock.tick(60)的呼叫幾秒鐘就可以確保它被稱爲每秒60次。在你的情況下,你應該刪除它,因爲你沒有每幀調用這個函數。

更大的延遲來自這樣一個事實,即每個函數調用都需要一些時間,並且您沒有考慮到這一點。

你也睡在一個計時器的結束1秒。這是不準確的。

我建議你跟蹤計時器花費多少ms,然後從你的下一個計時器中減去這個ms。

+0

好的。感謝這一點。看起來最好的選擇是重寫這個只使用一個startTime = time.time()變量,並使其計算。 – ScottEdge