How to change the timer in pygame?

Hello everybody, I have done the code in pycharm to make a time limit but i saw that the time goes forward like instead of backwards.
Does anyone know what i can change in the code so that the time will go backwards instead of forward?

Use -= instead of +=?

2 Likes

It didn’t work its just the same time but it added the - only here.

copy and paste your code here otherwise I can’t help you

that’s a screenshot you need to copy and paste all of your code

import pygame
pygame.init()

window= pygame.display.set_mode((500, 500))

done = False
secs = 0
mins = 0
hours = 0

available_fonts = pygame.font.get_fonts()

font = pygame.font.Font(None, 32)
text = font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center =500// 2, 500// 2

clock = pygame.time.Clock()

while not done:
    clock.tick(1)
    secs -= 1
    window.blit(text, textRect)
    if secs == 60:
        secs = 0
        mins -= 1
    if mins == 60:
        mins = 0
        secs = 00
        hours -= 1
    text= font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255), (0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
    pygame.display.update()

To make the timer “count down”, you also need to have an initial time to count down from.
So, set secs, mins, and hours to a time and then keep the subtraction code.

2 Likes

Okay i want it to have it only seconds so like 60 secs and down… is that something i can change here you think?

set seconds to 59 then

1 Like

Before you start the game loop, set secs to 59

1 Like

and @amel8star if you want it to stop after it hits 0, put this if statement before decrementing secs

    if secs > 0:
        secs -= 1
2 Likes

I followed the steps, but its just goes up like before instead of going down like what i wanted…

import pygame
pygame.init()

window= pygame.display.set_mode((500, 500))

done = False
secs = 59
mins = 0
hours = 0

available_fonts = pygame.font.get_fonts()

font = pygame.font.Font(None, 32)
text = font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center =500// 2, 500// 2

clock = pygame.time.Clock()

while not done:
    clock.tick(1)
    secs += 59
    window.blit(text, textRect)
    if secs > 0:
        mins += 1
    if mins == 60:
        mins = 0
        secs -= 1
        hours -= 1
    text= font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255), (0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
    pygame.display.update()

this should be

secs -= 1
    window.blit(text, textRect)
    if secs > 59:
        mins += int(secs / 60)

all of a sudden it gave me a error.

change line 13 back to

I didnt get an error so it worked, but at the end when i run it i only get a black screen and not the timer in it

import pygame
pygame.init()

window = pygame.display.set_mode((500,500))

done = False
secs = 0
mins = 0
hours = 0

available_fonts = pygame.font.get_fonts()

font = pygame.font.Font(None, 32)
text = font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center= 500// 2, 500// 2

clock= pygame.time.Clock()

while not done:
    clock.tick(1)
    secs -= 1
    window.blit(text, textRect)
    if secs > 59:
        secs = 0
        mins += secs / 60
    if mins == 60:
        mins == 0
        secs == 00
        hours+= 1
    text = font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255),  (0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()

you need to add this line back at the end

pygame.display.update()

I added it, but the timer doesnt add up. Its just counting from -1 and up. Dont know where it didnt went well. I wanted it from 59 and down…

import pygame
pygame.init()

window = pygame.display.set_mode((500,500))

done = False
secs = 0
mins = 0
hours = 0

available_fonts = pygame.font.get_fonts()

font = pygame.font.Font(None, 32)
text = font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255), (0, 0, 0))
textRect = text.get_rect()
textRect.center= 500// 2, 500// 2

clock= pygame.time.Clock()

while not done:
    clock.tick(1)
    secs -= 1
    window.blit(text, textRect)
    if secs > 59:
        secs = 0
        mins += secs / 60
    if mins == 60:
        mins == 0
        secs == 00
        hours+= 1
    text = font.render(f"{hours}:{mins}:{secs}", True, (255, 255, 255),  (0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
    pygame.display.update()

this should be 59 not 0

these need to be = instead of ==