Created
November 19, 2024 10:31
-
-
Save uwezi/5ba38183dabbec20b8b815f1f1921644 to your computer and use it in GitHub Desktop.
[timer] A Timer mobject. #manim #animate #decimalnumber #timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://discord.com/channels/581738731934056449/1308355923583963197/1308355923583963197 | |
from manim import * | |
class Timer(VGroup): | |
def __init__(self, starttime=99, countdown=True, run=True): | |
super().__init__() | |
self.currenttime = starttime | |
self.starttime = starttime | |
self.countdown = countdown | |
self.dorun = run | |
self.minten = DecimalNumber(0,num_decimal_places=0) | |
self.minone = DecimalNumber(0,num_decimal_places=0) | |
self.sepminsec = Text(":") | |
self.secten = DecimalNumber(0,num_decimal_places=0) | |
self.secone = DecimalNumber(0,num_decimal_places=2) | |
self.add(self.minten,self.minone,self.sepminsec,self.secten,self.secone) | |
self.arrange(RIGHT,buff=0.05) | |
self.add_updater(self.updaterf, call_updater=True) | |
def stop(self): | |
self.dorun = False | |
def run(self): | |
self.dorun = True | |
def reset(self, starttime=None, run=None): | |
if starttime is not None: | |
self.starttime = starttime | |
self.currenttime = starttime | |
else: | |
self.currenttime = self.starttime | |
if run is not None: | |
self.dorun = run | |
def updaterf(self,mobj,dt): | |
if mobj.dorun: | |
if mobj.countdown: | |
mobj.currenttime -= dt | |
else: | |
mobj.currenttime += dt | |
ctime = mobj.currenttime | |
mobj.minten.set_value(int(ctime/600)) | |
ctime %= 600 | |
mobj.minone.set_value(int(ctime/60)) | |
ctime %= 60 | |
mobj.secten.set_value(int(ctime/10)) | |
ctime %= 10 | |
mobj.secone.set_value(ctime) | |
class stopwatch(Scene): | |
def construct(self): | |
timer = Timer() | |
self.add(timer) | |
self.wait(5) | |
timer.reset(15, run=False) | |
self.wait(5) | |
timer.run() | |
self.wait(5) | |
timer.stop() | |
self.wait(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment