toolkit.py

Created by 1epauletteshark

Created on February 09, 2024

2.19 KB


from kandinsky import *
from math import *
from time import *
from ion import *

W,H=320,222
FW,FH=10,16
CM=55
MS=4

def text(text,y=H/2,x=W/2):
  text=str(text)
  draw_string(text,int(x-len(text)*FW/2),int(y-FH/2))

def square(x,y):
  fill_rect(int(x-MS/2),int(y-MS/2),MS,MS,"black")

def fill():
  fill_rect(0,0,W,H,"white")

def circle():
  x,y=W/2,H/4
  dir=0
  for _ in range(360):
    set_pixel(int(x),int(y),"black")
    x+=cos(radians(dir))
    y+=sin(radians(dir))
    dir+=1
  

def line(dir,size,len):
  dir=radians(dir-90)
  x,y=W/2,H/2
  for _ in range(len):
    fill_rect(int(x),int(y),size,size,"black")
    x+=cos(dir)
    y+=sin(dir)

def clock(time):
  line(time*6,1,58)
  line(time/10,2,45)
  line(time/120,3,25)
  circle()
  text(str(int(time/3600%24))+"H "+str(int(time/60%60))+"M "+str(int(time%60))+"S",H/8)

def distance():
  sx,sy=W/2,H/2
  while keydown(KEY_OK):
    pass
  while not keydown(KEY_OK):
    fill()
    if keydown(KEY_DOWN):
      sy+=2
    if keydown(KEY_UP):
      sy-=2
    if keydown(KEY_LEFT):
      sx-=2
    if keydown(KEY_RIGHT):
      sx+=2
    square(sx,sy)
    sleep(0.05)

  x,y=sx,sy

  while keydown(KEY_OK):
    pass
  while not keydown(KEY_OK):
    fill()
    square(sx,sy)
    if keydown(KEY_DOWN):
      y+=2
    if keydown(KEY_UP):
      y-=2
    if keydown(KEY_LEFT):
      x-=2
    if keydown(KEY_RIGHT):
      x+=2
    square(x,y)
    dist=sqrt((x-sx)**2+(y-sy)**2)
    text(str(round(dist))+" pixels",20)
    text(str(round(dist/CM,2))+" cm",40)
    sleep(0.05)

def ruler():
  for l in range(floor(H/CM)):
    text(l+1,l*CM+FH,5)
    fill_rect(FW,l*CM+FH,int(W/2),2,"black")
    for i in range(10):
      fill_rect(FW,int(l*CM+i*CM/10+FH),int(W/3),1,"black")

def stopwatch():
  while keydown(KEY_OK):
    pass
  start=monotonic()
  while not keydown(KEY_OK):
    fill()
    clock(monotonic()-start)
    sleep(0.05)

def timer(time):
  text("Press OK to start")
  while keydown(KEY_OK):
    pass
  while not keydown(KEY_OK):
    pass
  start=monotonic()
  while time-(monotonic()-start)>0:
    fill()
    clock(time-(monotonic()-start))
    sleep(0.05)
  

print("-"*43)
print("distance()")
print("ruler()")
print("stopwatch()")
print("timer(s)")
print("-"*43)