goodtime.py

Created by golem64

Created on July 10, 2021

5.96 KB

Print a fully fonctionnal 8-bits clock. Commands : arrows=move the clock ; On/Off=dark/light mode ; Plus/Minus=increase/decrase clock size ; Ans=reset clock size ; var=change time, OK=set a new event, Home=Open Notifications. Updates soon with some crazy things.


from math import *
from time import *
from kandinsky import *
from ion import *
colors={'bg':(255,255,255),'clock':(0,0,0)}
allowRedraw=1
try:
  get_keys()
except:
  colors.update({'os':(255,181,49)})
else:
  colors.update({'os':(197,52,49)})
def clearDraw(): fill_rect(0,0,320,222,(255,255,255))
def clearHome(): print("\n \n \n \n \n \n \n \n \n \n \n \n \n ")
def redraw(time,redra):
  if redra==1:
    hr="{:0>2}".format(time[0])
    mn="{:0>2}".format(time[1])
    fill_rect(0,0,320,222,colors['bg'])
    printLetter(pixNums[hr[0]],posx,posy,size,colors['clock'])
    printLetter(pixNums[hr[1]],posx+size*4,posy,size,colors['clock'])
    printLetter(pixNums[':'],posx+size*7,posy,size,colors['clock'])
    printLetter(pixNums[mn[0]],posx+size*10,posy,size,colors['clock'])
    printLetter(pixNums[mn[1]],posx+size*14,posy,size,colors['clock'])
    draw_string("{:0>2}".format(time[2]),int(posx+size*7.5),posy+size*6,colors['clock'],colors['bg'])
def printLetter(letter,x,y,size,color):
  for yi in range(5):
    for xi in range(3):
      if letter[yi*3+xi]==1 or letter[yi*3+xi]=="1":
        fill_rect(x+(xi*size),y+(yi*size),size,size,colors['clock'])
      else:
        fill_rect(x+(xi*size),y+(yi*size),size,size,colors['bg'])
notifs=[["Golem64","helo"]]
events={'12:00':'Food ! >w<','00:00':'New day'}
pixNums={
"0":"111101101101111",
"1":"110010010010111",
"2":"111001111100111",
"3":"111001111001111",
"4":"101101111001001",
"5":"111100111001111",
"6":"111100111101111",
"7":"111001001001001",
"8":"111101111101111",
"9":"111101111001111",
":":"000010000010000"
}
time=[]
def timeUp(time,re):
  changed=0
  for i in range(2,-1,-1):
    if (i==0 and int(time[i])==24) or (i!=0 and int(time[i])==60):
      time[i]=0
      time[i+(i==0)-1]+=(-(i==0)+1)
      changed+=1
  for j in range(1,1-changed,-1):
    act=time[j]
    if act<10:
      act="{:0>2}".format(act)
    act=str(act)
    if re==1:
      printLetter(pixNums[act[0]],posx+(size*(j*10)),posy,size,color)
      printLetter(pixNums[act[1]],posx+(size*(j*10)+size*4),posy,size,color)
  return time
timeMode=1
def newTime():
  time=[]
  while len(time)!=3 or (int(time[0])>23 or int(time[0])<0) or (int(time[1])>59 or int(time[1])<0) or (int(time[2])>59 or int(time[2])<0):
    time=input("Time (hh:mm:ss)> ").split(":")
    time[0]=int(time[0])
    time[1]=int(time[1])
    time[2]=int(time[2])
  return time
time=[localtime()[3],localtime()[4],localtime()[5]]
notifs.append(['Clock','Time is now set'])
now=int(monotonic())
posx=0
posy=0
size=10
reb=1
hr="{:0>2}".format(time[0])
mn="{:0>2}".format(time[1])
printLetter(pixNums[hr[0]],posx,posy,size,colors['clock'])
printLetter(pixNums[hr[1]],posx+size*4,posy,size,colors['clock'])
printLetter(pixNums[':'],posx+size*7,posy,size,colors['clock'])
printLetter(pixNums[mn[0]],posx+size*10,posy,size,colors['clock'])
printLetter(pixNums[mn[1]],posx+size*14,posy,size,colors['clock'])
draw_string("{:0>2}".format(time[2]),int(posx+size*7.5),posy+size*6,colors['clock'],colors['bg'])
while keydown(KEY_SHIFT)!=True or keydown(KEY_BACKSPACE)!=True:
  if now!=int(monotonic()):
    if localtime()[0:5]!=(01,01,2000,00,00): time=[localtime()[3],localtime()[4],localtime()[5]]
    else:
      now+=1
      time[2]+=1
    timeUp(time,allowRedraw)
    if allowRedraw==1:
      draw_string("{:0>2}".format(time[2]),int(posx+size*7.5),posy+size*6,colors['clock'],colors['bg'])
  if keydown(0):
    posx-=5
    if posx<0:
      posx=0
    redraw(time,allowRedraw)
  if keydown(1):
    posy-=5
    if posy<0:
      posy=0
    redraw(time,allowRedraw)
  if keydown(2):
    posy+=5
    if posy+(size*6+14)>222:
      posy-=5
    redraw(time,allowRedraw)
  if keydown(3):
    posx+=5
    if posx+(size*17)>320:
      posx-=5
    redraw(time,allowRedraw)
  if keydown(KEY_PLUS):
    size+=1
    if size>19:
      size=19
    redraw(time,allowRedraw)
  if keydown(KEY_MINUS):
    size-=1
    if size<0:
      size=0
    redraw(time,allowRedraw)
  if keydown(KEY_ANS):
    size=10
    redraw(time,allowRedraw)
  if keydown(KEY_ONOFF) and not reb==1:
    a=colors['bg']
    colors.update({'bg':colors['clock']})
    colors.update({'clock':a})
    redraw(time,allowRedraw)
    reb=1
  if not keydown(KEY_ONOFF) and not keydown(KEY_HOME) and not keydown(KEY_OK):
    reb=0
  if keydown(KEY_VAR):
    time=newTime()
    notifs.append(['Clock','Time changed'])
    now=int(monotonic())
    redraw(time,allowRedraw)
  try:
    if allowRedraw==1 and not time[2]>9:
      draw_string("Calendar: "+events["{:0>2}".format(time[0])+":"+"{:0>2}".format(time[1])],posx,posy-(size*2)+((posy+(size*11)<230)*(size*10)),colors['clock'],colors['bg'])
      try:
        notifs.index(["Calendar",events["{:0>2}".format(time[0])+":"+"{:0>2}".format(time[1])]])
      except:
        notifs.append(["Calendar",events["{:0>2}".format(time[0])+":"+"{:0>2}".format(time[1])]])
    elif allowRedraw==1 and time[2]>9:
      fill_rect(posx,posy-(size*2)+((posy+(size*11)<230)*(size*10)),320,20,colors['bg'])
  except:
    pass
  if keydown(KEY_HOME) and not reb==1:
    if allowRedraw==1:
      allowRedraw=0
      fill_rect(0,0,320,255,colors['os'])
      draw_string("Notifications",100,0,colors['clock'],colors['os'])
      fill_rect(0,18,320,2,colors['clock'])
      for i in range(len(notifs)):
        draw_string(notifs[i][0]+":",0,20+i*(20*2),colors['bg'],colors['os'])
        draw_string(notifs[i][1],30,40+i*(20*2),colors['clock'],colors['os'])
        fill_rect(0,59+i*(20*2),320,1,colors['clock'])
    elif allowRedraw==0:
      allowRedraw=1
      redraw(time,allowRedraw)
    reb=1
  if keydown(KEY_OK) and not reb==1:
    ev=input("New event: ")
    evtime=[]
    while len(evtime)!=2 or (int(evtime[0])>23 or int(evtime[0])<0) or (int(evtime[1])>59 or int(evtime[1])<0):
      evtime=input("Event time (hh:mm)> ").split(":")
    events.update({"{:0>2}".format(evtime[0])+":"+"{:0>2}".format(evtime[1]):str(ev)})
    redraw(time,allowRedraw)
    reb=1
  if keydown(KEY_BACKSPACE):
    notifs=[]
    allowRedraw=1
    redraw(time,allowRedraw)
print("Clock exited")
#mode=-(mode==1)+(mode==1)+(mode==0)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.