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

Help--On LogIn Menu

Started by DryToby, 30 September 2012 - 12:53 AM
DryToby #1
Posted 30 September 2012 - 02:53 AM
OK So To Start Off 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( "--MineLogin--" )
	term.setCursorPos(w-11,1)
	if nOption == 1 then
	term.write( "LogIn" )
	elseif nOption == 2 then
	term.write( "Reboot" )
	else
	term.write( "ShutDown" )
	end
  
end
-- Display the frontend
term.clear()
local function drawFrontend()
	printCentred( math.floor(h/2) - 3, "" )
	printCentred( math.floor(h/2) - 2, "--MineLogin--" )
	printCentred( math.floor(h/2) - 1, "" )
	printCentred( math.floor(h/2) + 0, ((nOption == 1) and "[ Login ]") or "Login" )
	printCentred( math.floor(h/2) + 1, ((nOption == 2) and "[ Reboot ]") or "Reboot" )
	printCentred( math.floor(h/2) + 2, ((nOption == 3) and "[ Shutdown  ]") or "Shutdown " )
	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
clear()
-- Conditions
if nOption == 1 then
print("Login")
elseif nOption == 2 then
print("Shutdown")
else
print("Reboot")
end


Ok So I Need Help Making A Menu With The Fallowing Options
1-Login
2-Reboot
3-ShutDown

So If You Could Help Thanks

Also This Is What It Looks Like
Ps I Get Errors
Pharap #2
Posted 30 September 2012 - 03:58 AM
If you are using someone else's code, please mention it straight away, it saves the whole
'so what does this do' 'I don't know I didn't write the code' 'oh, wonderful *sarcasm* '
conversation that inevitably follows.

You might want to try using some less complicated code than this if you're still fairly new to the whole programming thing.
I'm no beginner, but frankly I wouldn't want to be using this code, it's rather scattered and only semi-orderly.
DryToby #3
Posted 30 September 2012 - 04:18 AM
Ok first oh have to say I was looking up how
To make a menu and this one was sugested so I look at it
The Original had this
-option 1
-option 2
-option 3

So I like this

I really like the layout so that's why

I want help
Please help
I just need a way to do this
Iknow a lot but not a lot about menus
I need these options
-Login
-Shudown
- Reboot

Thanks
MrBarry #4
Posted 30 September 2012 - 04:21 AM
in drawFrontend, calculating math.floor(h/2) seven times is unnecessary.
Your error is probably coming from clear() toward the bottom. Should be term.clear(), I think.
DryToby #5
Posted 30 September 2012 - 04:30 AM
Wow I haven't tried it yet but is clear a command cuase how could I not
See that your right it should be term.clear() Iv done stuff with the terms
But I still need help with this code I want…well you no I said a couple of Times
mrSLIMEguy #6
Posted 30 September 2012 - 04:36 AM
Firstly, this belongs in the Ask a Pro section…
Anyway for the shutdown and reboot, (suspecting you now how to make code happen when you press a button) you can use
os.reboot() and os.shutdown()

And for the login system, I dont know if you want GUI or text here…

I also cant get a good text login system programmed right now…
DryToby #7
Posted 30 September 2012 - 04:53 AM
Ok is this right So the —login— is ware the login goes right
And also would the code be right


end
term.clear()
-- Conditions
if nOption == 1 then
---login----
elseif nOption == 2 then
os.shutdown()
elseif nOption == 3 then
os.reboot()
end
Pharap #8
Posted 30 September 2012 - 05:38 AM
Ok, forget all the fancy centring for the minute.
Selectable options:


local optnumber = 1 --variable to store current option number


while true do --an endless loop
term.clear() --clear screen
term.setCursorPos(1,1,) --returns the cursor to the top left
if optnumber == 1 then -- if condition 1
print("[ option 1 ]")
print("  option 2  ")
print("  option 3  ")
elseif optnumber == 2 then -- if condition 2
print("  option 1  ")
print("[ option 2 ]")
print("  option 3  ")
elseif optnumber == 3 then -- if condition 3
print("  option 1 ]")
print("  option 2  ")
print("[  option 3 ]")
end -- end if conditions
local e, k = os.pullEvent("key")
-- pauses until the user presses a key
--e registers the event as a key event
--k gets the keycode signifying which key was pressed
if k == keys.W then -- test if user pressed W
if optnumber > 1 then --if option is more than 1 (stops cursor going beyond 1)
optnumber = optnumber - 1 --subtract 1 from the option
end -- end if
elseif k == keys.X then -- test if user pressed X
  if optnumber < 3 then --if option is less than 3 (stops cursor going beyond 3)
optnumber = optnumber + 1 --add 1 to the option
end -- end if
elseif k == keys.enter then -- test if user pressed enter

end -- end ifs
end -- ends the loop

That is probably the most basic system you can find. Once you have that working and understand why it works, then I'll show you a better method (if you try this one first, you'll have a better understanding of why the other option is better).
DryToby #9
Posted 30 September 2012 - 02:10 PM
Sorry I put the code on computer and it says 6 unexpected symbol so It Dose not work
makerimages #10
Posted 30 September 2012 - 02:18 PM
remove the last , in that line
DryToby #11
Posted 30 September 2012 - 02:24 PM
ok im really stupid i dont know what you mean there is no last in all of the script show the code
im still new with a lot of stuff
DryToby #12
Posted 30 September 2012 - 02:51 PM
Can also some one tell me whats wrong with this code please



term.clear()
term.setCursorPos(1,1)
write( "Login: " )
input = read( )
if input == "Invalid" then
   print("Loged In")
   else
   print("Wrong Password Rebooting")
   os.restart
   end
MrBarry #13
Posted 30 September 2012 - 09:27 PM
Look at the os API again.
DryToby #14
Posted 30 September 2012 - 09:50 PM
Ok Never Mind wow Not focuse Ok thanks Its like this


os.shutdown()
mrSLIMEguy #15
Posted 01 October 2012 - 03:00 AM
Ok Never Mind wow Not focuse Ok thanks Its like this


os.shutdown()

You can do os.reboot() like you were going to
DryToby #16
Posted 01 October 2012 - 03:03 AM
Ok Thanks I think I Got The Hole Code ill show it when got hole thing together
DryToby #17
Posted 01 October 2012 - 03:18 AM
So This is the code tell me if this I good


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( "--MineLogin--" )
	term.setCursorPos(w-11,1)
	if nOption == 1 then
	term.write( "LogIn" )
	elseif nOption == 2 then
	term.write( "Reboot" )
	else
	term.write( "ShutDown" )
	end

end
-- Display the frontend
term.clear()
local function drawFrontend()
	printCentred( math.floor(h/2) - 3, "" )
	printCentred( math.floor(h/2) - 2, "--MineLogin--" )
	printCentred( math.floor(h/2) - 1, "" )
	printCentred( math.floor(h/2) + 0, ((nOption == 1) and "[ Login ]") or "Login" )
	printCentred( math.floor(h/2) + 1, ((nOption == 2) and "[ Reboot ]") or "Reboot" )
	printCentred( math.floor(h/2) + 2, ((nOption == 3) and "[ Shutdown  ]") or "Shutdown " )
	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
term.clear()
term.setCursorPos(1,1)
write( "Login: " )
input = read()
if input == "Password" then
   print("Loged In")
   else
   print("Wrong Password Rebooting..")
   os.reboot()
   end
elseif nOption == 2 then
os.shutdown()
elseif nOption == 3 then
os.reboot()
end
Pharap #18
Posted 02 October 2012 - 09:49 PM
Sorry I put the code on computer and it says 6 unexpected symbol so It Dose not work

Sorry, that was a typo, try it now:

local optnumber = 1 --variable to store current option number

while true do --an endless loop
term.clear() --clear screen
term.setCursorPos(1,1) --returns the cursor to the top left
if optnumber == 1 then -- if condition 1
print("[ option 1 ]")
print("  option 2  ")
print("  option 3  ")
elseif optnumber == 2 then -- if condition 2
print("  option 1  ")
print("[ option 2 ]")
print("  option 3  ")
elseif optnumber == 3 then -- if condition 3
print("  option 1  ")
print("  option 2  ")
print("[ option 3 ]")
end -- end if conditions
local e, k = os.pullEvent("key")
-- pauses until the user presses a key
--e registers the event as a key event
--k gets the keycode signifying which key was pressed
if k == keys.w then -- test if user pressed W
if optnumber > 1 then --if option is more than 1 (stops cursor going beyond 1)
optnumber = optnumber - 1 --subtract 1 from the option
end -- end if
elseif k == keys.x then -- test if user pressed X
  if optnumber < 3 then --if option is less than 3 (stops cursor going beyond 3)
optnumber = optnumber + 1 --add 1 to the option
end -- end if
elseif k == keys.enter then -- test if user pressed enter
end -- end ifs
end -- ends the loop