Advertisement
Guest User

startup

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