Search code examples
pythontimercountdown

Timer in Pygame


I'm completely new to Python and I'm trying to set a timer in this game I have... Everything else has been fine but this timer is a headache. I'll only post the parts associated with the timer to make it easier.

frame_count = 0
second = 0
minute = 5
hour = 1
time = "1 5. 0" 

And then in my main loop I have.

font = pygame.font.SysFont('DS-Digital', 50, False, False)
text = font.render(time,True,red)
display.blit(text, [302, 50]) 

frame_count += 1  

if frame_count == 60:
    frame_count = 0
    second -= 1
elif second == 0:
    second = 9
    minute -= 1
elif minute == 0:
    minute = 9
    hour -= 1
elif second == 0 and minute == 0 and hour == 0:
    second = 0
    minute = 0
    hour = 0

hour = time[0]
minute = time[2]
second = time[5]  

clock.tick(60)

This gives me back an error for being wrong type but I've tried converting to int and vice versa... So frustrating...

I've looked at so many examples but most examples are actual minutes and seconds.

I need my right number to just countdown from 9 to 0 then minus off middle number and so forth.


Solution

  • If this your whole code then you didn't imported pygame. (import pygame) at the begining and you should loop it all so for example:

    import pygame
    while True:
        ...
        Your code
        ...
    

    Specify more your question please, I see that you should use if insted of elif because once it got to 1 1 0 it will turn to 1 0 9 and then into 0 9 9 in two frames.

    elif second == 0 and minute == 0 and hour == 0:
        second = 0
        minute = 0
        hour = 0
    

    This doesn't make sence really it's like if you calculate if I have a = 0 then do a = 0 if you know what I mean (it does nothing).

    Edit: There is working code, you can edit it and replace your old code with it.

    import pygame
    pygame.init()
    screen = pygame.display.set_mode((500, 500))
    red = (255, 0, 0)
    bg_color = (0, 0, 0)
    
    frame_count = 0
    time = "1 5. 0"
    
    while True:
        pygame.time.Clock().tick(60)
        frame_count += 1
    
        hour = int(time[0])
        minute = int(time[2])
        second = int(time[5])
    
        if second > 0 and frame_count == 20:
            frame_count = 0
            second -= 1
        if second == 0 and minute > 0 and frame_count == 20:
            frame_count = 0
            second = 9
            minute -= 1
        if minute == 0 and hour > 0 and frame_count == 20:
            frame_count = 0
            minute = 9
            second = 9
            hour -= 1
    
        time = str(hour) + " " + str(minute) + ". " + str(second)
    
        font = pygame.font.SysFont('DS-Digital', 50, False, False)
        text = font.render(time, True, red)
        screen.fill(bg_color)
        screen.blit(text, (302, 50)) 
    
        pygame.display.update()
    

    I'm quite sure that there is easier solution or more pythonic one, but it works and that is most important.