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

Text Adventure Window

Started by Razputin, 09 November 2012 - 12:29 PM
Razputin #1
Posted 09 November 2012 - 01:29 PM
I'm trying to write a simple text adventure, but I can't figure out how to get the window to work.

print ("|+++++++++++++|")
print ("|		  |")
print ("|		  |")
print ("|		  |")
print ("|+++++++++++++|")
I can get it to print this ^ to the screen but what I'm trying to do is put the window code into a function to save time, and then have the 'encounter' code appear inside the window.

function window()

|+++++++++++++|
| |
| |
| |
|+++++++++++++|

end

function encounter()


end


window()
encounter()



|+++++++++++++|
| encounter() |
| |
| >You type here |
|+++++++++++++|

And the second part of my question is how do I prevent the walls of the window from moving like so if you type inside of it?
Any help would be appreciated.
Orwell #2
Posted 09 November 2012 - 03:35 PM
1. For the rectangles (windows) I suggest using basic maths. Here is a quick example:

local function drawWindow(x,y,w,h) -- x, y, width, height
  --[[ do every possible bounding check ]]
  local sx,sy = term.getSize()
  x = math.max(1,x)
  y = math.max(1,y)
  x = math.min(sx,x)
  y = math.min(sy,y)
  w = math.min(x+w,sx) - x
  w = math.max(1,w)
  h = math.min(y+h,sy) - y
  h = math.max(1,h)
  --[[ generate the horizontal line ]]
  local hLine = '|'
  if (w>1) then
	hLine = '|'..string.rep('+',w-2)..'|'
  end
  --[[ generate the vertical line ]]
  local vLine = '|'
  if (w>1) then
	vLine = '|'..string.rep(' ',w-2)..'|'  -- fill the gap with spaces
  end
  --[[ draw the two horizontal lines ]]
  term.setCursorPos(x,y)
  term.write(hLine)
  term.setCursorPos(x,y+h-1)
  term.write(hLine)
  --[[ draw the two vertical lines in one for loop ]]
  for dy=1,h-2 do
	term.setCursorPos(x,y+dy)
	term.write(vLine)
  end
end

drawWindow(10,5,30,8)

Now drawing the text should be quite simple, it's the same principle. Just set the cursor at the right place and write it to the screen. :unsure:/>/> Same for reading. If you really need an example for that, I could make one (or anyone else probably). But I suggest trying it out yourself first. ^_^/>/>

I was gonna start out with just a sample, but I ended up writing a full implementation. This might be way too complicated, but it covers it all. To clarify, string.rep(str,n) creates a string of 'n' times 'str' concatenated. If you have other questions, please ask. :(/>/>

2. You should look at the read() function in the file 'bios.lua' (located in the rom folder in the ComputerCraft mod itself). You can easily modify it to start scrolling on a certain x-value.
Lyqyd #3
Posted 09 November 2012 - 03:57 PM

local function drawWindow(x,y,w,h) -- x, y, width, height
  --[[ do every possible bounding check ]]
  local sx,sy = term.getSize()
  x = math.max(math.min(x,sx), 1)
  y = math.max(math.min(y,sy), 1)
  w = math.max(math.min(x+w,sx) - x, 1)
  h = math.max(math.min(y+h,sy) - y, 1)
  --[[ generate the horizontal line ]]
  local hLine
  if (w>1) then
	hLine = '|'..string.rep('+',w-2)..'|'
  end
  --[[ generate the vertical line ]]
  local vLine
  if (w>1) then
	vLine = '|'..string.rep(' ',w-2)..'|'  -- fill the gap with spaces
  end
  --[[ draw the two horizontal lines ]]
  term.setCursorPos(x,y)
  term.write(hLine)
  term.setCursorPos(x,y+h-1)
  term.write(hLine)
  --[[ draw the two vertical lines in one for loop ]]
  for dy=1,h-2 do
	term.setCursorPos(x,y+dy)
	term.write(vLine)
  end
end

drawWindow(10,5,30,8)

Cleaned this up a little bit, mostly cosmetic changes. This code looks workable if it's valid.