Jump to content




Countdown program


  • You cannot reply to this topic
3 replies to this topic

#1 AmbientSound

  • New Members
  • 2 posts

Posted 23 September 2012 - 04:02 PM

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!

#2 Matrixmage

  • Members
  • 116 posts
  • LocationAt my Computer coding for the Computer in my Computer

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.

#3 Jahmaican

  • Members
  • 26 posts
  • LocationPoland

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 Anonomit

  • Members
  • 67 posts

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