Page 2 of 2

Re: Seeking Constructive Criticism on a Snake Clone

Posted: Fri Oct 08, 2010 5:58 pm
by walesmd
bartbes wrote:It makes a lot more sense to change your speeds from pixels/update to pixels/second and then multiply by time (dt).
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.

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
I'm trying to think of other ways to go about doing it but nothing good is really coming to mind. I may have to rethink how I'm building the snake, I'm thinking math.floor() the new positions and only set a point there when the results are new x,y coordinates.

Re: Seeking Constructive Criticism on a Snake Clone

Posted: Fri Oct 08, 2010 7:10 pm
by bartbes
Most people use grids for snake, I guess you can do that too.