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
Moving the Grid
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- 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 your code or a .love file
-
- Prole
- Posts: 30
- Joined: Tue Feb 12, 2013 7:45 pm
Re: Moving the Grid
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 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
-
- Prole
- Posts: 17
- Joined: Mon Jul 30, 2012 9:15 pm
Re: Moving the Grid
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
Re: Moving the Grid
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
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
- Ranguna259
- Party member
- Posts: 911
- Joined: Tue Jun 18, 2013 10:58 pm
- Location: I'm right next to you
Re: Moving the Grid
On line 32 replace
with:
If you don't know why that happened at first glance then look again, it's really self explanatory ![Wink ;)](./images/smilies/ms-wink.png)
Code: Select all
love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
Code: Select all
love.graphics.rectangle("line", x * 32-32, y * 32-32, 32, 32)
![Wink ;)](./images/smilies/ms-wink.png)
- luislasonbra
- Citizen
- Posts: 60
- Joined: Sun Jun 24, 2012 1:57 pm
Re: Moving the Grid
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.
I correct
but if you want the speed at which you move the player to change
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 1 guest