how do the snake tail segments work?

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.
Post Reply
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

how do the snake tail segments work?

Post by get52 »

I''m trying to figure it out and its not happening [sorry for posting again its just a quited on my last snake game] So how do i make the snake tails segments follow the tail? I tried looking at the sample veethree gave me on my last thread but i couldnt figure it out.
Attachments
Snake game.love
Not shour if this will help but people always ask for a .love so eh
(365 Bytes) Downloaded 221 times
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

Re: how do the snake tail segments work?

Post by get52 »

I am completely clueless on how
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: how do the snake tail segments work?

Post by Inny »

There's a lot of ways to do it, but the most common way is a simple list. So, to give you an idea of what that means, have a look at this little snippet:

Code: Select all

function snake:addSegment()
  local old_tail = self.body[#self.body]
  local new_tail = { x=old_tail.x, y=old_tail.y, idle=true }    
  self.body[#self.body+1] = new_tail
end
So, the list in "body" is every segment that's part of the snake. New segments are added where the tail is. Each segment holds a value "idle" that means the tail isn't moving yet, because the player just ate a number.

Now, when you update the snake to move it along, you bubble the movement direction to the next segments, like so:

Code: Select all

-- x and y contains the next location that the player wants to move the head.
function snake:updateBody(x, y)
  local next_x, next_y = x, y
  for _, segment in ipairs(self.body) do
    x, y = segment.x, segment.y
    segment.x = next_x
    segment.y = next_y
    next_x, next_y = x, y
    if segment.idle then
      segment.idle = false
      break
    end
  end
end
Since the last few segments are idle, they'll 1 by 1 become mobile, while the one behind remains idle until the next turn. Collision detection is just checking the head against the rest of the body for an overlap. Drawing is just going to each segment and drawing it. Very simple.

To get smooth sliding like in that other thread, that's a bit more complex and veethree opted to slowly-draw the segments to give the illusion that it's sliding.
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

Re: how do the snake tail segments work?

Post by get52 »

Inny wrote:There's a lot of ways to do it, but the most common way is a simple list. So, to give you an idea of what that means, have a look at this little snippet:

Code: Select all

function snake:addSegment()
  local old_tail = self.body[#self.body]
  local new_tail = { x=old_tail.x, y=old_tail.y, idle=true }    
  self.body[#self.body+1] = new_tail
end
So, the list in "body" is every segment that's part of the snake. New segments are added where the tail is. Each segment holds a value "idle" that means the tail isn't moving yet, because the player just ate a number.

Now, when you update the snake to move it along, you bubble the movement direction to the next segments, like so:

Code: Select all

-- x and y contains the next location that the player wants to move the head.
function snake:updateBody(x, y)
  local next_x, next_y = x, y
  for _, segment in ipairs(self.body) do
    x, y = segment.x, segment.y
    segment.x = next_x
    segment.y = next_y
    next_x, next_y = x, y
    if segment.idle then
      segment.idle = false
      break
    end
  end
end
Since the last few segments are idle, they'll 1 by 1 become mobile, while the one behind remains idle until the next turn. Collision detection is just checking the head against the rest of the body for an overlap. Drawing is just going to each segment and drawing it. Very simple.

To get smooth sliding like in that other thread, that's a bit more complex and veethree opted to slowly-draw the segments to give the illusion that it's sliding.
Thank you, but i'm still kinda confused ... :P forgive me I'm 13 and just started this stuff...
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: how do the snake tail segments work?

Post by veethree »

Here's an example, Note that this is probably not the best method to do this, But it does work. There's plenty of comments that should explain the code, But if you don't understand something, Just let me know, I'll do my best to try and explain it.
Attachments
Simple_snake.love
(1.08 KiB) Downloaded 209 times
User avatar
MPQC
Citizen
Posts: 65
Joined: Fri Jun 28, 2013 2:45 pm

Re: how do the snake tail segments work?

Post by MPQC »

I always thought the easiest way would be as follows (no real code). This isn't using your code as well, as I haven't looked at it.

Table contains a grid (2d array? Whatever you want)
Have a variable which contains how long the tail is, as an integer
Whenever the snake moves to a new point (ie up one block), set that grid number (ie table[x][y]) to how long the tail is
If the snake eats a block (could be stored as -1 or something in the grid), then add 1 to how long its tail is, and all existing tails (loop through grid, if >0, then add 1)
Every "tick" (snake moves), decrease all squares by 1 if it's >0
If the square is at 0, don't draw the snake, if it's >0 then draw the snake there

Only downside would be looping through the entire grid to draw the snake, but with decently small maps (50x50? Random number) that wouldn't really be an issue. There would also be ways to optimize it (ie FIFO to hold locations of the snake). It also makes it way easier on the brain to do it this way.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 4 guests