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

[Programming] [menu] Choice system. Help please!

Started by Goof, 23 September 2012 - 01:11 PM
Goof #1
Posted 23 September 2012 - 03:11 PM
Hey…



i've made this realy hardy code….. but. when it comes to a specific line / SECTION of code, it just freezed the screen. i could not move / write anything, and then i got Black screen in the terminal… Why???

can someone fix the code, or just solve the problem???


Thanks!



My code is here .



Spoiler



---[[ SECTION: VARIABLES

D1 = "-"
D2 = "!"
D3 = "-"
D4 = "-"
D5 = "-"
D6 = "-"
D7 = "!"
D8 = "-"
D9 = "-"
DX1 = "!"
DX2 = "!"
loop1 = true
loop2 = false
loop3 = false
loop4 = 1
loopJail = false
loopJail1 = false
loopJail2 = false
loopLogin1 = false
loopLogin2 = false
OptionC = 0
OptionB = 0
Line1 = 0
Line2 = 0
Line3 = 0
Line4 = 0

---]] SECTION: VARIABLES


---[[ SECTION: MENU

function menu1(id, text)
if Line1 == id then
write"-> "
elseif Line1 == 1 then
write" "
end
print(text)
end

function menu2(id, text)
if Line2 == id then
write"-> "
elseif Line2 == 1 then
write" "
end
print(text)
end

function menu3(id, text)
if Line3 == id then
write"-> "
elseif Line3 == 1 then
write" "
end
print(text)
end

---]] SECTION: MENU


---[[ SECTION: JAIL SCREEN                        ---- in this SECTION i cant Write/do something.... i cant move with arrow keys anymore... ----

function Jail()
term.clear()
term.setCursorPos(1,1)
print("+--------------------------+")
print("+ Loading Jail OverView... +")
print("+--------------------------+")
sleep(1.5)
while loopJail do
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("+---+---+---+---+---+---+---+---+---+")
textutils.slowPrint("+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 +")
textutils.slowPrint("+-"..D1.."-+-"..D2.."-+-"..D3.."-+-"..D4.."-+-"..D5.."-+-"..D6.."-+-"..D7.."-+-"..D8.."-+-"..D9.."-+")
term.setCursorPos(1,4)
print("+---+")
print(""..DX1.."   "..DX2.."")
print("+---+")
loopJail = false
sleep(1.7)
term.setCursorPos(10,7)
term.clearLine()
term.setCursorPos(10,7)
menu2(0, "Open/Close Doors")
term.setCursorPos(10,8)
term.clearLine()
term.setCursorPos(10,8)
menu2(1, "Exit")

event, key = os.pullEvent("key")
if key == 200 then
if Line2 > 0 then
Line2 = Line2 - 1
end
elseif key == 208 then
if Line2 < 1 then
Line2 = Line2 + 1
end
elseif key == 28 then
if Line1 == 0 then
 term.setCursorPos(10,7)
 term.clearLine()
 term.setCursorPos(10,8)
 term.clearLine()
 term.setCursorPos(5,10)
loopJail1 = true
while loopJail1 do
term.setCursorPos(5,10)
term.clearLine()
term.setCursorPos(5,10)
menu3(0, "Door 1")
term.setCursorPos(5,11)
term.clearLine()
term.setCursorPos(5,11)
menu3(1, "Door 2")
term.setCursorPos(5,12)
term.clearLine()
term.setCursorPos(5,12)
menu3(2, "Door 3")
term.setCursorPos(5,13)
term.clearLine()
term.setCursorPos(5,13)
menu3(3, "Door 4")
term.setCursorPos(5,14)
term.clearLine()
term.setCursorPos(5,14)
menu3(4, "Door 5")
term.setCursorPos(5,15)
term.clearLine()
term.setCursorPos(5,15)
menu3(5, "Door 6")
term.setCursorPos(5,16)
term.clearLine()
term.setCursorPos(5,16)
menu3(6, "Door 7")
term.setCursorPos(5,17)
term.clearLine()
term.setCursorPos(5,17)
menu3(7, "Door 8")
term.setCursorPos(5,18)
term.clearLine()
term.setCursorPos(5,18)
menu3(8, "Door 9")


end
end
end
end
end
---]] SECTION: JAIL SCREEN

---[[ SECTION: LOGIN



function CheckAPILog()
while loopLogin2 do
repeat
if loop4 == 1 then
loop4 = 0

term.clear()
term.setCursorPos(1,1)
print("+------------------+")
print("+ Enter Password!  +")
print("+------------------+")
sleep(2)
term.setCursorPos(1,2)
print("+                  +")
term.setCursorPos(3,2)

PassWord = read("#")
if PassWord == "0903" then


term.setCursorPos(1,2)
print("+ Checking Input!  +")
sleep(2)
term.setCursorPos(1,2)
print("+ Password Correct +")
sleep(2)

  term.clear()
   loopJail = true
loop2 = false
loopLogin2 = false
loop4 = 0
Jail()




loop2 = false
loopLogin2 = false
loop4 = 0
sleep(1)
break
elseif PassWord ~= "0903" then
term.setCursorPos(1,2)
print("+ Checking Input!  +")
sleep(2)
term.setCursorPos(1,2)
print("+ Input Incorrect! +")
sleep(1.7)
loopLogin2 = true
loop2 = true
loop4 = 1
end
end
until i == 100
end
end










if loop4 == 1 then
while loop1 do
term.clear()
term.setCursorPos(2,1)
menu1(0, "Login")
term.setCursorPos(2,2)
menu1(1, "Exit")
event, key = os.pullEvent("key")
if key == 200 then
if Line1 > 0 then
Line1 = Line1 - 1
end
elseif key == 208 then
if Line1 < 1 then
Line1 = Line1 + 1
end
elseif key == 28 then
if Line1 == 0 then --Login System Automation
loop1 = false
loopLogin1 = true
while loopLogin1 do
term.clear()
term.setCursorPos(1,1)
print("+------------------+")
print("+ Enter Username:  +")
print("+------------------+")
sleep(2)
term.setCursorPos(1,2)
term.clearLine()
term.setCursorPos(1,2)
print("+                  +")
term.setCursorPos(3,2)
UserName = read()
if UserName == "Admin1" then
sleep(0.6)
term.setCursorPos(1,2)
print("+ Checking input.  +")
sleep(2)
term.setCursorPos(1,2)
print("+ Username Found!  +")
sleep(1)
term.setCursorPos(1,2)
print("+ Welcome "..UserName.."   +")
sleep(1.6)
loopLogin1 = false
loopLogin2 = true
loop2 = true
while loopLogin2 do
while loop2 do
CheckAPILog()

end
loop2 = true
loopLogin2 = true

end
elseif UserName ~= "Admin1" then
sleep(0.6)
term.setCursorPos(1,2)
print("+ Checking input.  +")
sleep(1.7)
term.setCursorPos(1,2)
print("+ Input Not found! +")
sleep(1)

loopLogin1 = true
end

end

elseif Line1 == 1 then
os.shutdown()
end
loop1 = true

end
end
end
---]] SECTION: LOGIN

Anonomit #2
Posted 23 September 2012 - 07:02 PM
You need a while loop containing the line in the jail section:

event, key = os.pullEvent("key")

the problem is that it only waits for a key once. if that key isn't key 28, it gets stuck in a while loop with no sleep call. That's what's been crashing your program. Try adding a while loop.



while true do
event, key = os.pullEvent("key")
if key == 200 then
  --change around menu
elseif key == 208 then
  --change around menu
elseif key == 28 then
  --break
end
end

--this happens only if key 28 is pressed