SHOW:
|
|
- or go back to the newest paste.
| 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.lime)) | |
| 40 | -- this changes the background colour of the text | |
| 41 | -- to lime green. | |
| 42 | ||
| 43 | monitor.setCursorPos(2,2) | |
| 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(" ON ")
| |
| 49 | -- this writes the word ON on the monitor. See the | |
| 50 | -- blank spaces before and after. These will be | |
| 51 | -- green. Our button is 5 letters long | |
| 52 | ||
| 53 | monitor.setCursorPos(2,4) | |
| 54 | -- this sets the next writing postition to 2 from | |
| 55 | -- the left and 4 down from the top. Just under | |
| 56 | -- the 1st button | |
| 57 | ||
| 58 | monitor.write(" OFF ")
| |
| 59 | -- this writes OFF but again its 5 long in total | |
| 60 | -- with the spaces | |
| 61 | ||
| 62 | monitor.setBackgroundColour((colours.black)) | |
| 63 | -- now we have drawn our buttons we should set | |
| 64 | -- the text background colour back to black | |
| 65 | ||
| 66 | ||
| 67 | -- Now we need to check if the button is clicked | |
| 68 | ||
| 69 | -- First we are going to create a function called | |
| 70 | -- checkClickPosition(). A function will not run | |
| 71 | -- until you ask for it. | |
| 72 | ||
| 73 | -- We know the first button starts at 2 from the | |
| 74 | -- top and 2 from the left. We also know it is 5 | |
| 75 | -- spaces long. This means the button ends | |
| 76 | -- at width 7 | |
| 77 | ||
| 78 | -- We will be told which width and | |
| 79 | -- height the click happened at. | |
| 80 | -- If the width position is greater than 1 AND | |
| 81 | -- less than 8 we have clicked somewhere between | |
| 82 | -- 2 and 7. | |
| 83 | ||
| 84 | -- If this is true we can then check the height | |
| 85 | -- position. Button one is at height 2 and button | |
| 86 | -- two is at height 4. | |
| 87 | ||
| 88 | -- This means that if the width is greater than 1 | |
| 89 | -- AND the width is less than 8 AND the height | |
| 90 | -- equals 2 we have clicked button 1 | |
| 91 | ||
| 92 | -- If the the width is greater than 1 AND the width | |
| 93 | -- is less than 8 AND the height equals 4 we have | |
| 94 | -- clicked button 2 | |
| 95 | ||
| 96 | -- now to write this as a function | |
| 97 | -- Functions are written like this | |
| 98 | ||
| 99 | -- function exampleFunction() | |
| 100 | -- print("Hello")
| |
| 101 | -- sleep(10) | |
| 102 | -- print("Goodbye")
| |
| 103 | -- end | |
| 104 | ||
| 105 | -- Now when you write exampleFunction() the program | |
| 106 | -- will print hello, sleep for 10 ticks and then | |
| 107 | -- print Goodbye. | |
| 108 | -- This is useful for making your programs easier | |
| 109 | -- to understand | |
| 110 | ||
| 111 | function checkClickPosition() | |
| 112 | if mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 2 then | |
| 113 | -- button one clicked | |
| 114 | rs.setOutput("right",true)
| |
| 115 | -- turns redstone connected to the right on | |
| 116 | elseif mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 4 then | |
| 117 | -- button two clicked | |
| 118 | rs.setOutput("right",false)
| |
| 119 | -- turns redstone connected to the left off | |
| 120 | end -- ends the if loop | |
| 121 | end -- ends the function | |
| 122 | ||
| 123 | -- this function does nothing until you write | |
| 124 | -- checkClickPostion(). We will be doing this below | |
| 125 | -- It then checks the click position and turns the | |
| 126 | -- lamp on if button one is clicked or turns the | |
| 127 | -- lamp off if button two is clicked | |
| 128 | ||
| 129 | -- OK. Now we need to check if a click happens | |
| 130 | -- we will use a repeat-until loop. | |
| 131 | -- In the loop we we use a os.pullEvent(). | |
| 132 | -- an os.pullEvent() gives you different info | |
| 133 | -- depending on the event type. We will mainly | |
| 134 | -- check the "monitor_touch" event. | |
| 135 | ||
| 136 | -- In the second line you will see | |
| 137 | -- event,p1,p2,p3 = os.pullEvent() | |
| 138 | -- if the event is a click on the monitor it | |
| 139 | -- will give us 4 bits of info: | |
| 140 | -- event will be "monitor_touch" | |
| 141 | -- p1 will be the side the monitor is on (top) | |
| 142 | -- p2 is the width postion of the click | |
| 143 | -- p3 is the height postition of the click | |
| 144 | ||
| 145 | ||
| 146 | ||
| 147 | repeat | |
| 148 | -- repeat runs a loop of code. | |
| 149 | ||
| 150 | event,p1,p2,p3 = os.pullEvent() | |
| 151 | -- this line tells the computer to wait until | |
| 152 | -- an event happens. We are waiting for a | |
| 153 | -- touchscreen event | |
| 154 | ||
| 155 | if event=="monitor_touch" then | |
| 156 | -- this checks to see if the event was a | |
| 157 | -- touchscreen event | |
| 158 | ||
| 159 | mouseWidth = p2 -- sets mouseWidth | |
| 160 | mouseHeight = p3 -- and mouseHeight | |
| 161 | checkClickPosition() -- this runs our function | |
| 162 | ||
| 163 | end | |
| 164 | -- the end of the "if loop". | |
| 165 | ||
| 166 | ||
| 167 | until event=="char" and p1==("x")
| |
| 168 | -- this is the end of the "repeat loop". This will | |
| 169 | -- stop the repeat loop if a "char" event happens | |
| 170 | -- A char event means you press a character on | |
| 171 | -- the keyboard. This line is looking for the x key |