创建背景框和可移动方块
function love.load()
player = {grid_x = 256,
grid_y = 256,
act_x = 200,
act_y = 200,
speed = 16 }
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)*dt
--player.act_x = player.act_x - (player.act_x - player.grid_x)*dt
player.act_y = player.act_y - (player.act_y - player.grid_y)*dt*player.speed
player.act_x = player.act_x - (player.act_x - player.grid_x)*dt*player.speed
end
function love.draw()
for y = 1, #map do
for x = 1, #map[1] do
if map[y][x] == 1 then
love.graphics.rectangle("line", x * 32, y * 32, 32, 32)
end
end
end
love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
end
function love.keypressed(key)
if key == "up" then
if testMap(0,-1) then
player.grid_y = player.grid_y - 32
end
--player.grid_y = player.grid_y - 32
elseif key == "down" then
if testMap(0,1) then
player.grid_y = player.grid_y + 32
end
--player.grid_y = player.grid_y + 32
elseif key == "left" then
if testMap(-1,0) then
player.grid_x = player.grid_x - 32
end
--player.grid_x = player.grid_x - 32
elseif key == "right" then
if testMap(1,0) then
player.grid_x = player.grid_x + 32
end
--player.grid_x = player.grid_x + 32
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