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.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.blorbin wrote:I've run into a small problem along they way... Any suggestions?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.
Snake Game Movement
Re: Snake Game Movement
Re: Snake Game Movement
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 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.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.blorbin wrote: I've run into a small problem along they way... Any suggestions?
Re: Snake Game Movement
How? Sorry, if i seem irritating, since i keep coming back and asking more questions, btw.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.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.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.
Re: Snake Game Movement
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.blorbin wrote:How? Sorry, if i seem irritating, since i keep coming back and asking more questions, btw.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.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.
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.
Re: Snake Game Movement
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:tuupakku wrote: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.blorbin wrote:How? Sorry, if i seem irritating, since i keep coming back and asking more questions, btw.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.
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.
Code: Select all
lastX = snake.body[#snake.body].x
lastY = snake.body[#snake.body].y
Who is online
Users browsing this forum: GetAsync and 3 guests