如何在 pygame(没有 tkinter)的状态栏内制作数字时钟?

How to make a digital clock within a status bar in pygame (without tkinter)?

目前我正在为学校做作业,在 Pygame 中创建一个游戏。我被困在创建一个时钟的过程中,它决定了;天、小时和分钟。

到目前为止,我已经创建了一些代码,但是当我 运行 它时,我的 Pygame 就会崩溃。我知道这可能有点笨拙,但我真的很感激您的帮助。

这是我的代码:

import sys, pygame, random, time
pygame.init()

size = width, height = 1280, 720
screen = pygame.display.set_mode(size)

done = False

Black=0,0,0
White=255,255,255

Time = 0
Minute = 0
Hour = 0
Day = 0

#Fonts
Font = pygame.font.SysFont("Trebuchet MS", 25)

#Day
DayFont = Font.render("Day:"+str(Day),1, Black)
DayFontR=DayFont.get_rect()
DayFontR.center=(985,20)
#Hour
HourFont = Font.render("Hour:"+str(Hour),1, Black)
HourFontR=HourFont.get_rect()
HourFontR.center=(1085,20)
#Minute
MinuteFont = Font.render("Minute:"+str(Minute),1, Black)
MinuteFontR=MinuteFont.get_rect()
MinuteFontR.center=(1200,20)

Clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill(White)

    #Timer
    while Time==0:
        time.sleep(1)
        Minute=Minute+1
        screen.blit(MinuteFont, MinuteFontR)
        if Minute == 60:
            Hour=Hour+1        
            screen.blit(HourFont, HourFontR)
        if Hour==12:
            Hour=0
            Day=Day+1
            screen.blit(DayFont, DayFontR)

    pygame.display.flip()

    Clock.tick(60)

pygame.quit()

您的问题是:

while Time==0: # <--- here
    time.sleep(1)
    # ...    

你用 0 初始化 Time 但你永远不会改变它的值。这会导致无限循环。

这是发布的初始代码的有效解决方案。请注意,时钟计数器元素格式化代码已移至 Pygame 主循环内。第二个修改:blit() 指令在每次循环迭代时被调用,而不仅仅是在各自的 if 中。最后,增加了显示秒数,使样例更加生动。

我不是 Pygame 方面的专家,所以不要认为这个解决方案是最佳解决方案!

import sys, pygame, random, time
pygame.init()

size = width, height = 1380, 720
screen = pygame.display.set_mode(size)

done = False

Black=0,0,0
White=255,255,255

Time = 0
Second = 0
Minute = 0
Hour = 0
Day = 0


Clock = pygame.time.Clock()


while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # Fonts
    Font = pygame.font.SysFont("Trebuchet MS", 25)

    # Day
    DayFont = Font.render("Days:" + str(Day).zfill(2), 1, Black)
    DayFontR = DayFont.get_rect()
    DayFontR.center = (975, 20)
    # Hour
    HourFont = Font.render("Hours:" + str(Hour).zfill(2), 1, Black)
    HourFontR = HourFont.get_rect()
    HourFontR.center = (1075, 20)
    # Minute
    MinuteFont = Font.render("Minutes:" + str(Minute).zfill(2), 1, Black)
    MinuteFontR = MinuteFont.get_rect()
    MinuteFontR.center = (1190, 20)
    # Second
    SecondFont = Font.render("Seconds:" + (str(Second).zfill(2)), 1, Black)
    SecondFontR = SecondFont.get_rect()
    SecondFontR.center = (1317, 20)

    screen.fill(White)

    #Timer
#    while Time==0: this caused the crash !
    time.sleep(1)
    Second += 1
    screen.blit(SecondFont, SecondFontR)
    screen.blit(MinuteFont, MinuteFontR)
    screen.blit(HourFont, HourFontR)
    screen.blit(DayFont, DayFontR)

    if Second == 60:
        Second = 0
        Minute=Minute+1
    if Minute == 60:
        Minute = 0
        Second = 0
        Hour=Hour+1
    if Hour==24:
        Hour=0
        Second = 0
        Minutes=0
        Day=Day+1

    pygame.display.flip()

    Clock.tick(60)

pygame.quit()