Advertisement
Guest User

newstart

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