Advertisement
jille_Jr

CC: Monitor test

May 28th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1.  
  2. local xPos,yPos = 15,6
  3. local mouseX,mouseY = 15,2
  4. local monitor = peripheral.wrap("top")
  5. or error("Unable to wrap monitor!",0)
  6.  
  7. local function closest(x,y)
  8.     local nearX,nearY = x,y
  9.     local nearestDistance = math.huge
  10.  
  11.     for xOffset = -1,1 do
  12.         for yOffset = -1,1 do
  13.             if (xOffset==0 or yOffset==0) and yOffset~=xOffset then
  14.                 local dx = x+xOffset-xPos
  15.                 local dy = y+yOffset-yPos
  16.                 local distance = math.sqrt(dx^2+dy^2)
  17.  
  18.                 if distance < nearestDistance then
  19.                     nearX = x + xOffset
  20.                     nearY = y + yOffset
  21.                 end
  22.             end
  23.         end
  24.     end
  25.  
  26.     return nearX,nearY
  27. end
  28.  
  29. local function draw()
  30.     monitor.clear()
  31.  
  32.     monitor.setCursorPos(mouseX,mouseY)
  33.     monitor.write("X")
  34.  
  35.     monitor.setCursorPos(closest(mouseX,mouseY))
  36.     monitor.write("o")
  37.  
  38.     monitor.setCursorPos(xPos,yPos)
  39.     monitor.write("T")
  40. end
  41.  
  42. local updateRate = 1/5
  43. local update = os.startTimer(updateRate)
  44. while true do
  45.     local ev,p1,p2,p3,p4,p5 = os.pullEvent()
  46.  
  47.     if ev == "timer" and p1 == update then
  48.         -- time to draw
  49.         update = os.startTimer(updateRate)
  50.         draw()
  51.     elseif ev == "monitor_touch" then
  52.         -- p1 = side of monitor
  53.         mouseX = p2
  54.         mouseY = p3
  55.     end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement