Page 1 of 1

How do you make a Grid?

Posted: Sun Apr 22, 2012 5:02 am
by NoodleNaught
Before i get yelled at, i would like to say this is a re-post because my previous thread name was written incorrectly. Basically how do you make a grid that allows one object to occupy each grid space? Any help would be greatly appreciated, i don't have extensive Lua experience so sorry for idiot question.

Re: How do you make a Grid?

Posted: Sun Apr 22, 2012 5:17 am
by iemfi
Have you tried the tutorials? https://love2d.org/wiki/Tutorial:Gridlocked_Player seems like exactly what you want.

Re: How do you make a Grid?

Posted: Sun Apr 22, 2012 6:12 am
by NoodleNaught
Yes thank you that is useful. I actually ran across that before but closed the tab by accident, and couldn't find it again. Thank you again.

Re: How do you make a Grid?

Posted: Sun Apr 22, 2012 7:02 am
by NoodleNaught
Alright, so that helped a bit. However when they use numbers in the tutorial to map out the spawning of blocks, could those numbers be used, to represent different terrain types in a game? For instance is it possible to assign a picture and value that effects objects occupying the same space, to each number? Also is there a tutorial on how to spawn multiple player objects that you can switch between?
Sorry if I use improper terms, I'm completely new to Lua and love.

Re: How do you make a Grid?

Posted: Sun Apr 22, 2012 1:57 pm
by tentus
Yep, you could use any number of numbers. You could so use letters with a tiny bit of modification. An example of having more numbers which render differently:

Code: Select all

function love.draw()
    love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
    for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
            elseif map[y][x] == 2 then
                love.graphics.rectangle("fill", x * 32, y * 32, 32, 32)
            end
        end
    end
end
To have multiple players, move the player variables into that new subtable, and add a couple variations. Something about like this:

Code: Select all

function love.load()
  player = {
    {
        grid_x = 32,
        grid_y = 32,
        act_x = 32,
        act_y = 32,
        speed = 10
    },
    {
        grid_x = 64,
        grid_y = 256,
        act_x = 64,
        act_y = 256,
        speed = 10
    },
    {
        grid_x = 256,
        grid_y = 256,
        act_x = 256,
        act_y = 256,[code]
speed = 10
}
}
end
[/code]
We will also need a viable to keep track of which player we're controlling, and may want to move some of the redundant variables out, like so:

Code: Select all

function love.load()
    playermeta = {
        index = 1,
        speed = 10
    }
end
Now we can can control the players by cycling through the index:

Code: Select all

function love.keypressed(key)
    if key == "up" then
        if testMap(0, -1) then
            player[playermeta.index].grid_y = player[playermeta.index].grid_y - 32
        end
    elseif key == "down" then
        if testMap(0, 1) then
            player[playermeta.index].grid_y = player[playermeta.index].grid_y + 32
        end
    elseif key == "left" then
        if testMap(-1, 0) then
            player[playermeta.index].grid_x = player[playermeta.index].grid_x - 32
        end
    elseif key == "right" then
        if testMap(1, 0) then
            player[playermeta.index].grid_x = player[playermeta.index].grid_x + 32
        end
    elseif key == "return" then
        if playermeta.index < #player then
            playermeta.index = playermeta.index + 1
        else
            playermeta.index = 1
        end
    end
end
We'll also need to change the testMap function to use the current player.