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