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

General discussion about LÖVE, Lua, game development, puns, and unicorns.
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

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

Post 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
Last edited by get52 on Fri Sep 13, 2013 6:56 pm, edited 1 time in total.
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

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

Post by micha »

Please be more specific: What have you got and what parts are not working/ what parts can you not figure out?
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

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

Post 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.
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

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

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

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

Post 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
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

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

Post 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)
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
get52
Prole
Posts: 36
Joined: Wed Sep 04, 2013 1:53 pm

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

Post 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.
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests