Moving the Grid

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
FatalMarshmallow
Prole
Posts: 17
Joined: Mon Jul 30, 2012 9:15 pm

Moving the Grid

Post by FatalMarshmallow »

I've searched both forums and the wiki for an answer, but none seem to be what I'm searching for.
At the bottom of the Gridlocked Player tutorial, you'll find this:
"The map starts off at 32,32 when it would make more sense for it to start at 0,0. A little bit of arithmetic could fix this pretty easily, but it does make the code a bit more complex and hard to follow."
However, I have no idea how this would be done, as I haven't been using love for long.
Thanks for any help
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Moving the Grid

Post by Ranguna259 »

Post your code or a .love file
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
SeducingOrange
Prole
Posts: 30
Joined: Tue Feb 12, 2013 7:45 pm

Re: Moving the Grid

Post by SeducingOrange »

FatalMarshmallow wrote:I've searched both forums and the wiki for an answer, but none seem to be what I'm searching for.
At the bottom of the Gridlocked Player tutorial, you'll find this:
"The map starts off at 32,32 when it would make more sense for it to start at 0,0. A little bit of arithmetic could fix this pretty easily, but it does make the code a bit more complex and hard to follow."
However, I have no idea how this would be done, as I haven't been using love for long.
Thanks for any help
It does make sense for it to start at 32,32 because you are adding the player width and height to 0,0. If you were to start at 0,0 the player would be off screen.
FatalMarshmallow
Prole
Posts: 17
Joined: Mon Jul 30, 2012 9:15 pm

Re: Moving the Grid

Post by FatalMarshmallow »

Code: Select all

function love.load()
    player = {
        grid_x = 256,
        grid_y = 256,
        act_x = 200,
        act_y = 200,
        speed = 10
    }
    map = {
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
        { 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1 },
        { 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1 },
        { 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
        { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
    }
end

function love.update(dt)
    player.act_y = player.act_y - ((player.act_y - player.grid_y) * player.speed * dt)
    player.act_x = player.act_x - ((player.act_x - player.grid_x) * player.speed * dt)
end

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

function love.keypressed(key)
    if key == "up" then
        if testMap(0, -1) then
            player.grid_y = player.grid_y - 32
        end
    elseif key == "down" then
        if testMap(0, 1) then
            player.grid_y = player.grid_y + 32
        end
    elseif key == "left" then
        if testMap(-1, 0) then
            player.grid_x = player.grid_x - 32
        end
    elseif key == "right" then
        if testMap(1, 0) then
            player.grid_x = player.grid_x + 32
        end
    end
end

function testMap(x, y)
    if map[(player.grid_y / 32) + y][(player.grid_x / 32) + x] == 1 then
        return false
    end
    return true
end
User avatar
Nuthen224
Citizen
Posts: 50
Joined: Sun Jul 28, 2013 9:40 pm

Re: Moving the Grid

Post by Nuthen224 »

I think under love.draw you'd want to change:

love.graphics.rectangle("line", x * 32, y * 32, 32, 32)

to:

love.graphics.rectangle("line", x * 32 - 32, y * 32 - 32, 32, 32)

because when x = 1, that means the tile begins at 32, subtracting 32 causes it to start at 0. I think that's the solution you're looking for
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Moving the Grid

Post by Ranguna259 »

On line 32 replace

Code: Select all

love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
with:

Code: Select all

love.graphics.rectangle("line", x * 32-32, y * 32-32, 32, 32)
If you don't know why that happened at first glance then look again, it's really self explanatory ;)
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
luislasonbra
Citizen
Posts: 60
Joined: Sun Jun 24, 2012 1:57 pm

Re: Moving the Grid

Post by luislasonbra »

FatalMarshmallow wrote:I've searched both forums and the wiki for an answer, but none seem to be what I'm searching for.
At the bottom of the Gridlocked Player tutorial, you'll find this:
"The map starts off at 32,32 when it would make more sense for it to start at 0,0. A little bit of arithmetic could fix this pretty easily, but it does make the code a bit more complex and hard to follow."
However, I have no idea how this would be done, as I haven't been using love for long.
Thanks for any help

here's what you want to do.
Test Move 32 Space.love
(739 Bytes) Downloaded 197 times
I correct
but if you want the speed at which you move the player to change
Test Move 32 Space.love
(820 Bytes) Downloaded 204 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest