Snake Game Movement

Showcase your libraries, tools and other projects that help your fellow love users.
blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin »

tuupakku wrote:
blorbin wrote:
tuupakku wrote:By incrementing the x and y positions in a fixed amount you can make it "jump" as you want. You can also achieve the delay between jumps by following the first example here: https://love2d.org/wiki/love.update only instead of making the npc think, you're making the snake jump to the next position.
I've run into a small problem along they way... Any suggestions?
SnakeWIP.love
In the function food:update(dt) you're only increasing the width of the head segment, so you're making a bigger segment, not a bigger snake. Instead what you need to do is create a new segments field inside your snake where you'll keep every segment and iterate through them when updating/drawing.
ok, so i should create a table inside the snake table, containing all of the segments. Then, I'm a little confused on where the x/y positions of the new segments should be, or the equation i should do to put it in the right one.
User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku »

blorbin wrote:
tuupakku wrote:
blorbin wrote: I've run into a small problem along they way... Any suggestions?
SnakeWIP.love
In the function food:update(dt) you're only increasing the width of the head segment, so you're making a bigger segment, not a bigger snake. Instead what you need to do is create a new segments field inside your snake where you'll keep every segment and iterate through them when updating/drawing.
ok, so i should create a table inside the snake table, containing all of the segments. Then, I'm a little confused on where the x/y positions of the new segments should be, or the equation i should do to put it in the right one.
When you iterate through them you give each of them the last position of the previous node. You can get the last position of the head by getting the snake.x and snake.y before you update its position inside the love.update function, as for the rest of the node all you have to do is propagate the other positions.
blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin »

tuupakku wrote:
blorbin wrote:
tuupakku wrote: In the function food:update(dt) you're only increasing the width of the head segment, so you're making a bigger segment, not a bigger snake. Instead what you need to do is create a new segments field inside your snake where you'll keep every segment and iterate through them when updating/drawing.
ok, so i should create a table inside the snake table, containing all of the segments. Then, I'm a little confused on where the x/y positions of the new segments should be, or the equation i should do to put it in the right one.
When you iterate through them you give each of them the last position of the previous node. You can get the last position of the head by getting the snake.x and snake.y before you update its position inside the love.update function, as for the rest of the node all you have to do is propagate the other positions.
How? Sorry, if i seem irritating, since i keep coming back and asking more questions, btw.
User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku »

blorbin wrote:
tuupakku wrote:
blorbin wrote: ok, so i should create a table inside the snake table, containing all of the segments. Then, I'm a little confused on where the x/y positions of the new segments should be, or the equation i should do to put it in the right one.
When you iterate through them you give each of them the last position of the previous node. You can get the last position of the head by getting the snake.x and snake.y before you update its position inside the love.update function, as for the rest of the node all you have to do is propagate the other positions.
How? Sorry, if i seem irritating, since i keep coming back and asking more questions, btw.
Don't worry, it's okay. After you have created a table field inside the snake table to hold the segments (as tables consisting of a x and y fields), in the love.update function you should then assign the position of the head to the first node, reminding to do this before you update the head's position, as well as store that node's previous position in an auxiliary variable so you can repeat this procedure for the following node(s) to propagate the previous positions down the tail.

For love.draw all you have to do is iterate through the segments table and put give the draw call the x and y parameters of the segment.

Also you might want to look at some other posters' work, such as this one: viewtopic.php?t=10339, by looking at how other people solved some of these problems you can learn a lot. One of the many reasons open source is great, but I digress.
blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin »

tuupakku wrote:
blorbin wrote:
tuupakku wrote: When you iterate through them you give each of them the last position of the previous node. You can get the last position of the head by getting the snake.x and snake.y before you update its position inside the love.update function, as for the rest of the node all you have to do is propagate the other positions.
How? Sorry, if i seem irritating, since i keep coming back and asking more questions, btw.
Don't worry, it's okay. After you have created a table field inside the snake table to hold the segments (as tables consisting of a x and y fields), in the love.update function you should then assign the position of the head to the first node, reminding to do this before you update the head's position, as well as store that node's previous position in an auxiliary variable so you can repeat this procedure for the following node(s) to propagate the previous positions down the tail.

For love.draw all you have to do is iterate through the segments table and put give the draw call the x and y parameters of the segment.

Also you might want to look at some other posters' work, such as this one: viewtopic.php?t=10339, by looking at how other people solved some of these problems you can learn a lot. One of the many reasons open source is great, but I digress.
Ok i think i know what to do, If not i might put this project down until I know a little more math or know more about the language (I'm in 6th grade). But i have the project on my computer and I've looked at several times, and what he has dont combined with what you telling has given me a good idea. This piece of code mainly:

Code: Select all

lastX = snake.body[#snake.body].x
lastY = snake.body[#snake.body].y
Thanks for the help though!
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests