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

Snake Game Movement

Post by blorbin » Fri Feb 12, 2016 11:39 pm

I am fairly new to LÖVE2D though i have experience with lua. I currently want to make a snake game, but I'm having trouble with making the movement of it. The best I've got is a white square that can move, and cant change opposite directions. Though, if you press up/down then press the opposite direction button, you will be able to. any idea's of how to make the movement like the snake, where it jumps a certain amount?

User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku » Sat Feb 13, 2016 12:02 am

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.

blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin » Sat Feb 13, 2016 4:30 am

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.
Please explain more

User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku » Sat Feb 13, 2016 1:56 pm

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.
Please explain more
I have created a simple LÖVE project and commented it (perhaps a bit too much), maybe that will give you a better understanding of how it's supposed to work? Here it is:
Snake Movement.love
(818 Bytes) Downloaded 85 times
The gist of it is that instead of updating the x and y coordinates every frame, you want to wait for some time before the next step. Also the snake should move at a constant rate every step (the same amount as a side of one of its nodes) top achieve the grid-like movement common to snake games.

If you still have any questions feel free to ask.

blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin » Sat Feb 13, 2016 4:12 pm

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.
Please explain more
I have created a simple LÖVE project and commented it (perhaps a bit too much), maybe that will give you a better understanding of how it's supposed to work? Here it is:
Snake Movement.love
The gist of it is that instead of updating the x and y coordinates every frame, you want to wait for some time before the next step. Also the snake should move at a constant rate every step (the same amount as a side of one of its nodes) top achieve the grid-like movement common to snake games.

If you still have any questions feel free to ask.
Great! Thanks so much! The only thing now is (ex.) if your going right, and press up then left quickly, you will still be able to go the opposite direction). Otherwise that helped a lot. I'm guessing the way to fix that is to create an if statement for how far its traveled, some how?

User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku » Sat Feb 13, 2016 4:58 pm

blorbin wrote:
tuupakku wrote:
blorbin wrote: Please explain more
I have created a simple LÖVE project and commented it (perhaps a bit too much), maybe that will give you a better understanding of how it's supposed to work? Here it is:
Snake Movement.love
(844 Bytes) Downloaded 72 times
The gist of it is that instead of updating the x and y coordinates every frame, you want to wait for some time before the next step. Also the snake should move at a constant rate every step (the same amount as a side of one of its nodes) top achieve the grid-like movement common to snake games.

If you still have any questions feel free to ask.
Great! Thanks so much! The only thing now is (ex.) if your going right, and press up then left quickly, you will still be able to go the opposite direction). Otherwise that helped a lot. I'm guessing the way to fix that is to create an if statement for how far its traveled, some how?
Oh damn, you're right. I fixed it by adding the next direction to the snake table and only updating it on the next step.

Here's the fixed version:
Snake Movement.love
(844 Bytes) Downloaded 72 times

blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin » Sat Feb 13, 2016 5:11 pm

tuupakku wrote:
blorbin wrote:
tuupakku wrote: I have created a simple LÖVE project and commented it (perhaps a bit too much), maybe that will give you a better understanding of how it's supposed to work? Here it is:
Snake Movement.love
The gist of it is that instead of updating the x and y coordinates every frame, you want to wait for some time before the next step. Also the snake should move at a constant rate every step (the same amount as a side of one of its nodes) top achieve the grid-like movement common to snake games.

If you still have any questions feel free to ask.
Great! Thanks so much! The only thing now is (ex.) if your going right, and press up then left quickly, you will still be able to go the opposite direction). Otherwise that helped a lot. I'm guessing the way to fix that is to create an if statement for how far its traveled, some how?
Oh damn, you're right. I fixed it by adding the next direction to the snake table and only updating it on the next step.

Here's the fixed version:
Snake Movement.love
Yaay! Thanks! I've seen a tutorial for making the points randomly spawn, and I've got and idea on how the points would make it grow. Thank you!

User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku » Sat Feb 13, 2016 5:16 pm

No problem man, best of luck with your game.

blorbin
Prole
Posts: 11
Joined: Fri Feb 12, 2016 11:31 pm

Re: Snake Game Movement

Post by blorbin » Sun Feb 14, 2016 8:48 pm

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
(21.91 KiB) Downloaded 64 times

User avatar
tuupakku
Prole
Posts: 21
Joined: Fri Feb 12, 2016 12:12 pm

Re: Snake Game Movement

Post by tuupakku » Sun Feb 14, 2016 11:12 pm

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.

Post Reply

Who is online

Users browsing this forum: slime and 4 guests