Timer in Python mode

I need timer, which resets when I press keyboard key (for example). I did basic timer, but it not resets, I understand why but I can’t figure it out how to make my class resets on key board hit. For example I need “stop” function for timer and “reset” function.

Here is my code

bg = 0
ts = 28 # text size
start = False

def setup():
    size(700,700)
    textSize(ts)
    # noLoop()

def draw():
    background(bg)
    fill(255)
    
    global bg
    global timer
    global timeleft
    
    timeleft = 10 # 10 seconds
    if (start == True):
        timeleft = 10
    if (start == False):
        timeleft = 0
    timer = Timer(timeleft)
    text(timer.getRemainingTime(), 20, 20)

    hit = "Hit SPACEBAR"
    hitw = textWidth(hit)
    text(hit,(width-hitw)/2,(height-ts)/2)
    print(start)
    print(timer)
    
class Timer: # Timer class
    def __init__(self, duration):
        self.duration = duration
    def getRemainingTime(self):
        return max(0, self.duration - millis()/1000)
            
def keyPressed(): # Press spacebar class + switch on/off
    global bg
    global start
    if (key == ' '): # space
        bg = int(random(100))
        if start == True:
            start = False
        else:
            start = True
            
            

forum.Processing.org/two/discussion/27733/countdown-class-library-for-java-js-python#Item_2

@GoToLoop can you please explain why I need external library?

Calling the class Countdown a “library” is perhaps a little too overrated. :money_mouth_face:

It’s merely a wrapper for the already existing builtin class Timer. :watch:

You can pick the idea and do the same for your own code if you prefer. :bulb:

2 Likes