Joshua
Joshua

Reputation: 35

How do I make pygame display the time and change it when the time changes using fonts?

I have got a working digital clock in python but I am stuck trying to make it a visual in pygame.

The code for the clock works but it just doesn't display anything, even though I have used .blit to do so.

The idea is to have the timer show every minute (Second), Hour(every 60 seconds) and Days (Every 12 in game hours). This is then to appear on the top left.

Here is my code:

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

Sorry if this is nooby, but any help is appreciated

Upvotes: 1

Views: 4679

Answers (1)

Isa
Isa

Reputation: 781

Barring all other problems, I'm not sure what your A1 and A2 are supposed to be, but

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

this will always increase A1 and set it back to zero in the same step, essentially doing nothing, so you never get to the part if A1==1 where you might blit the time.

Apart from that, Font.render() "creates a new Surface with the specified text rendered on it." (cf. the documentation) This means you have to re-render the font every time you want to update the text, otherwise you keep blitting the same (unchanged) surface again and again. You will also need to adjust the rect to account for the text being wider then the time goes up from one digit to two.

The easiest way to keep track of time might be to use a custom user event that's fired every second in the event queue like so:

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

I've zero-padded the minutes, hours and days so you don't have to recalculate the rectangle every time. You could also optimize the draw code by only drawing hours and days if they have changed (in the respective if statements).

To see other methods of how to handle timed events, check out Do something every x (milli)seconds in pygame.

Upvotes: 2

Related Questions