Page 1 of 3

[HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 12:18 pm
by get52
I have no F'ing idea and im stuck just here please help me my snake isnt gridlocked. im being special. Its gridlocked now lol

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 12:26 pm
by micha
Please be more specific: What have you got and what parts are not working/ what parts can you not figure out?

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 12:33 pm
by get52
micha wrote:Please be more specific: What have you got and what parts are not working/ what parts can you not figure out?
how to get the tail to follow the player.

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 12:50 pm
by Plu
Assign each tail a parent that it's following. For the first part this is the head, for the second part of the tail it's the first part, etc.

On every update, move every part of the tail to the exact location of its parent part, and then finally move the head to its new position. Make sure you start at the end of the tail when doing this, and work your way towards the head.

That should get you the effect you are looking for.

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 1:04 pm
by raidho36
If you do it via grid, save where head went for each snake piece. If head went to the left (absolute direction) then save to grid "snake left" or whatever, etc. When you move a tail, you read that direction from a grid, erase the cell, and shift tail coordinates one cell in specified direciton.

Code: Select all

if snake.move then
  if snake.head.direction == "left" then
    map[snake.head.x][snake.head.y] = "snake left" -- record where head went
    snake.head.x = snake.head.x + 1 -- dummy for moving left
    --snake.head.y = snake.head.y
    map[snake.head.x][snake.head.y] = "snake left" -- place head to the next position
  end -- etc.
  local piece = map[snake.tail.x][snake.tail.y]
  if piece == "snake left" then
    map[snake.tail.x][snake.tail.y] = "empty"
    snake.tail.x = snake.tail.x + 1
    --snake.tail.y = snake.tail.y
  end
end
However you actually do it, you need to keep track of segments' directions.

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 1:12 pm
by Nixola
I assign a number to each segment and I increase each by one every tick, then I check if it's higher than the snake's length and remove it if it is.

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 5:07 pm
by get52
Plu wrote:Assign each tail a parent that it's following. For the first part this is the head, for the second part of the tail it's the first part, etc.

On every update, move every part of the tail to the exact location of its parent part, and then finally move the head to its new position. Make sure you start at the end of the tail when doing this, and work your way towards the head.

That should get you the effect you are looking for.
That's genius, also how do i do every tick is it like love.everytick or something like that i cant find it anywhere :o

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 6:35 pm
by Davidobot
get52 wrote:
Plu wrote:Assign each tail a parent that it's following. For the first part this is the head, for the second part of the tail it's the first part, etc.

On every update, move every part of the tail to the exact location of its parent part, and then finally move the head to its new position. Make sure you start at the end of the tail when doing this, and work your way towards the head.

That should get you the effect you are looking for.
That's genius, also how do i do every tick is it like love.everytick or something like that i cant find it anywhere :o
It's called love.update(dt)

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 6:55 pm
by get52
Davidobot wrote:
get52 wrote:
Plu wrote:Assign each tail a parent that it's following. For the first part this is the head, for the second part of the tail it's the first part, etc.

On every update, move every part of the tail to the exact location of its parent part, and then finally move the head to its new position. Make sure you start at the end of the tail when doing this, and work your way towards the head.

That should get you the effect you are looking for.
That's genius, also how do i do every tick is it like love.everytick or something like that i cant find it anywhere :o
It's called love.update(dt)
:oops: god I feel like a dumbass...When i do that it makes it go super fast. also i made the snake gridlocked.

Re: [HOW DO I] make a snake game's tail work?

Posted: Fri Sep 13, 2013 7:03 pm
by Nixola
You should have a 'timer' variable and only trigger a snake update each time that variable is more than a certain value. Example:

Code: Select all

love.load = function()
   timer = 0
   timerLimit = 0.1
   --other code
end

love.update = function(dt)
   timer = timer + dt
   if timer > timerLimit then
      --update the snake, this is what I call 'tick'
   end
   --other code
end

--rest of the code