Kingdaro

CC 1.46 Analog Clock

Oct 25th, 2012
630
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.38 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local handLength = args[1] or 15
  4.  
  5. local function round(n)
  6.   return math.floor(n + 1)
  7. end
  8.  
  9. local function setColor(color)
  10.   if term.isColor() then
  11.     term.setTextColor(color)
  12.   end
  13. end
  14.  
  15. local function drawClock()
  16.   local w,h = term.getSize()
  17.   local cx, cy = w/2, h/2
  18.  
  19.   term.clear()
  20.  
  21.   local setPos = term.setCursorPos
  22.   for i=1, 12 do
  23.     local x,y =
  24.     math.sin(i/12 * math.pi*2)*handLength,
  25.     -math.cos(i/12 * math.pi*2)*handLength * (4/7)
  26.    
  27.     setPos(round(cx + x),round(cy + y))
  28.     setColor(colors.lightBlue)
  29.     write(i)
  30.   end
  31.  
  32.   setPos(cx + 1, cy + 1)
  33.   setColor(colors.white)
  34.   write '*'
  35.  
  36.   local function drawHand(r)
  37.     local char = '.'
  38.     for i=2, round(handLength/3), 2 do
  39.       local x,y =
  40.       math.sin(r * math.pi*2)*i,
  41.       -math.cos(r * math.pi*2)*i
  42.      
  43.       setPos(round(cx + x), round(cy + y))
  44.       write(char)
  45.     end
  46.   end
  47.  
  48.   local hours = os.time()
  49.   local minutes = hours%2
  50.  
  51.   setColor(colors.lime)
  52.   drawHand(hours*2/24)
  53.   setColor(colors.magenta)
  54.   drawHand(minutes/2)
  55.  
  56.   setPos(w-5, h)
  57.   if hours >= 0 and hours < 12 then
  58.     setColor(colors.orange)
  59.     write 'AM'
  60.     setColor(colors.gray)
  61.     write '/PM'
  62.   else
  63.     setColor(colors.gray)
  64.     write 'AM/'
  65.     setColor(colors.lightBlue)
  66.     write 'PM'
  67.   end
  68. end
  69.  
  70. while true do
  71.   drawClock()
  72.   sleep(0.05)
  73. end
Advertisement
Add Comment
Please, Sign In to add comment