Seeking Constructive Criticism on a Snake Clone

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
walesmd
Prole
Posts: 22
Joined: Fri Oct 08, 2010 3:11 am
Location: Augusta, GA
Contact:

Re: Seeking Constructive Criticism on a Snake Clone

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Seeking Constructive Criticism on a Snake Clone

Post by bartbes »

Most people use grids for snake, I guess you can do that too.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 11 guests