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

Monitor for my xp turtle

Started by Emil, 05 May 2013 - 05:56 AM
Emil #1
Posted 05 May 2013 - 07:56 AM
Hello, I have made an xp farm where I have a turtle that picks up xp and enchants books for me and i'm trying to hook up a monitor to it. The problem is that I don't know how to center the text and remove the .0 after the level from the monitor, here is the code I have right now.


m = peripheral.wrap("right")
monitor = peripheral.wrap("top")
local o = nil
m.setAutoCollect(true)
while true do
if m.getLevels() > 29 then
  turtle.select(1)
  turtle.suck()
  turtle.drop(turtle.getItemCount(1)-1)
  m.enchant(30)
  turtle.drop()
else
  o = m.getLevels()
  monitor.setCursorPos(1.5, 1.5)
  monitor.setTextScale(2.5)
  monitor.write(o)
  sleep(10)
end
end

I searched the forum for help before posting but I didn't get the problem solved since I've never coded before.
Lyqyd #2
Posted 05 May 2013 - 12:21 PM
Split into new topic.
diegodan1893 #3
Posted 05 May 2013 - 04:24 PM
To center the text in the monitor, add this at the beginning of your code:


function centerPrint(sText)
   local mon = peripheral.wrap("right")
   term.redirect(mon)
   local x, y = term.getCursorPos()
   local w, h = term.getSize()
   term.setCursorPos(1 + ((w - sText:len()) / 2), y)
   print(sText, textColor, backgroundColor)

   term.restore()
end

Then, if you want to write something in the monitor just to centerPrint("your text") and it will print your text centered on the monitor on right side.
Engineer #4
Posted 05 May 2013 - 09:20 PM
This is an simpel solution, there is probably something better, but this will work for now.


local level = tonumber(tostring(m.getLevels()):sub(1, tostring(m.getLevels()):find('%.') - 1))