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

Read a position's character?

Started by brett122798, 01 January 2013 - 09:55 AM
brett122798 #1
Posted 01 January 2013 - 10:55 AM
Hey guys, I am attempting to create a 4 Player Pac-man game and one of the things I'll need to know, is how to read a position's character so I know if there is a wall in front of me or not.
zekesonxx #2
Posted 01 January 2013 - 11:01 AM

x, y =term.getCursorPos()
brett122798 #3
Posted 01 January 2013 - 11:03 AM

x, y =term.getCursorPos()
No, I want to know like how I can read a character of a position I name, not the position of the cursor.
Luanub #4
Posted 01 January 2013 - 11:04 AM
Yeah there is no way to read what is printed on the screen. You will probably want to record what you have placed on the screen into a table then check against the table.
zekesonxx #5
Posted 01 January 2013 - 11:05 AM
You can't.

Make a table with the x and y values of all the walls. Then do a for loop through the table, checking for matches.

EDIT: Damn ninja post…
brett122798 #6
Posted 01 January 2013 - 11:06 AM
Yeah there is no way to read what is printed on the screen. You will probably want to record what you have placed on the screen into a table then check against the table.
Well, I'm completely lost now.
zekesonxx #7
Posted 01 January 2013 - 11:17 AM
Make a giant table. Each value is "1,4", "2,6", that kinda thing.
Look up how to make a for loop in Lua, then do that on the table, comparing it each time with term.getCursorPos().
brett122798 #8
Posted 01 January 2013 - 11:41 AM
Make a giant table. Each value is "1,4", "2,6", that kinda thing.
Look up how to make a for loop in Lua, then do that on the table, comparing it each time with term.getCursorPos().
Could you give me a small scale example of what you're saying?
Luanub #9
Posted 01 January 2013 - 11:46 AM
This should get you thinking. You will want to do something a long these lines..


local tWalls = {"1,2"}

x,y = term.getCursorPos()

currentPos = (x..","..y)

for x=1,#tWalls do
  if currentPos == tWalls[x] then
  --there is a wall there
  end
end
ChunLing #10
Posted 01 January 2013 - 01:38 PM
Er…do it the other way. Make "x1y2" a key, and have a value corresponding to what is there (wall, ghost, food, empty). That will speed up the collision searches a lot.
local tWalls = {x1y1 = "w",x1y2 = "w",x1y3 = "w",x1y4 = "w",x1y5 = "w",x1y6 = "w",
    x2y1 = "w",x2y2 = "g",x2y3 = "f",x2y4 = " ",x2y5 = "e",x2y6 = "w",
    x3y1 = "w",x3y2 = "w",x3y3 = "w",x3y4 = "w",x3y5 = "w",x3y6 = "w",
}-- "w" is wall, "g" is ghost, "f" is food, " " is space, "e" is pac-man...move fast

x,y = term.getCursorPos()

currentPos = "x"..x.."y"..y

if tWalls[currentPos] == "w" then
    --there is a wall there
elseif tWalls[currentPos] == "g" then
    --there is a ghost there
elseif tWalls[currentPos] == "f" then
    --there is food there
elseif tWalls[currentPos] == " " then
    --there is nothing there
elseif tWalls[currentPos] == "e" then
    --there you are
end
You could also do a two dimensional table, a table rows that would each be a table of positions on that row (or columns instead of rows, just be consistent)
local tWalls = {{"w","w","w","w","w","w",},
    {"w","g","f"," ","e","w",},
    {"w","w","w","w","w","w",},
}-- "w" is wall, "g" is ghost, "f" is food, " " is space, "e" is pac-man...move fast

x,y = term.getCursorPos()

if tWalls[y][x] == "w" then
    --there is a wall there
elseif tWalls[y][x] == "g" then
    --there is a ghost there
elseif tWalls[y][x] == "f" then
    --there is food there
elseif tWalls[y][x] == " " then
    --there is nothing there
elseif tWalls[y][x] == "e" then
    --there you are
end
I think this looks better, and you don't have to mess with the concatenation. I reversed the x and y because…well, just look at it.
brett122798 #11
Posted 01 January 2013 - 06:28 PM
Er…do it the other way. Make "x1y2" a key, and have a value corresponding to what is there (wall, ghost, food, empty). That will speed up the collision searches a lot.
local tWalls = {x1y1 = "w",x1y2 = "w",x1y3 = "w",x1y4 = "w",x1y5 = "w",x1y6 = "w",
	x2y1 = "w",x2y2 = "g",x2y3 = "f",x2y4 = " ",x2y5 = "e",x2y6 = "w",
	x3y1 = "w",x3y2 = "w",x3y3 = "w",x3y4 = "w",x3y5 = "w",x3y6 = "w",
}-- "w" is wall, "g" is ghost, "f" is food, " " is space, "e" is pac-man...move fast

x,y = term.getCursorPos()

currentPos = "x"..x.."y"..y

if tWalls[currentPos] == "w" then
	--there is a wall there
elseif tWalls[currentPos] == "g" then
	--there is a ghost there
elseif tWalls[currentPos] == "f" then
	--there is food there
elseif tWalls[currentPos] == " " then
	--there is nothing there
elseif tWalls[currentPos] == "e" then
	--there you are
end
You could also do a two dimensional table, a table rows that would each be a table of positions on that row (or columns instead of rows, just be consistent)
local tWalls = {{"w","w","w","w","w","w",},
	{"w","g","f"," ","e","w",},
	{"w","w","w","w","w","w",},
}-- "w" is wall, "g" is ghost, "f" is food, " " is space, "e" is pac-man...move fast

x,y = term.getCursorPos()

if tWalls[y][x] == "w" then
	--there is a wall there
elseif tWalls[y][x] == "g" then
	--there is a ghost there
elseif tWalls[y][x] == "f" then
	--there is food there
elseif tWalls[y][x] == " " then
	--there is nothing there
elseif tWalls[y][x] == "e" then
	--there you are
end
I think this looks better, and you don't have to mess with the concatenation. I reversed the x and y because…well, just look at it.
Very good! I now have a great idea on how to do things! Thanks!
ChunLing #12
Posted 02 January 2013 - 02:28 AM
One thing to watch out for, make sure that your table covers all the possible returns for x,y.

For a robust game, you don't even want to use the cursor return at all. You have a separate table to track all the active elements (mobs, so to speak) in your game, and you look up the position in that table.

When you move something (say, pac-man), you get the current position from the table, use some maths to look up the position that pac-man is moving to, and if the movement is possible, then delete pac-man from the screen table at the old coordinates and put him at the new coordinates (and update his coordinates in the mob table).