timer.py

Created by ziii

Created on February 19, 2022

1.36 KB

A basic timer with a round clock counting seconds and a digital clock with the amount of hours minutes and second elapsed.

How to use :

Ok to start or stop the timer.


from time import *
from math import *
from kandinsky import *
from ion import *
def draw_circle(r,pos,c):
  for x in range(0,r):
    y=((r**2)-(x**2))**0.5
    set_pixel(pos[0]+int(x),pos[1]+int(y),c)
    set_pixel(pos[0]+int(x),pos[1]-int(y),c)
    set_pixel(pos[0]-int(x),pos[1]+int(y),c)
    set_pixel(pos[0]-int(x),pos[1]-int(y),c)
    set_pixel(pos[0]+int(y),pos[1]+int(x),c)
    set_pixel(pos[0]+int(y),pos[1]-int(x),c)
    set_pixel(pos[0]-int(y),pos[1]+int(x),c)
    set_pixel(pos[0]-int(y),pos[1]-int(x),c)
def radius_circle(deg,pos,r,col):
  deg-=90
  deg*=3.14/180
  si=sin(deg)
  co=cos(deg)
  [set_pixel(int(x*co+pos[0]),int(si*x+pos[1]),col) for x in range(0,r)]
run=0
draw_string("OK to start timer",0,0)
sleep(0.2)
while 1:
  if run:
    if keydown(KEY_OK):
      run=0
      fill_rect(0,0,320,222,color(255,255,255))
      draw_string("OK to start timer",0,0)
      sleep(0.2)
    elif monotonic()-tick>sec+1:
      sec=int(monotonic()-tick)
      draw_string("{}:{}:{}{}".format(sec//3600,sec//60,""if sec%60>9 else "0",sec%60),0,0)
      radius_circle(int(6*((sec-1)%60)),[160,111],110,color(255,255,255))
      radius_circle(int(6*(sec%60)),[160,111],110,color(0,0,0))
  else:
    if keydown(KEY_OK):
      run=1
      sec=-1
      tick=monotonic()
      fill_rect(0,0,320,222,color(255,255,255))
      draw_circle(110,[160,111],color(0,0,0))
      sleep(0.25)