Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame和倒计时中显示经过的时间。带pygame的Tabata定时器_Python_Timer_Pygame_Clock - Fatal编程技术网

Python 在pygame和倒计时中显示经过的时间。带pygame的Tabata定时器

Python 在pygame和倒计时中显示经过的时间。带pygame的Tabata定时器,python,timer,pygame,clock,Python,Timer,Pygame,Clock,我正在尝试使用pygame创建Tabata或HIIT计时器。基本上,我希望以秒为单位进行上下计数,并显示经过或剩余的时间。我修改了我在这里找到的代码 我在用上面链接的标准代码显示准确的时间时遇到了问题。一点也不准确。屏幕上每更新一秒可能需要2秒。所以倒计时的时间是原来的两倍。所以我修改了代码以获得更准确的时间计数 首先,我不确定这是最好的解决办法。最终,我会希望在上下计数器重复的地方进行轮次。我最大的问题是大约两分钟后时间就结束了。因此,实际运行时间可能是2:00分钟,但间隔计时器落后一秒或两秒

我正在尝试使用pygame创建Tabata或HIIT计时器。基本上,我希望以秒为单位进行上下计数,并显示经过或剩余的时间。我修改了我在这里找到的代码

我在用上面链接的标准代码显示准确的时间时遇到了问题。一点也不准确。屏幕上每更新一秒可能需要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()

你的延误来自两个地方

Clock.tick(60)
的调用会休眠一小部分秒,以确保每秒调用60次。在您的情况下,您应该删除它,因为您不是每帧都调用这个函数

更大的延迟来自这样一个事实,即每个函数调用都需要一些时间,而您没有考虑到这一点

你也会在计时器结束时睡眠1秒。这是不准确的


我建议你记录下一个计时器需要多少毫秒,然后从下一个计时器中减去。

你的延迟来自两个地方

Clock.tick(60)
的调用会休眠一小部分秒,以确保每秒调用60次。在您的情况下,您应该删除它,因为您不是每帧都调用这个函数

更大的延迟来自这样一个事实,即每个函数调用都需要一些时间,而您没有考虑到这一点

你也会在计时器结束时睡眠1秒。这是不准确的


我建议你记录下一个计时器需要多少毫秒,然后从下一个计时器中减去。

你的延迟来自两个地方

Clock.tick(60)
的调用会休眠一小部分秒,以确保每秒调用60次。在您的情况下,您应该删除它,因为您不是每帧都调用这个函数

更大的延迟来自这样一个事实,即每个函数调用都需要一些时间,而您没有考虑到这一点

你也会在计时器结束时睡眠1秒。这是不准确的


我建议你记录下一个计时器需要多少毫秒,然后从下一个计时器中减去。

你的延迟来自两个地方

Clock.tick(60)
的调用会休眠一小部分秒,以确保每秒调用60次。在您的情况下,您应该删除它,因为您不是每帧都调用这个函数

更大的延迟来自这样一个事实,即每个函数调用都需要一些时间,而您没有考虑到这一点

你也会在计时器结束时睡眠1秒。这是不准确的


我建议您记录一个计时器多用了多少毫秒,然后从下一个计时器中减去。

您如何检查间隔计时器是否落后?您如何检查间隔计时器是否落后?您如何检查间隔计时器是否落后?您如何检查间隔计时器是否落后?您如何检查间隔计时器是否落后?好的。谢谢你。看起来最好的选择是重写它,只使用一个startTime=time.time()变量并对其进行计算。谢谢你。看起来最好的选择是重写它,只使用一个startTime=time.time()变量并对其进行计算。谢谢你。看起来最好的选择是重写它,只使用一个startTime=time.time()变量并对其进行计算。谢谢你。看起来最好的选择是重写它,只使用一个startTime=time.time()变量并对其进行计算。