Countdown timer is not working

[Original post] Countdown timer in python

I’ve been using this timer in my game for a while, but I’ve just noticed a problem.
The timer utilises second(), once the seconds on my clock go past 59 and turn to 0, the timer in my game adds 60 seconds to the countdown. How can I fix this? Thank you so much!

Code:

start = False

def setup():
    size(500, 500)
    
def draw():
    global timer
    background(0)
    if start:
        textSize(100)
        text(timer.remainingTime(), 220, 280)
    else:
        rect(200, 200, 100, 100)
    

class Timer:
    def __init__(self, duration):
        self.duration = duration
        self.start_time = second()
    def remainingTime(self):
        return self.duration - (second() - self.start_time)
    
def mousePressed(): 
    global start, timer
    start = True
    timer = Timer(10)

Use millis() instead. This returns the number of milliseconds since starting the sketch (not the current second from your computer clock) –

class Timer:
    def __init__(self, duration):
        self.duration = duration
        self.start_time = millis()/1000
    def remainingTime(self):
        return self.duration - (millis()/1000 - self.start_time)
1 Like