簡體   English   中英

在pygame和倒計時中顯示經過的時間。 Tabata計時器與pygame

[英]Display elapsed time in pygame and countdown. Tabata timer with pygame

我正在嘗試使用pygame創建Tabata或HIIT計時器。 本質上,我想以秒為單位上下計數,並顯示經過或剩余的時間。 我修改了在這里找到的代碼http://www.pygame.org/project-Countdown-1237-.html

使用上面鏈接的標准代碼,我無法准確顯示時間。 這根本不准確。 屏幕上每秒更新一次可能需要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()

您的延遲來自2個地方。

Clock.tick(60)的調用睡眠了Clock.tick(60)一秒的時間,以確保每秒被調用60次。 在這種情況下,您應該刪除它,因為您並不是在每一幀都調用此函數。

較大的延遲來自以下事實:每個函數調用都需要一些時間,而您沒有考慮到這一點。

您還在計時器結尾處睡眠1秒鍾。 這是不准確的。

我建議您跟蹤一個計時器花費了多少毫秒,並將其從下一個計時器中減去。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM