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

how to print text based on tables?

Started by Nahuel3d, 07 November 2013 - 03:09 PM
Nahuel3d #1
Posted 07 November 2013 - 04:09 PM
Title: how to print text based on tables?
im making a pacman game for computercraft, i made these tables:

foods = {"o", "0","."}
foodscore = {5, 10, 1}
foodXpos = {5, 15, 25}
foodYpos = {5, 15, 25}
what i want to do is to draw those contents in their respective positions, using print(""), and then make something like collision check, if pacman x and y is equal to any of these x and y positions of these tables, delete their respective variables, this is the code:


x = 10
food = "o"
foodX = 5
foodY = 5
foodE = true
foodcolor = colors.red
y = 10
pac = "<"
hnum = 1
function Update()
term.clear()
term.setCursorPos(x, y)
term.setTextColor(colors.yellow)
print(pac)
term.setCursorPos(foodX, foodY)
term.setTextColor(foodcolor)
print(food)
if x == foodX and y == foodY and foodE == true then
food = ""
term.setCursorPos(x, y)
term.setTextColor(colors.yellow)
print(pac)
foodX = 0
foodY = 0
foodE = false
end
end
while true do
local sEvent, param = os.pullEvent("key")
if sEvent == "key" then
if param == 205 then
  x = x + 1
  pac = "<"
end
if param == 203 then
  x = x - 1
  pac = ">"
end
if param == 200 then
  y = y - 1
  pac = "V"
end
if param == 208 then
  y = y + 1
  pac = "A"
end
Update()
end
end
Edited on 07 November 2013 - 03:12 PM
Yevano #2
Posted 07 November 2013 - 04:47 PM
Since the objects correspond to their respective attributes by table index, just have a 2d table which stores a number from 0 to 4 (0 being pacman, 1-3 being an index from your object tables) for an index [x][y]. If you're not sure what I mean by 2d table, you want something like this:


local map = { }

local function mapSet(x, y, id)
	if not map[x] then map[x] = { } end
	map[x][y] = id
end

local function mapGet(x, y)
	if not map[x] then return nil end
	return map[x][y]
end

There are entirely different ways of doing what you want. You really might want to investigate in a more entity-based system, where each entity is its own object, but the above example should be enough to get you started.
Edited on 07 November 2013 - 03:48 PM
Nahuel3d #3
Posted 07 November 2013 - 06:15 PM
Since the objects correspond to their respective attributes by table index, just have a 2d table which stores a number from 0 to 4 (0 being pacman, 1-3 being an index from your object tables) for an index [x][y]. If you're not sure what I mean by 2d table, you want something like this:


local map = { }

local function mapSet(x, y, id)
	if not map[x] then map[x] = { } end
	map[x][y] = id
end

local function mapGet(x, y)
	if not map[x] then return nil end
	return map[x][y]
end

There are entirely different ways of doing what you want. You really might want to investigate in a more entity-based system, where each entity is its own object, but the above example should be enough to get you started.
thank you, how can i draw the map?
Bomb Bloke #4
Posted 07 November 2013 - 06:35 PM
Let's say you built a simple little map like this:

map = {
{"#","#","#","#","#","#"}, -- This table is stored in map[1]
{"#","#"," "," "," ","#"}, -- This table is stored in map[2]
{"#"," "," ","#"," ","#"}, -- etc
{"#"," "," "," ","#","#"},
{"#"," "," "," "," ","#"},
{"#"," ","#"," "," ","#"},
{"#","#","#","#","#","#"}}

You could then render it like this:

for y=1,#map do  -- Loop an amount of times equal to the number of tables in "map"
  term.setCursorPos(1,y) 
  for x=1,#map[y] do   -- Loop an amount of times equal to the length of the current table in "map"
    term.write(map[y][x])
  end
end

Not an efficient implementation (as far as just "getting the map on the screen" goes), but one that makes it easy to apply different colours, check for walls while moving around the play area, and so on.
Edited on 07 November 2013 - 05:37 PM
Nahuel3d #5
Posted 08 November 2013 - 12:59 PM
Let's say you built a simple little map like this:

map = {
{"#","#","#","#","#","#"}, -- This table is stored in map[1]
{"#","#"," "," "," ","#"}, -- This table is stored in map[2]
{"#"," "," ","#"," ","#"}, -- etc
{"#"," "," "," ","#","#"},
{"#"," "," "," "," ","#"},
{"#"," ","#"," "," ","#"},
{"#","#","#","#","#","#"}}

You could then render it like this:

for y=1,#map do  -- Loop an amount of times equal to the number of tables in "map"
  term.setCursorPos(1,y)
  for x=1,#map[y] do   -- Loop an amount of times equal to the length of the current table in "map"
	term.write(map[y][x])
  end
end

Not an efficient implementation (as far as just "getting the map on the screen" goes), but one that makes it easy to apply different colours, check for walls while moving around the play area, and so on.
thank you!
EDIT: almost forgot, can you send me a little code for collision checking?
Edited on 08 November 2013 - 12:01 PM
Yevano #6
Posted 08 November 2013 - 03:55 PM
EDIT: almost forgot, can you send me a little code for collision checking?

For that, just check if neighboring map cells (e.g., x + 1, y - 1) are walls before you set pacman's new position.
Bomb Bloke #7
Posted 08 November 2013 - 04:31 PM
Eg,

if param == keys.right and map[y][x+1] ~= "#" and x+1 <= #map[y] then
  x = x + 1
  pac = ">"
end

This checks to see if you've tapped the right arrow (check out the keys API, taking advantage of it leads to far more readable code then putting in numbers like 205/203/200/etc), if the move will avoid a wall, and if the move will leave you inside the play area.

When checking whether a vertical movement down would put you out of bounds, you'd be checking just #map instead of #map[y], or when going left/up you'd make sure x/y won't go below 1.

Rather late edit:

There's a rather large problem with the logic behind the above code, in that if you get to the very top or bottom of the table, attempting to go further up/down will crash out.

Let's say you're at the top left (x is 1 and y is 1) and you want to go up again. You check what's in map[y-1][x], which translates to map[0][1].

Because there's no table in map[0], this means map[0][1] is attempting to pull index 1 from a table that doesn't exist. Splat.

As a result, the code has to be drawn out a bit:

if param == keys.up and y > 1 then
  if map[y-1][x] ~= "#"  then  -- This check only gets performed if "map[y-1][x]" exists.
    y = y - 1
    pac = "A"
  end
end
Edited on 08 November 2013 - 10:50 PM