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

Boxes

Started by grand_mind1, 03 April 2013 - 02:15 PM
grand_mind1 #1
Posted 03 April 2013 - 04:15 PM
I am trying to make some buttons for my games and menus. I already know how to do buttons that are just plain out text but I wanted to know how to make some that have like colored boxes with text in them. When trying to figure out how to do this, this is all I came up with:

term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.green)
write("	    ")
term.setCursorPos(1,2)
write("	    ")
term.setCursorPos(1,3)
write(" Start! ")
term.setCursorPos(1,4)
write("	    ")
term.setCursorPos(1,5)
write("	    ")
Of course I could probably use a for loop for this but it's just a concept. However, I feel like this concept is somewhat bad. I don't know if there is a better way to make boxes instead of writing blank spaces, but that definitely seems odd to me. I honestly don't know if there is a better way to do this or if how I'm doing it is the only way to do it. If someone knows a better way please tell me.
Help is appreciated!
Thanks! :D/>
PixelToast #2
Posted 03 April 2013 - 04:24 PM
theres not really a better way
though if you want to have a variable size box you use:

string.rep(" ",count)
to make more or less spaces

also using GUI functions / for loops would dramatically reduce the number of lines
grand_mind1 #3
Posted 03 April 2013 - 04:35 PM
I don't really understand what

string.rep(" ",count)
is supposed to do. I've tried experimenting with it by changing count to an actual number or putting something in the string but it always prints nothing.
Bubba #4
Posted 03 April 2013 - 05:59 PM
I don't really understand what

string.rep(" ",count)
is supposed to do. I've tried experimenting with it by changing count to an actual number or putting something in the string but it always prints nothing.

string.rep will essentially just concatanate the given character(s) together the specified number of times. For example:

local str = string.rep("x", 5) --5 is the number of times the given character(s) should repeat
print(str == "xxxxx") --Outputs true because str now contains "xxxxx" and they are equal

The reason you probably aren't seeing anything is because you are not changing the background color before you try to write it to the screen.

Example of how it should work:

local function box(x1, y1, width, height)
  term.setBackgroundColor(colors.blue)
  term.clear()
  term.setBackgroundColor(colors.yellow)
  for y=y1, y1+height-1,2 do --Skip counts so you can see that it is only writing spaces and not just clearing an entire area.
	term.setCursorPos(x1, y)
	term.write(string.rep(" x", width))
  end
end

box(3,2, 15, 10)

The above code will make this:
Spoiler