Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Since this is a basic indepenant
- -- menu, the use of access specifiers
- -- are not necessary.
- -- Access specifiers in LUA: "local"
- nSelect = 1 -- Holds the current selection
- nWidth, nHeight = term.getSize() -- Size of the screen
- -- You can set up menues within tables
- -- however I don't, because I am lazy :P
- -- Tables for menues are beneficial (spelling lol)
- -- They make your code efficient by allowing for
- -- use of the '#' operator.
- function cPrint(height, string)
- local xPos = nWidth/2 - string.len(string)/2
- -- The abve line holds the position to print
- -- In the exact center of the screen.
- term.setCursorPos(xPos, height)
- term.write(string)
- end
- function clear() term.clear(); term.setCursorPos(1, 1) end
- -- Clears the screen.
- function printMenu()
- -- This is where tables for menues are good
- -- You can call menues in a simple and small
- -- loop. Instead I just right long 'if' 'else'
- -- statements.
- if nSelect == 1 then cPrint(6, "[ Option 1 ]")
- else cPrint(6, " Option 1 ") end
- -- The above checks if we selected this option
- -- If we did then print in the center of the screen
- -- The option with brackets. If we didn't
- -- then print the option in the same spot without
- -- brackets. We put spaces in the non bracketed
- -- options because it prevents the brackets
- -- from not being cleared when we print the options.
- if nSelect == 2 then cPrint(7, "[ Option 2 ]")
- else cPrint(7, " Option 2 ") end
- if nSelect == 3 then cPrint(8, "[ Exit ]")
- else cPrint(8, " Exit ") end
- -- Always make sure your strings to print
- -- are aligned. It helps :D
- end
- -- Here is where we loop all of the stuff together
- -- literally.
- -- When creating infinite loops as the one below
- -- it is common practice to have a variable as
- -- the condition instead of 'true, but I don't care
- -- right now :P.
- while true do
- clear() -- Clear the screen to be updated.
- printMenu() -- Update the screen
- event, key = os.pullEvent()
- if event == "key" then
- if key == 200 and nSelect > 1 then nSelect = nSelect-1
- elseif key == 208 and nSelect < 3 then nSelect = nSelect+1
- elseif key == 28 and nSelect == 3 then clear(); print(os.version()); break
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement