[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

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

Post by get52 »

Nixola wrote: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
Thank you, you even made me a code snippet. :awesome: -edit OMG YES ITS NOT SUPER FAST I LOVE YOU.
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 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.
How do i make a parent child relationship? (I did it alot in 3D design i just dont know how to do it in love2D :P im a noob lol
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 »

Code: Select all

snake = {}
snake.head = --code to create the head, such as direction, position, etc;
snake.children = {}
snake.children[1] = {} --code to create the first tail square
snake.children[1].parent = snake.head
That's just an example, you can actually do it as you want; just remember that when you assign a variable to an already existing table, you only create a new reference. Example of what happens in Lua:

Code: Select all

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> a = 6
> b = a
> b = 7
> print(a)
6
> print(b)
7
> --this is what happens with strings, booleans and numbers
> t = {field = 6}
> v = t
> v.field = 7
> print(t.field)
7
> print(v.field)
7
> --you see? it's the exact same table, not a copy
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
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 »

Since each part just needs access to its parent, you can just make them into a sort of linked list:

Code: Select all

function part( parent )
return {
  x = 1, y = 2, -- etc, stuff goes here
  parent = parent
}
end

local last = nil
for i = 1, 10 do
  last = part( last )
end
This leaves you with the tip of the tail in the last variable, and you can loop upwards through all the other parts by using the parent property.
So you could do something like this to loop the snake:

Code: Select all

  local copy = last -- or whatever you called the tip of the snake's tail.
  while copy do
    -- handle the part, like drawing it or such
    drawFuncOrWhatever( copy )
    copy = copy.parent
  end
This should work. But I didn't test it, so can't be sure :)
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:Since each part just needs access to its parent, you can just make them into a sort of linked list:

Code: Select all

function part( parent )
return {
  x = 1, y = 2, -- etc, stuff goes here
  parent = parent
}
end

local last = nil
for i = 1, 10 do
  last = part( last )
end
This leaves you with the tip of the tail in the last variable, and you can loop upwards through all the other parts by using the parent property.
So you could do something like this to loop the snake:

Code: Select all

  local copy = last -- or whatever you called the tip of the snake's tail.
  while copy do
    -- handle the part, like drawing it or such
    drawFuncOrWhatever( copy )
    copy = copy.parent
  end
This should work. But I didn't test it, so can't be sure :)
:( How long did it take you to get good at this ive only used love for a week or so and this is really confuseing I dont know what to do :( I'm thinking I should do the snake game after i learn more about love/lua how did you get started? I really whanna make games but im really fucking confused ._. I'm 13 and I dont feel that i can do it. ._.
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 »

I started at around your age with this kind of stuff as well, that's been about 14 years. The best thing you can do is not give up and keep messing about with stuff you like. You'll learn eventually.

It might also help to get some books on programming, although most of them are probably aimed at people a few years older, so maybe it's better to just keep playing around for a few years. You have to make a lot of crap before you can start producing gems :) Best get the crap out of the way as early as possible.
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

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

Post by jjmafiae »

you can always read lua first edition.
User avatar
shatterblast
Party member
Posts: 136
Joined: Tue Dec 11, 2012 9:47 pm
Location: Dallas, Texas, USA

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

Post by shatterblast »

I'm just a hobbyist programmer, and I found this link useful and educating for learning parts of LUA:

http://nova-fusion.com/2012/08/27/lua-f ... rs-part-1/
FireZenk
Prole
Posts: 25
Joined: Tue Apr 09, 2013 4:14 pm
Location: Valencia, Spain
Contact:

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

Post by FireZenk »

You can also use Box2D to make the tail, here an example with box2d:

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 »

FireZenk wrote:You can also use Box2D to make the tail, here an example with box2d:

AwesomeSauce just Awesome sauce thanks for all the help guys lol
Only douchebags tell the admins to delete threads once they get they're answer. :awesome:
Post Reply

Who is online

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