#Snake Game Logic

16 messages · Page 1 of 1 (latest)

sturdy tangle
#

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.

gloomy sealBOT
#

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.

undone adder
#

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.

old plank
#

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

sturdy tangle
undone adder
#
MoveSnake()
if collide:
  CreateNewNodeAt(LASTPOS)
else
  LASTPOS = snake.end.position

sturdy tangle
undone adder
#

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