【问题标题】:How do I make pygame display the time and change it when the time changes using fonts?如何使 pygame 显示时间并在使用字体更改时间时更改它?
【发布时间】:2016-06-27 02:44:42
【问题描述】:

我在 python 中有一个可以工作的数字时钟,但我一直试图让它在 pygame 中成为可视化。

时钟的代码有效,但它不显示任何内容,即使我使用了 .blit 来显示。

这个想法是让计时器每分钟(秒)、小时(每 60 秒)和天(每 12 个游戏小时)显示一次。然后它会出现在左上角。

这是我的代码:

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

#Screen
size = width, height = 1280, 720 #Make sure background image is same size
screen = pygame.display.set_mode(size)

done = False

#Animation
A1=0
A2=0

#Time Info
Time = 0
Minute = 0
Hour = 0
Day = 0
counter=0

#Colour
Black = (0,0,0)
White = (255, 255, 255)

#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)

#Images

Timer=pygame.time.get_ticks

Clock = pygame.time.Clock()

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

    screen.fill(White)

    #Timer
    if Time<60:
        time.sleep(1)
        Minute=Minute+1
        if Minute == 60:
            Hour=Hour+1
            Minute=0
        if Hour==12:
            Day=Day+1
            Hour=0

    if A1==0:
        A1=A1+1
        A2=A2+1
        time.sleep(1)
        if A1==1 or A2==1:
            A2=A2-1
            A1=A1-1         
    if A1==1:
        screen.blit(MinuteFont, MinuteFontR)
        screen.blit(HourFont, HourFontR)
        screen.blit(DayFont, DayFontR)
    if A2==0:
        screen.fill(pygame.Color("White"), (1240, 0, 40, 40))


    pygame.display.flip()

    Clock.tick(60)

pygame.quit()

对不起,如果这是nooby,但任何帮助表示赞赏

【问题讨论】:

    标签: python python-2.7 animation pygame blit


    【解决方案1】:

    除了所有其他问题,我不确定您的 A1 和 A2 应该是什么,但是

    if A1==0: #true for the first run through
        A1=A1+1 #A1 = 1
        A2=A2+1
        time.sleep(1)
        if A1==1 or A2==1: #always true, since A1==1
            A2=A2-1
            A1=A1-1 #A1 = 0
    

    这将始终增加 A1 并在同一步骤中将其设置回零,基本上什么都不做,因此您永远不会到达可能会占用时间的部分 if A1==1

    除此之外,Font.render()“创建一个新的 Surface,并在其上呈现指定的文本。” (参见documentation)这意味着您每次想要更新文本时都必须重新渲染字体,否则您会一次又一次地保持相同(未更改)的表面。您还需要调整 rect 以说明文本变宽,然后时间从一位数增加到两位数。

    跟踪时间的最简单方法可能是使用事件队列中每秒触发的自定义用户事件,如下所示:

    import pygame
    pygame.init()
    
    #Screen
    size = width, height = 1280, 720 #Make sure background image is same size
    screen = pygame.display.set_mode(size)
    
    done = False
    
    #Time Info
    Time = 0
    Minute = 0
    Hour = 0
    Day = 0
    counter=0
    
    #Colour
    Black = (0,0,0)
    White = (255, 255, 255)
    
    #Fonts
    Font = pygame.font.SysFont("Trebuchet MS", 25)
    
    #Day
    DayFont = Font.render("Day:{0:03}".format(Day),1, Black) #zero-pad day to 3 digits
    DayFontR=DayFont.get_rect()
    DayFontR.center=(985,20)
    #Hour
    HourFont = Font.render("Hour:{0:02}".format(Hour),1, Black) #zero-pad hours to 2 digits
    HourFontR=HourFont.get_rect()
    HourFontR.center=(1085,20)
    #Minute
    MinuteFont = Font.render("Minute:{0:02}".format(Minute),1, Black) #zero-pad minutes to 2 digits
    MinuteFontR=MinuteFont.get_rect()
    MinuteFontR.center=(1200,20)
    
    Clock = pygame.time.Clock()
    CLOCKTICK = pygame.USEREVENT+1
    pygame.time.set_timer(CLOCKTICK, 1000) # fired once every second
    
    screen.fill(White)
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == CLOCKTICK: # count up the clock
                #Timer
                Minute=Minute+1
                if Minute == 60:
                    Hour=Hour+1
                    Minute=0
                if Hour==12:
                    Day=Day+1
                    Hour=0
                # redraw time
                screen.fill(White)
                MinuteFont = Font.render("Minute:{0:02}".format(Minute),1, Black)
                screen.blit(MinuteFont, MinuteFontR)
                HourFont = Font.render("Hour:{0:02}".format(Hour),1, Black)
                screen.blit(HourFont, HourFontR)
                DayFont = Font.render("Day:{0:03}".format(Day),1, Black)
                screen.blit(DayFont, DayFontR)
    
                pygame.display.flip()
    
        Clock.tick(60) # ensures a maximum of 60 frames per second
    
    pygame.quit()
    

    我已经对分钟、小时和天进行了零填充,因此您不必每次都重新计算矩形。您还可以优化绘制代码,如果它们已更改(在相应的 if 语句中),则仅绘制小时和天。

    要了解如何处理定时事件的其他方法,请查看Do something every x (milli)seconds in pygame

    【讨论】:

    • 非常感谢你,伙计。这些信息也会对我有很大帮助。 :) 实际上我已经坚持了这么久,我很高兴你能帮到她:)
    猜你喜欢
    • 1970-01-01
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 2013-06-14
    • 1970-01-01
    相关资源
    最近更新 更多