This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kaiman's profile picture

Help with looping!

Started by kaiman, 26 December 2012 - 12:04 AM
kaiman #1
Posted 26 December 2012 - 01:04 AM
HELP!!
I'm currently working on a main menu, and having some difficulties…
this is my code:

local w,h = term.getSize()
function printCentred( y, s )
local x = math.floor((w - string.len(s)) / 2)
term.setCursorPos(x,y)
term.clearLine()
term.write( s )
end

local nOption = 1
– Display menu
local function drawMenu()
term.clear()
term.setCursorPos(1,1)
term.write( "MainMenu" )

term.setCursorPos(w-11,1)
if nOption == 1 then
term.write( "Open gates" )
elseif nOption == 2 then
term.write( "Close gates" )
else
term.write( "Maintenance" )
end

end
– Display the frontend
term.clear()
local function drawFrontend()
printCentred( math.floor(h/2) - 3, "" )
printCentred( math.floor(h/2) - 2, "Welcome to the net, please pick an option:" )
printCentred( math.floor(h/2) - 1, "" )
printCentred( math.floor(h/2) + 0, ((nOption == 1) and "[ OPEN GATES ]") or "OPEN GATES" )
printCentred( math.floor(h/2) + 1, ((nOption == 2) and "[ CLOSE GATES ]") or "CLOSE GATES" )
printCentred( math.floor(h/2) + 2, ((nOption == 3) and "[ MAINTAINENCE ]") or "MAINTAINENCE" )
printCentred( math.floor(h/2) + 3, "" )
end


– Call functions for display menu
drawMenu()
drawFrontend()

while true do
local e,p = os.pullEvent()
if e == "key" then
local key = p
if key == 17 or key == 200 then
– Up
if nOption > 1 then
nOption = nOption - 1
drawMenu()
drawFrontend()
end
elseif key == 31 or key == 208 then
– Down
if nOption < 3 then – Change 3 by the number of option.
nOption = nOption + 1
drawMenu()
drawFrontend()
end
elseif key == 28 then
– Enter
break
end
end
end
term.clear()

– Conditions
if nOption == 1 then
print ("Opening Door")
redstone.setOutput("back", true)

elseif nOption == 2 then
print ("Closing Door")
redstone.setOutput("back", false)

else
print("Togling")
redstone.getOutput("back")
end





I want this menu to loop, but i dont know where to put the code and wich one, please help!?

Greetings Kaiman
remiX #2
Posted 26 December 2012 - 05:24 AM
This belongs in Ask a Pro

ANd people fix your code, it's unreadable with the html coding.