Advertisement
TeoremaPi

Hay un botón

Oct 8th, 2020 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- programa para crear un mando a distancia con dos botones
  2.  
  3. principal = term.current()
  4. principal.setCursorPos(1, 1)
  5. principal.setTextColor(colors.yellow)
  6. principal.clearLine()
  7. principal.write("Hay un botón:")
  8.  
  9. h = 3
  10. hw, ww = principal.getSize()
  11. w = hw-2
  12.  
  13. textV = {"Encender", "Apagar"}
  14. x = 2
  15.  
  16. -- first button
  17. y1 = 3
  18.  
  19. window1 = window.create(term.current(),x,y1,w,h)
  20. window1.setBackgroundColor(colors.green)
  21. window1.clear()
  22.  
  23. --add text
  24. text = textV[1]
  25. newX = math.floor((w - #text)/2) + 1
  26. newY = math.floor(h/2) + 1
  27. window1.setCursorPos(newX, newY)
  28. window1.setTextColor(colors.black)
  29. window1.write(text)
  30.  
  31.  
  32. -- second button
  33. y2 = y1+h+1
  34.  
  35. window2 = window.create(term.current(),x,y2,w,h)
  36. window2.setBackgroundColor(colors.red)
  37. window2.clear()
  38.  
  39. --add text
  40. text = textV[2]
  41. newX = math.floor((w - #text)/2) + 1
  42. newY = math.floor(h/2) + 1
  43. window2.setCursorPos(newX, newY)
  44. window2.setTextColor(colors.black)
  45. window2.write(text)
  46.  
  47.  
  48. -- start net
  49. rednet.open("back")
  50.  
  51. pcID = 112
  52.  
  53. while true do
  54.  
  55.     event, side, xM, yM = os.pullEvent("mouse_click")
  56.  
  57.     principal.setCursorPos(1, y2+h+1)
  58.     principal.setTextColor(colors.white)
  59.     principal.clearLine()
  60.    
  61.     if xM >= 1 and xM <= ww-1 then
  62.         if yM >= y1 and yM <= y1 + h then
  63.             lastText = "Encendido"
  64.             principal.write(lastText.." en")
  65.  
  66.             rednet.send(pcID,lastText)
  67.            
  68.             for k = 0,3 do
  69.                 principal.write(" "..3-k)
  70.                 if k == 3 then
  71.                     principal.write("!")
  72.                 end
  73.                
  74.                 sleep(1)
  75.             end
  76.  
  77.         elseif yM >= y2 and yM < y2 + h then
  78.             lastText = "Apagado"
  79.             principal.write(lastText)
  80.  
  81.             rednet.send(pcID,lastText)
  82.            
  83.         else
  84.             principal.write(lastText)
  85.         end
  86.     end
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement