Advertisement
Guest User

startup

a guest
Apr 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.81 KB | None | 0 0
  1. rednet.open("back")
  2. -- This is a comment. It's for humans only. All
  3. -- the green text is not run by the program
  4.  
  5. mouseWidth = 0
  6. mouseHeight = 0
  7. -- this creates two variables called mouseWidth
  8. -- and mouseHeight and sets them to 0. We will
  9. -- use them later
  10.  
  11. monitor = peripheral.wrap("top")
  12. -- you need this line! It tells the computer
  13. -- the monitor is on top. Change it if you want
  14. -- the monitor on a different side of the computer
  15.  
  16. monitor.clear()
  17. -- this clears the monitor screen
  18.  
  19. monitor.setCursorPos(1,1)
  20. -- this sets the cursor position to the top left
  21. -- corner of the monitor
  22.  
  23. w,h=monitor.getSize()
  24. -- gets the width and the height of the monitor
  25. -- and stores the numbers as w and h.
  26. -- w and h are variables
  27.  
  28. print(w)
  29. print(h)
  30. -- prints the w and h to the computer screen.
  31. -- You can see the monitor width is 7, height is 5
  32. -- It starts in the top left corner like a book.  
  33.  
  34.  
  35. -- Now to draw the two buttons
  36. -- Im english so I write colour but you can change
  37. -- it to color. It works the same.
  38.  
  39.  
  40. monitor.setBackgroundColour((colours.red))
  41. -- this changes the background colour of the text
  42. -- to lime green.
  43.  
  44. monitor.setCursorPos(3,3)
  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("ON")
  50. monitor.setCursorPos(3,9)
  51. monitor.write("OFF")
  52. -- this writes the word ON on the monitor. See the
  53. -- blank spaces before and after. These will be
  54. -- green. Our button i
  55. -- this writes OFF but again its 5 long in total
  56. -- with the spaces
  57.  
  58. monitor.setBackgroundColour((colours.black))
  59. -- now we have drawn our buttons we should set
  60. -- the text background colour back to black
  61.  
  62.  
  63. -- Now we need to check if the button is clicked
  64.  
  65. -- First we are going to create a function called
  66. -- checkClickPosition(). A function will not run
  67. -- until you ask for it.
  68.  
  69. -- We know the first button starts at 2 from the
  70. -- top and 2 from the left. We also know it is 5
  71. -- spaces long. This means the button ends
  72. -- at width 7
  73.  
  74. -- We will be told which width and
  75. -- height the click happened at.
  76. -- If the width position is greater than 1 AND
  77. -- less than 8 we have clicked somewhere between
  78. -- 2 and 7.
  79.  
  80. -- If this is true we can then check the height
  81. -- position. Button one is at height 2 and button
  82. -- two is at height 4.
  83.  
  84. -- This means that if the width is greater than 1
  85. -- AND the width is less than 8 AND the height
  86. -- equals 2 we have clicked button 1
  87.  
  88. -- If the the width is greater than 1 AND the width
  89. -- is less than 8 AND the height equals 4 we have
  90. -- clicked button 2
  91.  
  92. -- now to write this as a function
  93. -- Functions are written like this
  94.  
  95. --     function exampleFunction()
  96. --       print("Hello")
  97. --       sleep(10)
  98. --       print("Goodbye")
  99. --     end
  100.  
  101. -- Now when you write exampleFunction() the program
  102. -- will print hello, sleep for 10 ticks and then
  103. -- print Goodbye.
  104. -- This is useful for making your programs easier
  105. -- to understand
  106.  
  107. function checkClickPosition()
  108.   if mouseWidth > 0 and mouseWidth < 8 and mouseHeight == 3 then
  109.     rednet.broadcast("ON")
  110.   elseif mouseWidth > 2 and mouseWidth < 5 and mouseHeight == 9 then
  111.     rednet.broadcast("OFF")
  112.   end  
  113. end -- ends the function
  114.    
  115. -- this function does nothing until you write
  116. -- checkClickPostion(). We will be doing this below
  117. -- It then checks the click position and turns the
  118. -- lamp on if button one is clicked or turns the
  119. -- lamp off if button two is clicked  
  120.  
  121. -- OK. Now we need to check if a click happens
  122. -- we will use a repeat-until loop.
  123. -- In the loop we we use a os.pullEvent().
  124. -- an os.pullEvent() gives you different info
  125. -- depending on the event type. We will mainly
  126. -- check the "monitor_touch" event.
  127.  
  128. -- In the second line you will see
  129. -- event,p1,p2,p3 = os.pullEvent()
  130. -- if the event is a click on the monitor it
  131. -- will give us 4 bits of info:
  132. --    event will be "monitor_touch"    
  133. --    p1 will be the side the monitor is on (top)
  134. --    p2 is the width postion of the click
  135. --    p3 is the height postition of the click  
  136.  
  137.  
  138.  
  139. repeat
  140. -- repeat runs a loop of code.
  141.  
  142.   event,p1,p2,p3 = os.pullEvent()
  143.   -- this line tells the computer to wait until
  144.   -- an event happens. We are waiting for a
  145.   -- touchscreen event
  146.  
  147.    if event=="monitor_touch" then
  148.    -- this checks to see if the event was a
  149.    -- touchscreen event
  150.    
  151.      mouseWidth = p2 -- sets mouseWidth
  152.      mouseHeight = p3 -- and mouseHeight
  153.      checkClickPosition() -- this runs our function
  154.      
  155.    end
  156.    -- the end of the "if loop".
  157.    
  158.    
  159. until event=="char" and p1==("x")
  160. -- this is the end of the "repeat loop". This will
  161. -- stop the repeat loop if a "char" event happens
  162. -- A char event means you press a character on
  163. -- the keyboard. This line is looking for the x key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement