Hey guys, I am currently working on a simple ECS engine to make a game in for a college project of mine. I have gotten a lot of things working like the basic player movement, the fruit, collisions but absolutely cannot figure out how to tackle the snake growing. It would be great if guys could let me know how to tackle this problem with my systems.
#Snake Game Logic
16 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
when i did that i kept the previous position of the last section of the tail every frame. and if i got a fruit i used that position to know where to place the new part of the tail.
you can store the position of the snake as a list of points on a grid. Each frame you can move the snake by adding a point to the front of the list and popping a point from the back (sort of like a queue). To make the snake grow, all you have to do is not pop the point in the back if the snake ate something.
this also makes collisions really easy to detect because you just have to see if the same point appears twice in the list
That makes sense, but I'll have to keep track of the last position of every single 'node' because I'll have to update the previous node's current position to be the next node's last position. I just don't know how to do this in practice.
You just need to store the last nodes position. When the snake collides with a fruit you get the last node position, create a node there, and set the last node position to be the position of the last node.
MoveSnake()
if collide:
CreateNewNodeAt(LASTPOS)
else
LASTPOS = snake.end.position
But isn't this more like creating new nodes for the snake? I am currently having trouble keeping any more than 1 node connected to the snake and have the proper behavior to follow the existing snake around.
ah. i thought you meant specifically with the growing part, not the movement
ah. @old plank example seems good
or just store all nodes in a list with their pos. and moving it you just move the head (first element) in the direction of the movement, and then recursively move the body (the other elements) to the prior's position.
MovebodyRecursive(parentNode, childNode):
MovebodyRecursive(childNode, childNode.prev)
childNode.position = parentNode.position
Move(snakeLinkedListHead):
MoveBodyRecursive(snakeLinkedListHead, snakeLinkedListHead.prev)
snakeLinkedListHead.position = newPos
just an example. not the best way
ofc you have to end the recursion and also update the last one in the list... i just dont wanna bother
gn