Ce programme trace une horloge indiquant l’heure. Utilisation: taper horloge(9,30,120) si vous voulez voir l’heure défiler pendant 2 minutes (120 s) à partir de 9h30. Taper horloge(9,30) simplement pour voir cette heure (9h30) s’afficher. L’étalonnage peut être amélioré (modifier le range de la fonction temporise) si on cherche une meilleure précision.
from kandinsky import * from math import * def cercle(x0,y0,r,c,e): for i in range(2*e): xd=x0-int((r-i*0.5)/sqrt(2)) xf=x0+int((r-i*0.5)/sqrt(2)) for x in range(xd,xf+1): x1=x y1=y0+int(sqrt((r-i*0.5)**2-(x-x0)**2)) set_pixel(x,y1,c) for j in range(3): x2=x0+y1-y0 y2=y0+x0-x1 set_pixel(x2,y2,c) x1,y1=x2,y2 def seg(xa,ya,xb,yb,c): if abs(yb-ya)<abs(xb-xa): if xb<xa: xa,xb=xb,xa ya,yb=yb,ya m=(yb-ya)/(xb-xa) p=ya-m*xa for i in range(xb-xa): set_pixel(int(xa+i), int(m*(xa+i)+p),c) else: if yb<ya: ya,yb=yb,ya xa,xb=xb,xa m=(xb-xa)/(yb-ya) p=xa-m*ya for i in range(yb-ya): set_pixel(int(m*(ya+i)+p), int(ya+i),c) def coord(h,r): t=pi*(h/6+1.5) return [int(r*cos(t)+160), int(r*sin(t)+111)] def heure(hh,mm,r): hm=(hh*60+mm)/60 return coord(hm,r) def cadran(x0,y0,r,c1,e,c2): cercle(x0,y0,r,c1,e) cercle(x0,y0,4,c1,2) for j in range(60): t=pi/30*j set_pixel(int((90)*cos(t)+160), int((90)*sin(t)+111),c1) for i in range(12): h=heure(i+1,0,80) draw_string(str(i+1),h[0]-8,h[1]-8) def temporise(c): for i in range(7800): c=(c**2)%12345 def aiguilles(t,c): h=heure(t[0],t[1],50) seg(160,111,h[0],h[1],c) h=heure(t[1]/5,0,70) seg(160,111,h[0],h[1],c) def horloge(h,m,t=0): time=[h,m] c1=color(0,0,0) #noir c2=color(240,11,20) #trotteuse c3=color(255,255,255)#blanc cadran(160,111,100,c1,4,c2) aiguilles(time,c1) s=0 while s<t: h=heure((s)%60/5,0,80)#trotteuse seg(160,111,h[0],h[1],c2)#trace temporise(98765) s=s+1 seg(160,111,h[0],h[1],c3) aiguilles(time,c3)#efface aiguilles if s%60==0: time[1]+=1 if time[1]==60: time[1]=0 time[0]=(time[0]+1)%12 h=heure(time[0],time[1],50) aiguilles(time,c1)#trace aiguilles cadran(160,111,100,c1,4,c2)