Jump to content




[Question] How to center something...


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

#1 dextermb

  • New Members
  • 34 posts

Posted 12 December 2012 - 03:12 AM

How would you go about centering something in a computer eg:

  print ("center these words")

How would you center the words in the middle of the computer screen?

#2 ScruffyRules

  • Members
  • 98 posts

Posted 12 December 2012 - 03:23 AM

i think theres something in textutils API that can help you with this!

#3 dextermb

  • New Members
  • 34 posts

Posted 12 December 2012 - 03:27 AM

View PostScruffyRules, on 12 December 2012 - 03:23 AM, said:

i think theres something in textutils API that can help you with this!

I am working on a script/API myself :/
I was just wondering if there is some standard lua code that i can use rather than installing a secondary API.

#4 ScruffyRules

  • Members
  • 98 posts

Posted 12 December 2012 - 03:45 AM

textutils is in LUA.

#5 RyanISan

  • New Members
  • 1 posts
  • LocationAustralia

Posted 12 December 2012 - 05:07 AM

Come on, this is some pretty simple maths ;)

OK, here's a function that figures out what X coordinate to print a string at so that it's centred.

function findCentre(str)
    local centreX = term.getSize()
    centreX = (centreX - #str) / 2
    if centreX > 0 then
        return centreX+1 -- This +1 is not necessary, it's personal preference.
    else
        return 1
    end
end

Can you figure out how to display your string AT that X coordinate? HINT: http://computercraft...rm.setCursorPos

Note: A string with an even number of characters can't really be centred properly...this MAY set off your OCD as it did mine.

#6 FUCKCOMPUTERCRAFT!"£

  • Validating
  • 87 posts
  • LocationBasement

Posted 12 December 2012 - 12:04 PM

Take a look at my some GUI functions I made.

http://pastebin.com/hLLjTfjz

If you don't understand what the functions do just pm me or ask here :)

With my funtions just do..

shell.run("GUI")
cPrint ("center these words")

Note the shell cmd may vary...

#7 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 12 December 2012 - 12:51 PM

local function writeC(string)
	local tX, tY = term.getSize()
	local b = string.len(string) / 2
	local x = (tX / 2) - b
	local y = tY / 2
	term.setCursorPos(x, y)
	write(string)
end

This function prints any string in the center of the screen.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users