Jump to content




Read a position's character?


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

#1 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

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.

#2 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

Posted 01 January 2013 - 11:01 AM

x, y =term.getCursorPos()


#3 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 01 January 2013 - 11:03 AM

View Postzekesonxx, on 01 January 2013 - 11:01 AM, said:

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.

#4 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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.

#5 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

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...

#6 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 01 January 2013 - 11:06 AM

View Postluanub, on 01 January 2013 - 11:04 AM, said:

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.

#7 zekesonxx

  • Signature Abuser
  • 263 posts
  • LocationWhere you aren't

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().

#8 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 01 January 2013 - 11:41 AM

View Postzekesonxx, on 01 January 2013 - 11:17 AM, said:

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?

#9 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

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


#10 ChunLing

  • Members
  • 2,027 posts

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.

#11 brett122798

  • Members
  • 300 posts
  • LocationIn the TARDIS at an unknown place in time.

Posted 01 January 2013 - 06:28 PM

View PostChunLing, on 01 January 2013 - 01:38 PM, said:

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!

#12 ChunLing

  • Members
  • 2,027 posts

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).





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users