Although i have had ' some ' programming experience I am a complete noob when it comes to game programming.
I have followed this tutorial https://www.love2d.org/wiki/Tutorial:Gridlocked_Player.
Everything works fine.
The problem I have is that i want to allow the player to move whilst holding down a key rather than having to keep pressing a key, so i looked at this tutorial https://www.love2d.org/wiki/Tutorial:Hamster_Ball
and decided to try and adapt the Keypress routine.
The code works within reason apart from
Player is moving to fast now
when player gets near the map edge it seems to be too slow
Also if you hold down both up and left or up and right ( and the same for the down key ) the player tends too ' shoot ' through the inner map walls.
I know i haven't made the map any larger but I don't understand why when holding the 2 keys together the player ' shoots ' through the inner walls
I know the speed thing will be to do with it being in the update but i cant figure out how to slow it down enough.
Hope you can point me in the right direction.
Here is the full code with my adaption
Code: Select all
function love.load()
player = {
grid_x = 256,
grid_y = 256,
act_x = 200,
act_y = 200,
speed = 5
}
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) -- dt stands for delta time.
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)
if love.keyboard.isDown("up") then
if testMap(0, -1) then
player.grid_y = player.grid_y - 32
end
elseif love.keyboard.isDown("down") then
if testMap(0, 1) then
player.grid_y = player.grid_y + 32
end
elseif love.keyboard.isDown("left") then
if testMap(-1, 0) then
player.grid_x = player.grid_x - 32
end
elseif love.keyboard.isDown("right") then
if testMap(1, 0) then
player.grid_x = player.grid_x + 32
end
end
end
function love.draw()
love.graphics.rectangle("fill", player.act_x, player.act_y, 32, 32)
-- draw map
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