Page 2 of 3

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

Posted: Fri Sep 13, 2013 8:54 pm
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.

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

Posted: Fri Sep 13, 2013 9:01 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.
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

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

Posted: Fri Sep 13, 2013 9:07 pm
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

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

Posted: Sun Sep 15, 2013 11:50 am
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 :)

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

Posted: Sun Sep 15, 2013 5:26 pm
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. ._.

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

Posted: Sun Sep 15, 2013 6:53 pm
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.

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

Posted: Tue Sep 17, 2013 5:35 am
by jjmafiae
you can always read lua first edition.

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

Posted: Tue Sep 17, 2013 8:23 am
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/

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

Posted: Tue Sep 17, 2013 10:10 am
by FireZenk
You can also use Box2D to make the tail, here an example with box2d:


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

Posted: Thu Sep 19, 2013 5:47 pm
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