I'm trying to write a program that counts down to 0, and the countdown can be stopped by entering a password. Ideally it would look like this
Time left 0:00:59
Password: ******
and you can enter something for the password
so my two questions are how do I get the time to countdown automatically I've tried
time = ("10")
while true do
print(time-1)
sleep(1)
end
but that just outputs 9 over and over, so how do I get the timer to countdown?
and is it possible to have both a changing number at the top and something you can input without being interrupted below? If so, how?
I'm pretty new to all this, and thank you guys so much if you can help with this! Cheers!
Countdown program
Started by AmbientSound, Sep 23 2012 04:02 PM
3 replies to this topic
#1
Posted 23 September 2012 - 04:02 PM
#2
Posted 23 September 2012 - 04:21 PM
For the code you showed, put "time = time - 1" after the print. Before you weren't actually changing the variable, only printing it minus 1, that's why you got 9 over and over. To have it be able to have a password entered uninterrupted would be difficult, and I'm not sure I know how.
EDIT: I would suggest using a for loop, not a while loop, using a while loop would make it continue to countdown even after it reached 0.
EDIT: I would suggest using a for loop, not a while loop, using a while loop would make it continue to countdown even after it reached 0.
#3
Posted 23 September 2012 - 04:22 PM
You just have to use parallel API:
term.clear()
local pas
function Count()
local time=60
while time~=0 do
sleep(1)
term.setCursorPos(1,1)
term.clearLine()
time=time-1
write("0:00:"..time)
end
term.setCursorPos(1,1)
term.clearLine()
write("BOOM!")
term.setCursorPos(1,3)
end
function Pass()
repeat
term.setCursorPos(1,2)
term.clearLine()
pas=read("*")
until pas=="password"
term.setCursorPos(1,2)
term.clearLine()
write("Stopped :P/>/>")
term.setCursorPos(1,3)
end
parallel.waitForAny(Count,Pass)
#4
Posted 23 September 2012 - 04:32 PM
Try this. Just change the password to anything you want and change time to the amount of time to allow. At the bottom is an if statement so you can set what happens if time runs out, or doesn't.
Spoiler
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











