游戏倒计时显示。带pygam的Tabata计时器

2024-10-01 11:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用pygame创建一个Tabata或HIIT计时器。基本上,我想以秒为单位进行上下计数,并显示已用或剩余的时间。我修改了在这里找到的代码http://www.pygame.org/project-Countdown-1237-.html

我有问题的准确时间显示与标准代码链接以上。这一点都不准确。屏幕上每更新一秒可能就要2秒。所以倒计时的时间是原来的两倍。所以我修改了代码以获得更精确的时间计数。在

首先,我不确定这是最好的办法。最后,我会希望有一个回合,上升和下降计数器重复。我最大的问题是大约2分钟后时间就结束了。因此,真正的运行时间可能是2:00分钟,但间隔计时器落后一两秒。随着计时器继续运行,显示的时间继续落后于实际运行时间n秒,情况继续恶化。任何帮助都将不胜感激。我见过有人用游戏时间但我不确定这能让我更接近我需要完成的目标

谢谢

#!/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()

Tags: posiftimedisplay时间screenglobalsurface
1条回答
网友
1楼 · 发布于 2024-10-01 11:32:55

你的延误来自两个地方。在

Clock.tick(60)的调用会休眠一小段时间,以确保每秒调用60次。在您的例子中,您应该删除它,因为您不是每帧都调用这个函数。在

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

你也在计时器的末尾睡觉1秒钟。这不准确。在

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

相关问题 更多 >