Advertisement
Guest User

newstart

a guest
May 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.57 KB | None | 0 0
  1.  
  2. mouseWidth = 0
  3. mouseHeight = 0
  4. -- this creates two variables called mouseWidth
  5. -- and mouseHeight and sets them to 0. We will
  6. -- use them later
  7.  
  8. monitor = peripheral.wrap("back")
  9. -- you need this line! It tells the computer
  10. -- the monitor is on top. Change it if you want
  11. -- the monitor on a different side of the computer
  12.  
  13. monitor.clear()
  14. -- this clears the monitor screen
  15.  
  16. monitor.setCursorPos(1,1)
  17. -- this sets the cursor position to the top left
  18. -- corner of the monitor
  19.  
  20. w,h=monitor.getSize()
  21. -- gets the width and the height of the monitor
  22. -- and stores the numbers as w and h.
  23. -- w and h are variables
  24. print(w)
  25. print(h)
  26. -- prints the w and h to the computer screen.
  27. -- You can see the monitor width is 7, height is 5
  28. -- It starts in the top left corner like a book.  
  29.  
  30.  
  31. -- Now to draw the two buttons
  32. -- Im english so I write colour but you can change
  33. -- it to color. It works the same.
  34. function GuiDraw()
  35.  rs.setOutput("left",false)
  36.  rs.setOutput("right",false)
  37.  rs.setOutput("front",false)
  38.  rs.setOutput("bottom",false)
  39.  
  40.  monitor.setBackgroundColour((colours.red))
  41. -- this changes the background colour of the text
  42. -- to lime green.
  43.  
  44.  monitor.setCursorPos(4,2)
  45. -- this sets the start position for writing the 1st
  46. -- button on the monitor. It puts it 2 in from the
  47. -- left and 2 down from the top.
  48.  
  49.  monitor.write(" Requirement")
  50. -- this writes the word ON on the monitor. See the
  51. -- blank spaces before and after. These will be
  52. -- green. Our button is 5 letters long
  53.  
  54.  monitor.setCursorPos(4,6)
  55. -- this sets the next writing postition to 2 from
  56. -- the left and 4 down from the top. Just under
  57. -- the 1st button
  58.  
  59.  monitor.write("    NODE    ")
  60. -- this writes OFF but again its 5 long in total
  61. -- with the spaces
  62.  monitor.setCursorPos(4,10)
  63.  monitor.write("  Eldritch  ")
  64.  
  65.  monitor.setBackgroundColour((colours.black))
  66. -- now we have drawn our buttons we should set
  67. -- the text background colour back to black
  68. end
  69. GuiDraw()
  70.  
  71. -- Now we need to check if the button is clicked
  72.  
  73. -- First we are going to create a function called
  74. -- checkClickPosition(). A function will not run
  75. -- until you ask for it.
  76.  
  77. -- We know the first button starts at 2 from the
  78. -- top and 2 from the left. We also know it is 5
  79. -- spaces long. This means the button ends
  80. -- at width 7
  81.  
  82. -- We will be told which width and
  83. -- height the click happened at.
  84. -- If the width position is greater than 1 AND
  85. -- less than 8 we have clicked somewhere between
  86. -- 2 and 7.
  87.  
  88. -- If this is true we can then check the height
  89. -- position. Button one is at height 2 and button
  90. -- two is at height 4.
  91.  
  92. -- This means that if the width is greater than 1
  93. -- AND the width is less than 8 AND the height
  94. -- equals 2 we have clicked button 1
  95.  
  96. -- If the the width is greater than 1 AND the width
  97. -- is less than 8 AND the height equals 4 we have
  98. -- clicked button 2
  99.  
  100. -- now to write this as a function
  101. -- Functions are written like this
  102.  
  103. --     function exampleFunction()
  104. --       print("Hello")
  105. --       sleep(10)
  106. --       print("Goodbye")
  107. --     end
  108.  
  109. -- Now when you write exampleFunction() the program
  110. -- will print hello, sleep for 10 ticks and then
  111. -- print Goodbye.
  112. -- This is useful for making your programs easier
  113. -- to understand
  114. function countdown(c)
  115.         for i=1,c do
  116.                 monitor.setCursorPos(1,1)
  117.                 monitor.write("       ")
  118.                 monitor.setCursorPos(1,2)
  119.                 monitor.write("       ")
  120.                 monitor.setCursorPos(1,3)
  121.                 monitor.write("   ".. c .."   ")
  122.                 monitor.setCursorPos(1,4)
  123.                 monitor.write("       ")
  124.                 monitor.setCursorPos(1,5)
  125.                 monitor.write("       ")
  126.                 sleep(1)
  127.                 c = c-1
  128.  
  129.      end
  130.      monitor.clear()
  131.      GuiDraw()
  132. end
  133.  
  134. function checkClickPosition()
  135.   if mouseWidth > 4 and mouseWidth < 13 and mouseHeight == 2 then
  136.     -- button one clicked
  137.     rs.setOutput("right",true)
  138.     sleep(3)
  139.     rs.setOutput("right",false)
  140.     sleep(5)
  141.     countdown(5)
  142.     rs.setOutput("top",true)
  143.     sleep(3)
  144.     rs.setOutput("top",false)
  145.     rs.setOutput("bottom",true)
  146. sleep(1)
  147.     rs.setOutput("bottom",false)
  148.     sleep(5)
  149.  
  150.  
  151.   elseif mouseWidth > 4 and mouseWidth < 13 and mouseHeight == 6 then
  152.     -- button two clicked
  153.     rs.setOutput("left",true)
  154.     sleep(3)
  155.     rs.setOutput("left",false)
  156.     sleep(5)
  157.     countdown(5)
  158.     rs.setOuput("top",true)
  159.     sleep(3)
  160.     rs.setOuput("top",false)
  161.     rs.setOutput("bottom",true)
  162. sleep(1)
  163.     rs.setOuput("bottom",false)
  164.     sleep(5)
  165.    
  166.   elseif mouseWidth > 4 and mouseWidth < 13 and mousHeight == 10 then
  167.  
  168.     rs.setOutput("front",true)
  169.     sleep(3)
  170.     rs.setOutput("front",false)
  171.     sleep(5)
  172.        countdown(5)
  173.     rs.setOutput("top",true)
  174.     sleep(3)
  175.     rs.setOutput("top",false)
  176.     rs.setOutput("bottom",true)
  177. sleep(1)
  178.     rs.setOuput("bottom",false)
  179.     sleep(5)
  180.    
  181.     -- turns redstone connected to the left off
  182.   end -- ends the if loop
  183. end -- ends the function
  184.    
  185. -- this function does nothing until you write
  186. -- checkClickPostion(). We will be doing this below
  187. -- It then checks the click position and turns the
  188. -- lamp on if button one is clicked or turns the
  189. -- lamp off if button two is clicked  
  190.  
  191. -- OK. Now we need to check if a click happens
  192. -- we will use a repeat-until loop.
  193. -- In the loop we we use a os.pullEvent().
  194. -- an os.pullEvent() gives you different info
  195. -- depending on the event type. We will mainly
  196. -- check the "monitor_touch" event.
  197.  
  198. -- In the second line you will see
  199. -- event,p1,p2,p3 = os.pullEvent()
  200. -- if the event is a click on the monitor it
  201. -- will give us 4 bits of info:
  202. --    event will be "monitor_touch"    
  203. --    p1 will be the side the monitor is on (top)
  204. --    p2 is the width postion of the click
  205. --    p3 is the height postition of the click  
  206.  
  207.  
  208.  
  209. repeat
  210. -- repeat runs a loop of code.
  211.  
  212.   event,p1,p2,p3 = os.pullEvent()
  213.   -- this line tells the computer to wait until
  214.   -- an event happens. We are waiting for a
  215.   -- touchscreen event
  216.  
  217.    if event=="monitor_touch" then
  218.    -- this checks to see if the event was a
  219.    -- touchscreen event
  220.    
  221.      mouseWidth = p2 -- sets mouseWidth
  222.      mouseHeight = p3 -- and mouseHeight
  223.      checkClickPosition() -- this runs our function
  224.      
  225.    end
  226.    -- the end of the "if loop".
  227.    
  228.    
  229. until event=="char" and p1==("x")
  230. -- this is the end of the "repeat loop". This will
  231. -- stop the repeat loop if a "char" event happens
  232. -- A char event means you press a character on
  233. -- the keyboard. This line is looking for the x key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement