Actually, I've run across a "bug" in doing things this way. In multiplying by dt the chances of x, y coordinate being exactly the same at any two points in time is extremely rare. My collision detection with the snake itself never really occurs.bartbes wrote:It makes a lot more sense to change your speeds from pixels/update to pixels/second and then multiply by time (dt).
Code: Select all
function love.update(dt)
if game.running then
-- Figure out the next position for the snake's head
player.x = player.x + directions[player.dir].x * player.speed * dt
player.y = player.y + directions[player.dir].y * player.speed * dt
-- Is the player running into a wall?
if player.x <= 0 or player.x >= game.width or player.y <= 0 or player.y >= game.height then
game.running = false
end
-- Is the player running into itself?
for _, pos in pairs(player.points) do
if pos.x == player.x and pos.y == player.y then
game.running = false
end
end
table.insert(player.points, 1, {x = player.x, y = player.y})
-- Keep the snake no larger than the max_length
if #player.points >= player.max_length then
table.remove(player.points)
end
end
end