Advertisement
Guest User

1

a guest
Feb 20th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.22 KB | None | 0 0
  1. EtatPorte = 0
  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. rednet.open("left")
  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(1,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(" Ouvrir  ")
  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(1,4)
  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(" Fermer ")
  60. -- this writes OFF but again its 5 long in total
  61. -- with the spaces
  62.  
  63. monitor.setBackgroundColour((colours.black))
  64. -- now we have drawn our buttons we should set
  65. -- the text background colour back to black
  66.  
  67.  
  68. -- Now we need to check if the button is clicked
  69.  
  70. -- First we are going to create a function called
  71. -- checkClickPosition(). A function will not run
  72. -- until you ask for it.
  73.  
  74. -- We know the first button starts at 2 from the
  75. -- top and 2 from the left. We also know it is 5
  76. -- spaces long. This means the button ends
  77. -- at width 7
  78.  
  79. -- We will be told which width and
  80. -- height the click happened at.
  81. -- If the width position is greater than 1 AND
  82. -- less than 8 we have clicked somewhere between
  83. -- 2 and 7.
  84.  
  85. -- If this is true we can then check the height
  86. -- position. Button one is at height 2 and button
  87. -- two is at height 4.
  88.  
  89. -- This means that if the width is greater than 1
  90. -- AND the width is less than 8 AND the height
  91. -- equals 2 we have clicked button 1
  92.  
  93. -- If the the width is greater than 1 AND the width
  94. -- is less than 8 AND the height equals 4 we have
  95. -- clicked button 2
  96.  
  97. -- now to write this as a function
  98. -- Functions are written like this
  99.  
  100. --     function exampleFunction()
  101. --       print("Hello")
  102. --       sleep(10)
  103. --       print("Goodbye")
  104. --     end
  105.  
  106. -- Now when you write exampleFunction() the program
  107. -- will print hello, sleep for 10 ticks and then
  108. -- print Goodbye.
  109. -- This is useful for making your programs easier
  110. -- to understand
  111. function checkClickPosition()
  112.   if mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 2 then
  113.     -- button one clicked
  114.     EtatPorte = 0
  115.     sleep(0.1)
  116.     rednet.send(20, EtatPorte)
  117.     -- turns redstone connected to the right on
  118.   elseif mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 4 then
  119.     -- button two clicked
  120.     EtatPorte = 1
  121.     sleep(0.1)
  122.     rednet.send(20, EtatPorte)
  123.     -- turns redstone connected to the left off
  124.   end -- ends the if loop
  125. end -- ends the function
  126.    
  127. -- this function does nothing until you write
  128. -- checkClickPostion(). We will be doing this below
  129. -- It then checks the click position and turns the
  130. -- lamp on if button one is clicked or turns the
  131. -- lamp off if button two is clicked  
  132.  
  133. -- OK. Now we need to check if a click happens
  134. -- we will use a repeat-until loop.
  135. -- In the loop we we use a os.pullEvent().
  136. -- an os.pullEvent() gives you different info
  137. -- depending on the event type. We will mainly
  138. -- check the "monitor_touch" event.
  139.  
  140. -- In the second line you will see
  141. -- event,p1,p2,p3 = os.pullEvent()
  142. -- if the event is a click on the monitor it
  143. -- will give us 4 bits of info:
  144. --    event will be "monitor_touch"    
  145. --    p1 will be the side the monitor is on (top)
  146. --    p2 is the width postion of the click
  147. --    p3 is the height postition of the click  
  148.  
  149.  
  150.  
  151. repeat
  152. -- repeat runs a loop of code.
  153.  
  154.   event,p1,p2,p3 = os.pullEvent()
  155.   -- this line tells the computer to wait until
  156.   -- an event happens. We are waiting for a
  157.   -- touchscreen event
  158.  
  159.    if event=="monitor_touch" then
  160.    -- this checks to see if the event was a
  161.    -- touchscreen event
  162.    
  163.      mouseWidth = p2 -- sets mouseWidth
  164.      mouseHeight = p3 -- and mouseHeight
  165.      checkClickPosition() -- this runs our function
  166.      
  167.    end
  168.    -- the end of the "if loop".
  169.    
  170.    
  171. until event=="char" and p1==("x")
  172. -- this is the end of the "repeat loop". This will
  173. -- stop the repeat loop if a "char" event happens
  174. -- A char event means you press a character on
  175. -- the keyboard. This line is looking for the x keys.a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement