如何以00:00的格式显示时间?目前,它以十进制格式显示时间。

huangapple go评论162阅读模式
英文:

how do you display time in the format 00:00? Currently, it displays the time in a decimal format

问题

计时器正常工作,但我在以格式 00:00 显示它方面遇到了问题。当前,它以十进制格式显示时间,例如:2.547959356:688.9846939。

while True: # 主游戏循环
    mouseClicked = False

    DISPLAYSURF.fill(BGCOLOR) # 绘制窗口
    drawBoard(mainBoard, revealedBoxes)

    counting_time = pygame.time.get_ticks() - start_time

    # 将毫秒转换为分钟和秒
    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)
英文:

the timer works but i am having trouble displaying it in the format 00:00 - Currently, it displays the time in a decimal format for exapmle: 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)

答案1

得分: 0

while True: # 主游戏循环
    mouseClicked = False

    DISPLAYSURF.fill(BGCOLOR) # 绘制窗口
    drawBoard(mainBoard, revealedBoxes)

    ########计时器######
    # 计算总秒数
    total_seconds = frame_count // frame_rate
    # 除以60以获取总分钟数
    minutes = total_seconds // 60
    # 使用模运算(余数)获取秒数
    seconds = total_seconds % 60
    # 使用Python字符串格式化以带有前导零的格式
    output_string = "时间:{0:02}:{1:02}".format(minutes, seconds)
    text = font.render(output_string, True, (0, 0, 0))
    DISPLAYSURF.blit(text, (350, 3))
    frame_count += 1
    # 限制每秒帧数
    clock.tick(20)
    # 使用计时器更新屏幕
    pygame.display.flip()

不用担心我弄清楚了然而计时器每增加1秒需要4-5秒的时间我已经尝试增加和减少时钟滴答声但似乎没有帮助有什么建议可以解决这个问题吗
英文:
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()

never mind I figure it out. however, it takes 4-5 seconds for the timer to increment by 1 second. I have already tried increasing and decreasing the clock ticks but it doesn't seem to help. Any suggestions to solve this?

答案2

得分: 0

只需像这样使用时间库:

import time

start_time = time.time()

while True:
    minutes = int(time.time() - start_time) // 60
    seconds_remaining = int(time.time() - start_time) % 60
    print(f'{minutes}:0{seconds_remaining}' if seconds_remaining < 10 else f'{minutes}:{seconds_remaining}')
英文:

Just use the time library like this:

import time

start_time = time.time()

while True:
    minutes = int(time.time() - start_time) // 60
    seconds_remaining = int(time.time() - start_time) % 60
    print(f&#39;{minutes}:0{seconds_remaining}&#39; if seconds_remaining &lt; 10 else f&#39;{minutes}:{seconds_remaining}&#39;)

huangapple
  • 本文由 发表于 2020年1月6日 02:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定