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

Countdown option

Started by Churik, 05 January 2013 - 11:59 PM
Churik #1
Posted 06 January 2013 - 12:59 AM
I'm still new to lua and CC, so I don't know how everything works, yet.
So I was wondering if there was a option to add a countdown for when he sleeps?

Like, I say sleep(60) and then something like print(" The turtle stays for 60 more seconds asleep")
I could not really think of anything that would work.
zekesonxx #2
Posted 06 January 2013 - 01:23 AM

x, y = term.getCursorPos()
for i = 1,60 do
  term.setCursorPos(x, y)
  print(i.." ticks left")
  sleep(1)
end
Churik #3
Posted 06 January 2013 - 02:02 AM

x, y = term.getCursorPos()
for i = 1,60 do
  term.setCursorPos(x, y)
  print(i.." ticks left")
  sleep(1)
end

Thanks!
But could maybe also explain what everything dos?
Don't really know how it works :D/>
ChunLing #4
Posted 06 January 2013 - 03:33 AM
x, y = term.getCursorPos() --save the current cursor position
for i = 1,60 do --loop the following, incrementing i by one each time, starting at 1, till i > 60
  term.setCursorPos(x, y) --put the cursor in the same position each time
  print(i.." ticks left") --print the message (you could also use write() here)
  sleep(1) -- sleep 1 second
end
The key point is that you put the cursor back in the same position before each iteration of print, so that the new message overwrites the old one. Or maybe the key point is looping?
remiX #5
Posted 06 January 2013 - 04:53 AM
Didn't you want a countdown loop?


for i = 60, 1, -1 do -- starts at 60 until 1, decrement 1 each time.
    term.setCursorPos(1, 1)
    term.clearLine() -- clear the line
    print("Time left: " .. i .. " " .. (i == 1 and "second" or "seconds")) -- prints second if i is one, else it will print seconds.
    sleep(1)
end
Churik #6
Posted 06 January 2013 - 05:35 AM
Didn't you want a countdown loop?


for i = 60, 1, -1 do -- starts at 60 until 1, decrement 1 each time.
	term.setCursorPos(1, 1)
	term.clearLine() -- clear the line
	print("Time left: " .. i .. " " .. (i == 1 and "second" or "seconds")) -- prints second if i is one, else it will print seconds.
	sleep(1)
end

Dos not really matter that much :P/> as long as I understand it

And thanks for the explanation guys, helped me a lot.