relogio.py

Created by numworks-pt

Created on November 27, 2020

1.92 KB

Este script de Python criado pelo utilizador Philippe Moutou (ph-moutou) e traça um relógio que indica a hora. Utilização: Digite relógio(9,30,120) se quiser ver a contagem das horas durante 2 minutos (120 s) a partir das 9:30. Digite relógio(9,30) apenas para ver esta hora (9:30) afixada. A calibração pode ser melhorada (alterando o alcance da função do temporizador) se for desejada uma melhor precisão.


from kandinsky import *
from math import *
def circulo(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 hora(hh,mm,r):
  hm=(hh*60+mm)/60
  return coord(hm,r)

def mostrador(x0,y0,r,c1,e,c2):
  circulo(x0,y0,r,c1,e)
  circulo(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=hora(i+1,0,80)
    draw_string(str(i+1),h[0]-8,h[1]-8)

def tempo(c):
  for i in range(7800):
    c=(c**2)%12345
    
def ponteiros(t,c):
  h=hora(t[0],t[1],50)
  seg(160,111,h[0],h[1],c)
  h=hora(t[1]/5,0,70)
  seg(160,111,h[0],h[1],c)  

def relogio(h,m,t=0):
  time=[h,m]
  c1=color(0,0,0)      #preto
  c2=color(240,11,20)  #segundos
  c3=color(255,255,255)#branco
  mostrador(160,111,100,c1,4,c2)
  ponteiros(time,c1)
  s=0
  while s<t:
    h=hora((s)%60/5,0,80)#segundos
    seg(160,111,h[0],h[1],c2)#desenhar
    tempo(98765)
    s=s+1
    seg(160,111,h[0],h[1],c3)
    ponteiros(time,c3)#apagar ponteiros
    if s%60==0:
      time[1]+=1
      if time[1]==60:
        time[1]=0
        time[0]=(time[0]+1)%12
    h=hora(time[0],time[1],50)
    ponteiros(time,c1)#desenhar ponteiros
    mostrador(160,111,100,c1,4,c2)

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 cookies policy.