Advertisement
Guest User

door

a guest
Apr 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.75 KB | None | 0 0
  1. -- This is a comment. It's for humans only. All
  2. -- the green text is not run by the program
  3.  
  4. mouseWidth = 0
  5. mouseHeight = 0
  6. -- this creates two variables called mouseWidth
  7. -- and mouseHeight and sets them to 0. We will
  8. -- use them later
  9.  
  10. monitor = peripheral.wrap("top")
  11. -- you need this line! It tells the computer
  12. -- the monitor is on top. Change it if you want
  13. -- the monitor on a different side of the computer
  14.  
  15. monitor.clear()
  16. -- this clears the monitor screen
  17.  
  18. monitor.setCursorPos(1,1)
  19. -- this sets the cursor position to the top left
  20. -- corner of the monitor
  21.  
  22. w,h=monitor.getSize()
  23. -- gets the width and the height of the monitor
  24. -- and stores the numbers as w and h.
  25. -- w and h are variables
  26.  
  27. print(w)
  28. print(h)
  29. -- prints the w and h to the computer screen.
  30. -- You can see the monitor width is 7, height is 5
  31. -- It starts in the top left corner like a book.  
  32.  
  33.  
  34. -- Now to draw the two buttons
  35. -- Im english so I write colour but you can change
  36. -- it to color. It works the same.
  37.  
  38.  
  39. monitor.setBackgroundColour((colours.red))
  40. -- this changes the background colour of the text
  41. -- to lime green.
  42.  
  43. monitor.setCursorPos(1,3)
  44. -- this sets the start position for writing the 1st
  45. -- button on the monitor. It puts it 2 in from the
  46. -- left and 2 down from the top.
  47.  
  48. monitor.write(" Kinyit")
  49. -- this writes the word ON on the monitor. See the
  50. -- blank spaces before and after. These will be
  51. -- green. Our button i
  52. -- this writes OFF but again its 5 long in total
  53. -- with the spaces
  54.  
  55. monitor.setBackgroundColour((colours.black))
  56. -- now we have drawn our buttons we should set
  57. -- the text background colour back to black
  58.  
  59.  
  60. -- Now we need to check if the button is clicked
  61.  
  62. -- First we are going to create a function called
  63. -- checkClickPosition(). A function will not run
  64. -- until you ask for it.
  65.  
  66. -- We know the first button starts at 2 from the
  67. -- top and 2 from the left. We also know it is 5
  68. -- spaces long. This means the button ends
  69. -- at width 7
  70.  
  71. -- We will be told which width and
  72. -- height the click happened at.
  73. -- If the width position is greater than 1 AND
  74. -- less than 8 we have clicked somewhere between
  75. -- 2 and 7.
  76.  
  77. -- If this is true we can then check the height
  78. -- position. Button one is at height 2 and button
  79. -- two is at height 4.
  80.  
  81. -- This means that if the width is greater than 1
  82. -- AND the width is less than 8 AND the height
  83. -- equals 2 we have clicked button 1
  84.  
  85. -- If the the width is greater than 1 AND the width
  86. -- is less than 8 AND the height equals 4 we have
  87. -- clicked button 2
  88.  
  89. -- now to write this as a function
  90. -- Functions are written like this
  91.  
  92. --     function exampleFunction()
  93. --       print("Hello")
  94. --       sleep(10)
  95. --       print("Goodbye")
  96. --     end
  97.  
  98. -- Now when you write exampleFunction() the program
  99. -- will print hello, sleep for 10 ticks and then
  100. -- print Goodbye.
  101. -- This is useful for making your programs easier
  102. -- to understand
  103.  
  104. function checkClickPosition()
  105.   if mouseWidth > 0 and mouseWidth < 8 and mouseHeight == 3 then
  106.     -- button one clicked
  107.     rs.setOutput("right",true)
  108.     sleep(2)
  109.     rs.setOutput("right",false)
  110.   else
  111.     os.reboot()
  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