Skip to content

Instantly share code, notes, and snippets.

@GoToLoop
Last active April 13, 2018 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GoToLoop/a476ac6a01d18700240853fed33c0e57 to your computer and use it in GitHub Desktop.
Save GoToLoop/a476ac6a01d18700240853fed33c0e57 to your computer and use it in GitHub Desktop.
Countdown Class (Python)
"""
Countdown Class (v1.2.5)
GoToLoop (2017/Aug/26)
https://Forum.Processing.org/two/discussion/27733/
countdown-class-library-for-java-js-python#Item_2
https://Forum.Processing.org/two/discussion/23846/
time-delay-in-python-mode#Item_14
https://Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57
"""
from java.util import Timer, TimerTask
class Countdown:
_t = Timer('Countdown')
def __init__(self, waitTime=0): # milliseconds
self.delay = abs(int(waitTime))
self.done = True
class Job(TimerTask):
def run(_): self.done = True
self._Timeout = Job
self.task = None
def __str__(self, STR='Delay: %d - Done: %s'):
return STR % (self.delay, self.done)
def start(self):
self.task and self.task.cancel()
self.task = self._Timeout()
self.done = False
self._t.schedule(self.task, self.delay)
return self
"""
Countdown Class (v1.2.5)
GoToLoop (2017/Aug/28)
Forum.Processing.org/two/discussion/27733/
countdown-class-library-for-java-js-python#Item_2
Forum.Processing.org/two/discussion/23846/
time-delay-in-python-mode#Item_14
Gist.GitHub.com/GoToLoop/a476ac6a01d18700240853fed33c0e57
"""
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(WAIT_TIME)
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