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

Need Help with timer

Started by antoniox20, 18 April 2015 - 08:29 PM
antoniox20 #1
Posted 18 April 2015 - 10:29 PM
So I have been working on a 15 second timer for a 5x3 monitor, i dont exactly know how to make the text center on the monitor

s = 15
for i=1, 170 do --15s = 15s
    term.clear()
    term.setCursorPos(1, 1)
    print("Time Left: "..s)
    sleep(1)
    if s == 0 then
		    s = 15
    end
		    s = s-1
end
HPWebcamAble #2
Posted 19 April 2015 - 12:32 AM
You need to calculate where the center is, based on the lenght of the text you want to print

Since you are changing the text often, that means you'll need to recalculate it every time, so a function will be your best bet


local function printCentered(text,place)
  place = place or term
  local w,h = place.getSize()
  local x,y = place.getCursorPos()
  place.setCursorPos( math.floor(w/2)-math.ceil(#text/2) , y ) --# Put the cursor in the right place to print the text in the center
  place.write(text)
  place.setCursorPos(x,y+1) --# Goes to the next line, like print()
end

If you just call it like ' printCentered("hi") ', that will do it on the computer's screen

Call it like ' printCentered("hi",m) ' to print it on a monitor

Where 'm' is this:

m = peripheral.wrap("monitor_name")
Edited on 18 April 2015 - 10:32 PM