Jump to content




Boxes


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

#1 grand_mind1

  • Members
  • 207 posts

Posted 03 April 2013 - 03: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

#2 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 03 April 2013 - 03: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

#3 grand_mind1

  • Members
  • 207 posts

Posted 03 April 2013 - 03: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.

#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 03 April 2013 - 04:59 PM

View Postgrand_mind1, on 03 April 2013 - 03:35 PM, said:

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users