View difference between Paste ID: xzmmN1Wv and eBHRY8eq
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("left")
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.green))
39+
monitor.setBackgroundColour((colours.red))
40
-- this changes the background colour of the text
41
-- to lime green. 
42
43-
monitor.setCursorPos(2,4)
43+
monitor.setCursorPos(1.5,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.setTextColor(colors.black)
49
monitor.write(" OPEN  ")
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-
monitor.setCursorPos(2,5)
53+
monitor.setTextColor(colors.black)
54
55
monitor.setCursorPos(1.5,4)
56
-- this sets the next writing postition to 2 from 
57
-- the left and 4 down from the top. Just under 
58
-- the 1st button
59
60
monitor.write(" CLOSE ")
61
-- this writes OFF but again its 5 long in total
62
-- with the spaces
63
64
monitor.setBackgroundColour((colours.black))
65
-- now we have drawn our buttons we should set 
66
-- the text background colour back to black
67
monitor.setTextColor(colors.black)
68
69
-- Now we need to check if the button is clicked
70
71
-- First we are going to create a function called
72
-- checkClickPosition(). A function will not run 
73
-- until you ask for it.
74
75
-- We know the first button starts at 2 from the 
76
-- top and 2 from the left. We also know it is 5
77
-- spaces long. This means the button ends 
78
-- at width 7
79
80
-- We will be told which width and
81
-- height the click happened at. 
82
-- If the width position is greater than 1 AND 
83
-- less than 8 we have clicked somewhere between
84
-- 2 and 7. 
85
86
-- If this is true we can then check the height
87
-- position. Button one is at height 2 and button
88
-- two is at height 4. 
89
90
-- This means that if the width is greater than 1
91
-- AND the width is less than 8 AND the height
92
-- equals 2 we have clicked button 1
93
94
-- If the the width is greater than 1 AND the width
95
-- is less than 8 AND the height equals 4 we have 
96
-- clicked button 2
97
98
-- now to write this as a function
99
-- Functions are written like this
100
101
--     function exampleFunction()
102
--       print("Hello")
103
--       sleep(10)
104
--       print("Goodbye")
105
--     end
106
107
-- Now when you write exampleFunction() the program
108
-- will print hello, sleep for 10 ticks and then
109
-- print Goodbye. 
110
-- This is useful for making your programs easier 
111
-- to understand
112
113
function checkClickPosition()
114
  if mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 2 then
115
    -- button one clicked
116
    rs.setOutput("right",true)
117
	sleep(3)
118
	rs.setOutput("right",false)
119
    -- turns redstone connected to the right on
120
  elseif mouseWidth > 1 and mouseWidth < 8 and mouseHeight == 4 then
121
    -- button two clicked
122
    rs.setOutput("right",false)
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 key