【问题标题】:how do you display time in the format 00:00? Currently, it displays the time in a decimal format如何以 00:00 格式显示时间?目前,它以十进制格式显示时间
【发布时间】:2020-01-05 18:49:45
【问题描述】:

计时器工作,但我无法以 00:00 格式显示它 - 目前,它以十进制格式显示时间,例如:2.547959356:688.9846939

while True: # main game loop
        mouseClicked = False

        DISPLAYSURF.fill(BGCOLOR) # drawing the window
        drawBoard(mainBoard, revealedBoxes)

        counting_time = pygame.time.get_ticks() - start_time

        # change milliseconds into minutes, seconds
        counting_seconds = str(counting_time/1000 ).zfill(2)
        counting_minutes = str(counting_time/60000).zfill(2)

        counting_string = "%s:%s" % (counting_minutes, counting_seconds)

        counting_text = font.render(str(counting_string), 1, (0,0,0))

        DISPLAYSURF.blit(counting_text,(350,3))

        pygame.display.update()

        clock.tick(25)

【问题讨论】:

    标签: python timer seconds pygame-clock pygame-tick


    【解决方案1】:
    while True: # main game loop
        mouseClicked = False
    
        DISPLAYSURF.fill(BGCOLOR) # drawing the window
        drawBoard(mainBoard, revealedBoxes)
    
        ########TIMER######
        # Calculate total seconds
        total_seconds = frame_count // frame_rate
        # Divide by 60 to get total minutes
        minutes = total_seconds // 60
        # Use modulus (remainder) to get seconds
        seconds = total_seconds % 60
        # Use python string formatting to format in leading zeros
        output_string = "Time: {0:02}:{1:02}".format(minutes, seconds)
        text = font.render((output_string), True, (0,0,0))
        DISPLAYSURF.blit(text,(350,3))
        frame_count += 1
        # Limit frames per second
        clock.tick(20)
        #update the screen with timer.
        pygame.display.flip()
    

    没关系,我想通了。但是,计时器增加 1 秒需要 4-5 秒。我已经尝试增加和减少时钟滴答声,但似乎没有帮助。有什么解决这个问题的建议吗?

    【讨论】:

      猜你喜欢
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2011-06-27
      • 2014-12-29
      相关资源
      最近更新 更多