Skip to content

Instantly share code, notes, and snippets.

Created October 26, 2013 13:43
Show Gist options
  • Save anonymous/7169611 to your computer and use it in GitHub Desktop.
Save anonymous/7169611 to your computer and use it in GitHub Desktop.
function love.load()
player = {
grid_x = 256,
grid_y = 256,
act_x = 256,
act_y = 256,
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)
if up == 1 then
love.graphics.rectangle("line", player.act_x, player.act_y-32, 32, 32)
end
end
function love.keypressed (key)
if key == "up" then
if testMap (0, -1) then
player.grid_y = player.grid_y - 32
else up=1
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment