Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Notes ]]--
- --[[
- -- It is a good thing to organize your code.
- -- I have placed comments explaining the code.
- -- You should put your functions and variables before the code that calls them, so they aren't "nil"
- -- The floor number is not known when the computer is restarted, so you should send it to the floor it's not on to set it, or it can go either way.
- --]]
- --[[ Local Variables ]]--
- Listen = true -- Start the loop to listen for key presses
- if fs.exists("floor.txt") then -- Is the floor location saved?
- f = fs.open("floor.txt", "r") -- Open "floor.txt" in read mode
- GetN = f.readLine() -- Get the number from the file
- f.close() -- close the file
- n = tonumber(GetN) -- Change "n" to n... Making it a number instead of a string (string in " 's and number not in " 's)
- else
- n = nil
- end
- --[[ Functions ]]--
- -- Elevator --
- function elevateDown() -- You define the function
- redstone.setOutput("top", true) -- You power redstone on the top of the computer
- sleep(0.5) -- Sleep for 0.5 seconds
- redstone.setOutput("top", false) -- You take out the power for the top of the computer
- end -- End the function
- function elevateUp()
- redstone.setOutput("bottom", true) -- You power redstone on the top of the computer
- sleep(0.5)
- redstone.setOutput("bottom", false) -- You take out the power from the bottom of the computer
- end
- while Listen do -- While you want to have someone press keys, do the following code:
- repeat -- Start a repeat loop
- event, key = os.pullEvent('key') -- Listen for a key event
- os.sleep(0.1)
- until key -- Exit loop when key has been pressed
- if key == 200 then -- Check if up key was pressed
- if n == 1 then -- Check if elevator is already at the top floor
- print("Elevator is already at the top floor.") -- Tell the player the elevator is already at the top floor
- else
- n = 1 -- Set the floor value
- Listen = false -- Exit the loop to listen for keys
- end
- elseif key == 208 then -- Check if down key was pressed
- if n == 2 then
- print("Elevator is already at the bottom floor.")
- else
- n = 2
- Listen = false
- end
- else -- If the key pressed was invalid...
- print("Please press Up or Down key.") -- Tell the player, and then reset the loop!
- end
- end
- while not Listen do -- Starts elevator actions when not listening for key presses.
- if n == 1 then -- Should the elevator go up?
- for i=1,11 do
- elevateUp() -- Run the function "elevateUp()"
- sleep(0.5)
- end
- f = fs.open("floor.txt", "w") -- Open "floor.txt" in "write" mode
- f.writeLine(n) -- Write the value of "n" to the file
- f.close() -- Close the file
- os.reboot()
- elseif n == 2 then -- Should the elevator go down?
- for i = 1, 11 do
- elevateDown() -- Run the function "elevateDown()"
- sleep(0.5)
- end
- f = fs.open("floor.txt", "w")
- f.writeLine(n)
- f.close()
- os.reboot()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment