Advertisement
Guest User

Untitled

a guest
May 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. ul=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,5)
  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,9)
  63. monitor.write(" Eldritch ")
  64.  
  65. monitor.setCursorPos(4,12)
  66. monitor.write("Unload Room ")
  67.  
  68. monitor.setBackgroundColour((colours.black))
  69. -- now we have drawn our buttons we should set
  70. -- the text background colour back to black
  71. end
  72. GuiDraw()
  73.  
  74. -- Now we need to check if the button is clicked
  75.  
  76. -- First we are going to create a function called
  77. -- checkClickPosition(). A function will not run
  78. -- until you ask for it.
  79.  
  80. -- We know the first button starts at 2 from the
  81. -- top and 2 from the left. We also know it is 5
  82. -- spaces long. This means the button ends
  83. -- at width 7
  84.  
  85. -- We will be told which width and
  86. -- height the click happened at.
  87. -- If the width position is greater than 1 AND
  88. -- less than 8 we have clicked somewhere between
  89. -- 2 and 7.
  90.  
  91. -- If this is true we can then check the height
  92. -- position. Button one is at height 2 and button
  93. -- two is at height 4.
  94.  
  95. -- This means that if the width is greater than 1
  96. -- AND the width is less than 8 AND the height
  97. -- equals 2 we have clicked button 1
  98.  
  99. -- If the the width is greater than 1 AND the width
  100. -- is less than 8 AND the height equals 4 we have
  101. -- clicked button 2
  102.  
  103. -- now to write this as a function
  104. -- Functions are written like this
  105.  
  106. -- function exampleFunction()
  107. -- print("Hello")
  108. -- sleep(10)
  109. -- print("Goodbye")
  110. -- end
  111.  
  112. -- Now when you write exampleFunction() the program
  113. -- will print hello, sleep for 10 ticks and then
  114. -- print Goodbye.
  115. -- This is useful for making your programs easier
  116. -- to understand
  117. function countdown(c)
  118. for i=1,c do
  119. monitor.setCursorPos(9,2)
  120. monitor.write(" ")
  121. monitor.setCursorPos(9,4)
  122. monitor.write(" ")
  123. monitor.setCursorPos(9,6)
  124. monitor.write(" ".. c .." ")
  125. monitor.setCursorPos(9,8)
  126. monitor.write(" ")
  127. monitor.setCursorPos(9,10)
  128. monitor.write(" ")
  129. sleep(1)
  130. c = c-1
  131.  
  132. end
  133. monitor.clear()
  134. GuiDraw()
  135. end
  136.  
  137. function checkClickPosition()
  138. if mouseWidth > 4 and mouseWidth < 13 and mouseHeight == 2 and ul <> -1 then
  139. -- button one clicked
  140. rs.setOutput("right",true)
  141. sleep(3)
  142. rs.setOutput("right",false)
  143. sleep(5)
  144. countdown(5)
  145. rs.setOutput("top",true)
  146. sleep(3)
  147. rs.setOutput("top",false)
  148. rs.setOutput("bottom",true)
  149. sleep(1)
  150. rs.setOutput("bottom",false)
  151. sleep(5)
  152. ul=-1
  153.  
  154.  
  155. elseif mouseWidth > 4 and mouseWidth < 13 and mouseHeight == 5 and ul <> -1 then
  156. -- button two clicked
  157. rs.setOutput("left",true)
  158. sleep(3)
  159. rs.setOutput("left",false)
  160. sleep(5)
  161. countdown(5)
  162. rs.setOuput("top",true)
  163. sleep(3)
  164. rs.setOuput("top",false)
  165. rs.setOutput("bottom",true)
  166. sleep(1)
  167. rs.setOuput("bottom",false)
  168. sleep(5)
  169. ul=-1
  170.  
  171. elseif mouseWidth > 4 and mouseWidth < 13 and mousHeight == 9 and ul <> -1 then
  172.  
  173. rs.setOutput("front",true)
  174. sleep(3)
  175. rs.setOutput("front",false)
  176. sleep(5)
  177. countdown(5)
  178. rs.setOutput("top",true)
  179. sleep(3)
  180. rs.setOutput("top",false)
  181. rs.setOutput("bottom",true)
  182. sleep(1)
  183. rs.setOuput("bottom",false)
  184. sleep(5)
  185. ul=-1
  186.  
  187. elseif mouseWidth > 4 and mouseWidth < 13 and mousHeight == 12 and ul = -1 then
  188.  
  189. rs.setOutput("front",true)
  190. sleep(3)
  191. rs.setOutput("front",false)
  192. sleep(5)
  193. countdown(5)
  194. rs.setOutput("top",true)
  195. sleep(3)
  196. rs.setOutput("bottom",true)
  197. rs.setOutput("top",false)
  198. sleep(5)
  199. rs.setOuput("bottom",false)
  200. sleep(5)
  201. ul=1
  202.  
  203. -- turns redstone connected to the left off
  204. end -- ends the if loop
  205. end -- ends the function
  206.  
  207. -- this function does nothing until you write
  208. -- checkClickPostion(). We will be doing this below
  209. -- It then checks the click position and turns the
  210. -- lamp on if button one is clicked or turns the
  211. -- lamp off if button two is clicked
  212.  
  213. -- OK. Now we need to check if a click happens
  214. -- we will use a repeat-until loop.
  215. -- In the loop we we use a os.pullEvent().
  216. -- an os.pullEvent() gives you different info
  217. -- depending on the event type. We will mainly
  218. -- check the "monitor_touch" event.
  219.  
  220. -- In the second line you will see
  221. -- event,p1,p2,p3 = os.pullEvent()
  222. -- if the event is a click on the monitor it
  223. -- will give us 4 bits of info:
  224. -- event will be "monitor_touch"
  225. -- p1 will be the side the monitor is on (top)
  226. -- p2 is the width postion of the click
  227. -- p3 is the height postition of the click
  228.  
  229.  
  230.  
  231. repeat
  232. -- repeat runs a loop of code.
  233.  
  234. event,p1,p2,p3 = os.pullEvent()
  235. -- this line tells the computer to wait until
  236. -- an event happens. We are waiting for a
  237. -- touchscreen event
  238.  
  239. if event=="monitor_touch" then
  240. -- this checks to see if the event was a
  241. -- touchscreen event
  242.  
  243. mouseWidth = p2 -- sets mouseWidth
  244. mouseHeight = p3 -- and mouseHeight
  245. checkClickPosition() -- this runs our function
  246.  
  247. end
  248. -- the end of the "if loop".
  249.  
  250.  
  251. until event=="char" and p1==("x")
  252. -- this is the end of the "repeat loop". This will
  253. -- stop the repeat loop if a "char" event happens
  254. -- A char event means you press a character on
  255. -- the keyboard. This line is looking for the x key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement