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

Countdown program

Started by AmbientSound, 23 September 2012 - 03:02 PM
AmbientSound #1
Posted 23 September 2012 - 05: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!
Matrixmage #2
Posted 23 September 2012 - 05: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.
Jahmaican #3
Posted 23 September 2012 - 05: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)
Anonomit #4
Posted 23 September 2012 - 05: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

password = "bananas"

time = 60

shell.run( "clear" )
x = 1
y = 1

local function countdown()
	while true do
		if time > 9 then
		  write( "0:00:" .. time )
		else
		  write( "0:00:0" .. time )
		end
		term.setCursorPos( x, y )
		time = time - 1
		sleep( 1 )
		x, y = term.getCursorPos()
		term.setCursorPos( 1, 1 )
		term.clearLine()
		if time == 0 then
			outOfTime = true
			break --while true
		end --if time == 0
	end --while true
end --local function countdown()

local function input()
	while true do
		term.setCursorPos( 1, 2 )
		term.clearLine()
		write( "Enter Password: " )
		input = read()
		if input == password then
			outOfTime = false
			break --while true
		else --input == password
			print( "Invalid Password" )
			sleep( 0.5 )
			term.setCursorPos( 1, 3 )
			term.clearLine()
		end --i finput == password
	end --while true
end --local function input


parallel.waitForAny( countdown, input )

shell.run( "clear" )

if outOfTime then
	print( "Awww...!" ) --ran out of time
else --outOfTime
	print( "Yay!" ) --inputted correct password
end --if outOfTime