Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Countdown Class II (Python)
"""
Countdown Class II (v1.2.5)
GoToLoop (2017/Aug/30)
https://Forum.Processing.org/two/discussion/27733/
countdown-class-library-for-java-js-python#Item_3
https://Forum.Processing.org/two/discussion/23846/
time-delay-in-python-mode#Item_17
https://Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1
"""
from threading import Timer
class Countdown:
def __init__(self, waitTime=0.0): # seconds
self.delay = abs(waitTime)
self.done = True
def job(): self.done = True
self._timeout = job
self.task = None
def __str__(self, STR='Delay: %.3f - Done: %s'):
return STR % (self.delay, self.done)
def start(self):
self.task and self.task.cancel()
self.task = Timer(self.delay, self._timeout)
self.done = False
self.task.start()
return self
"""
Countdown Class II (v1.2.5)
GoToLoop (2017/Aug/30)
Forum.Processing.org/two/discussion/27733/
countdown-class-library-for-java-js-python#Item_3
Forum.Processing.org/two/discussion/23846/
time-delay-in-python-mode#Item_17
Gist.GitHub.com/GoToLoop/893dc18856a7842b76a92d8aea837da1
"""
from countdown import Countdown
SECS = 7.5
WAIT_TIME = int(SECS * 1000)
WAITING_BG = PImage.BLUE_MASK | PImage.ALPHA_MASK
DONE_BG = PImage.RED_MASK | PImage.ALPHA_MASK
countdown = Countdown(SECS)
def setup():
size(300, 180)
smooth(3)
frameRate(60)
colorMode(RGB)
fill(0xffFFFF00)
textSize(0100)
textAlign(CENTER, CENTER)
m = millis(); t = m + WAIT_TIME
countdown.start()
print m, t, t - m, TAB, countdown
def draw(AWAIT='Awaiting ' + `SECS`, END='Finished'):
this.surface.title = countdown.done and END or AWAIT
background(DONE_BG if countdown.done else WAITING_BG)
txt = `millis()` + ENTER + `frameCount`
text(txt, width>>1, height>>1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.