How can i make a snake following my player without any lag, when moving on a grid?

Hi everyone !!! Hope you’re fine and doing good! I might need some help…

How do I…

I’d like to make a Snake that follows my Player,being behind him with only 1px behind and grows up. On Discord, got recommended to ask here^^

What is the expected result

If it works, it would work that way: Game is on a 320x320 grid, of 10x10 tiles.
You control a player, that collect diamonds. However, He’s followed by a Snake, that follows every movement of the player and stay behind. The Snake must grow up everytime a diamond is collected.

What is the actual result

Best result so far, is using tween to move my player and pathfinding on each part of Snake, folowing each other: Snake Tail follow Snake Part that follows SnakeHead, that follows Player. It mostly looks like what i want.

Problem, is turns! Snake must do squared turns since it’s on a grid (called L Shape turns, showed on a screenshot). But pathfinding wouldn’t work with it, as a L shape shouldn’t move.

My current idea, now, is to put topdown behavior on my player, AND on my Snake. Except a hybrid version, like putting a static L turn sprite and giving impression of movement around it.
But are there better ideas?
It’s been 3 months that i’m stuck.

Related screenshots

Here are events and vid of my tweenPlayer/ Pathfinding Snake:

Exemple of L-Turn Sprite:

My start of making my player topdown, for then doing the snake after it:

Project files (optional)

Can’t seem to upload the Project Files tho…
Insert a minimal game showing your issue in a .zip or .rar.

1 Like

Hi …this is an interesting problem
The way that I’d do this is by creating the snake sections in a repeat event at the beginning of the scene and numbering them with a scene variable that counts up.
With all the sections numbered with a unique ID you can then use a repeat event to deal with each snake part in turn and reference the snake part with a number that’s 1 above its number. Number 1 would be the head and number two would refer to the position of number 1 to determine what it does next and so on up the chain until the tail. The difficult thing is referring to the correct section. It sometimes helps here to record the position of snake part 1 in a scene var and then when dealing with part 2 you can refer to that variable rather than snake part 1 …
If that makes sense.
Setting the correct corner animation is tricky and I would use the four points adjacent and outside each snake part to check whether those parts are inside another snake part and you might also need to check the number of these to determine the correct corner animation for each part …this needs to be done after the sections have moved in a repeat event

I kinda did some of that but not shown in my screenshots. Each segments of snake has an Index variable. Head being 1, First part being 2, and so on.

On a previous version, was able to generate parts, but… visual wasn’t … good.

What i don’t get at all, isn’t that, it’s truly that movement. Should i keep my idea of top down behavior on player, and each Snake Part? Or is there other way?

The other way of doing this …if you really want to have some fun with it! …
.is use two arrays ‘before’ and ‘after’… the children of which represent the squares in the grid and the number refers to each type of object but the snake uses numbers say numbers 10 to 40.
You can then, at the beginning of the scene scan through the objects from top to bottom using two repeat events inside each other to record the objects into the before array.
Then in a repeat events count through the grid and check the number of each cell and then check the number of the cells to the left (-1) to the right )+1)and above (deduct world width) below (add world width) in both arrays and use basic if statements to determine changes to the after array.
Then once objects have been moved in the array convert their array number to xy and move the objects themselves
You don’t even need to check collisions now as you can compare the two arrays at the end of each frame. Which sounds like lots of fun to me but I wouldnt recommend trying it.

unsure i understand how it helps me avoiding parts having the right anim at the right time, even when moving between two tilesas, it’s in fact the problem. And you talked about moving the snake, sure, but with what ?Tween, pathfinding, else?

Just a thought - you could check this thread out

@Keith_1357 s algorithm there could work here

The methods I’d mentioned I wasn’t thinking of using any behaviors just moving them one space in the grid which could be done with a tween, but based on logic rather than pathfinding and I’d need to actually do it to work it out. I did some good snakes for my game ‘forty eight foe’ but you need to play it for a while before you get to them.

The tricky bit I think is determining the animations. It needs to be done based on the relative positions of the neighbouring segments. It would be easier if each section was angled between its neighbors using angle between two positions instead of having that kink in the sections…if they’re moving smoothly between the grid it might look weird with kinks moving like that…if when they’re moving the switch to a straight animation and change the angle the the angle between their neighbous then that might look good and then they change back to the right angles when they’ve reached their destination

In an ideal world, what i’d like to achieve is Pokemon GBA movement for both player and Snake. he moves on a grid, yet has an anim that let him move from one to another. Except in my project, player must not stop moving.

Gotcha …I think I’d need to actually do it to see what works best as it’s quite a complicated problem but Im away and don’t have any free computer time for a while. I think that some of the others will find this an interesting puzzle!

Sorry …I didn’t mean to attach that last pathfinding thread here …but it’s an interesting one

i can send the project files if you want, but i don’t know how to put a zip file here…

Hi @Skyman77

As i like to simplify the problems, I give you the base of a solution without comportment:=.

2 cases to see:

  1. the head of snake could avance (in any direction but you must save it in 1 variable attached to the head sprite) without touch a wall (or obstacle)

  2. the head of snake could not avance and you choose an another direction for the head (to memorize in variable of the Head sprite)

To be continue :smile:

Xierra

Hi, thanks for replying! it’s very similar to what i did previously. As shown in my video, Head follows the player correctly.

Head… isn’t much the problem. Problem is the body of the snake. the parts, and tail. having to move, adapt their sprites to their movement AND the tiles they are in.

It’s mostly the transition, from moving from one tile to another that bother me. Especially with those L-Shape snake parts. Got told to make the snake as tiles animation itself, like old snake game… but then there’s a gap between player and Snakehead until player reach center of a tile… so it’s out as an option too.

I’m truly lost. Which upset me so much. because i’m sure i’m near finding the right option…

Hello Skyman77!

Who said i have finished my speach? :smiley:

For that, i must take time to create drawing for best explanations.
Today, i should not have the time but after, i think i’ll ahve time for that.

A+
Xierra

Hi @Amigo54 - I think this ones an interesting puzzle but sadly I’ve not got any time to sort it out. As i said above, the way I really wanted to try doing it, is to treat the whole thing as numbers in an array, but that’s not going to work well with what skyman has already done.
Doing it the gdevelop way the main problem is the pick list problem, and particularly when dealing with setting the animations.
I think it would be best to sort this out in two stages, and sort out moving the sections first. It might help here to number each of them with a text object set to it’s id
Once that’s working then deal with the animation setting, which i think can only be done once all the sections have moved (- Or perhaps there’s a cunning way to do it inside the moving event with variables defined by the direction that each part has moved). Check the adjacent grid positions for each snake section and determine the positions of snake parts with id -1 and +1,

I’ve been watching this thread. What if you used the old snake strategy where you add a new snake tile in the new position and then delete the oldest or tale tile.

But for the smooth movement you could either animate the tiles or use a tiled sprite and move the snake part in or out of view. It would have the effect of making it look like the snake was moving.

So, when you add a new tile it would be blank. Then the snake’s head would either animate into view or you could use a tiled sprite and tween the offset to move the head image into view. IDK if you can actually tween the offset but you can tween a value and set that value to the offset. Meanwhile, the image of the previous head would either move through offset or you could animate into a body image with the head appearing to move to the new tile.

The opposite would be done with the tail. The tail image could move off the last tile to a blank image and move into the 2nd to last tile. Then when the image is either moved or the animation is done then toy delete the last object.

The turns would still be an issue, I guess they would need to be animated to make it look like the snake is moving. And I guess you might need an animation turning the bend into a straight tile or tail. I guess the same would be for the head or any tile that morphed into another type like staraight to curve and curve to straight.

The classic snake movement is easy. You check if the space it’s moving to is empty you then add a tile there. You then delete the last or oldest tile. The 1st tile becomes a head, the 2nd tile a body, the last tile is deleted, so the new last tile becomes a tail.

When I did it, I gave each tile an ID. The new tile or new head was given the next ID number. Then I deleted the oldest tile by subtracting the snake length from that number. I can’t remember if I needed to add or subtract an additional number.

If the snake was 5 tiles long. The head would be 5. So, I guess the tail would be 1.so, it would be 5 - the length +1 unless you don’t count the head in the length. Then you could simply subtract the body length from the head ID. It would be 5 - 4 equals 1.

IDK, this was an early morning thought. The idea might evolve or might not be practical. IDK. When I get the time, I might try it myself.

1 Like

That’s interesting…I hadn’t thought about deleting objects and creating new ones. The last snake that I did, when you chopped the head off the next section becomes the head and they are renumbered. What I couldn’t decide here In this game was when things happen…whether they move in sequence or all at once.
it would be interesting to have a new type of object for this sort of thing called say ‘grid object’ and you were able to refer to cells in the grid rather than x y

Oh wow, wasn’t expecting that many people answering! It’s so cool, love that community!

Will try to answer all in one reply:

@Amigo54 My bad, i thought you finished haha haven’t saw the “To be Continued” at first, my bad^^ Take your time Life is important.

@petlimpet You totally lost me, works kinda saving positions in variables, but for sprites animation?

@Keith_1357 Hi!!! Old Snake strategy is like you said snake not being an object, but Tile anim.
Your idea to simulate smooth movement is kinda what is left of my try: I’m working on as a test for now:
Putting Player, And Snake and his parts all top-down, to see if it might work. For curls, i’ll make it static,so a tile will be change to a Snake Lshape Part but having a 4-frame version of it, so that it looks like it moves. I call that “HybridTopdown”.
Would imply some work on events to move snake parts before after it, but i like to hope it will work^^

Problem i met with full-on old snake Style, is that the Player isn’t the Snake, and as such doesn’t move like it. And i truly don’t want to change how he moves in terms of anim… I like how he is.
So it ends that while the player moves from one tile to another, the Snake doesn’t as it needs an empty tile in front to be there, and as such, has to wait that Player reach the Tile. So it leaves a lil gap…

1 Like

I think I totally lost myself too during this one!