如何让pygame显示时间并在使用字体更改时间时更改它?

时间:2016-06-27 02:44:42

标签: python python-2.7 animation pygame blit

我在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,但感谢任何帮助

1 个答案:

答案 0 :(得分:0)

除了所有其他问题,我不确定你的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)这意味着每次要更新文本时都必须重新渲染字体,否则会一次又一次地保持相同(未更改)的表面。您还需要调整矩形以使文本更宽,然后时间从一位数增加到两位。

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

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