# Snake Trail - Example -

0 favourites
• 33 posts
From the Asset Store
Snake
\$9.99 USD
Template for snake, fully documented in comments and video
• Okay ... Chcking it out this night ...

• Whiteclaws, I still haven't solved my dilemma. The positioning of the body parts is not as easy as I thought it would be. On the slower computers the body parts are spread far apart.

However, I am also a confused about MaxArrayW you are effectively adding to the size every 0.01 of second. Would that array just get bigger and bigger? I am having trouble following the logic.

• Okay , I'm solving it for you today , sorry if I forgot you , I'm currently making a procedural dungeon generator ... czar , just to explain you , the tail takes the n*10 first values of the arrays and position the tail depending on them and thus the more we add data , the n*10 values change ... That makes the tail follow the player

• Cheers thanks. Good solution.

I changed formula to 10*(fps/60) and then it works. Before you had a divide so only FPS the gap was getting larger.

• Oops , math misurstanding by me ;P

Well , you found it anyways !

• dl.dropbox.com/u/89532891/Others/C2HowdoI/MaybeFixed.capx%20

Hi, I wonder if there's any chance of you sharing this again? I'm having the same problem with the segments spreading far apart whenever the frame rate drops a bit, and unfortunately the link to your solution no longer works.

Thanks!

• czar , do you always have the capx , I think I lost mine

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• Oh, that's a shame...

I did tried solving the issue, and this is what kind of worked. I took the expression

Pos.At(MaxArrayW-10*Crystal.SpawnNumber,0)

and changed it to

Pos.At(MaxArrayW-10*(fps/60)*Crystal.SpawnNumber,0)

The result was that the segments no longer started spacing apart, because the distance kept adjusting. But also because of that, the size of the snake kept changing slightly, resulting in a constant twitch.

• No, I'm guessing the actual problem is the fact that the array grows every 0.01 seconds. After a few minutes, the framerate starts dropping because the array is too large, and things only go worse from that.

Is there a way to tho the snake trail without a constantly growing array? I'd kill for the solution...

Edit: Okay, I think I've managed to actually come up with a solution for this. I still have to test it, and there's probably a way to do it with dt to avoid issues with lower framerates, but at least it works from White Claw's principle but without using an ever growing array.

So I have two Sprites:

• Player (the "head" of the snake, which is a bullet)
• Segment (each segment of the body)

The "Segment" has a "SpawnNumber" variable, set on Created, just like in White Claw's version, to keep track of the number of each segment in the body.

There are two arrays, with sizes (X,Y,Z):

• PosX (1000,1,1)
• PosY (1000,1,1)

(1000 is the size I've choose, but you could change it. It should be the value you're using to set the distance (here 10) * the maximum number of segments you're planning on allowing (here 100).)

Every 0.01 seconds, this happens:

• PosX --> push front Player.X on X axis
• PosX --> pop back on X axis
• PosY --> push front Player.Y on X axis
• PosY --> pop back on X axis

(this adds the current X and Y position on the front of each array, pushing the whole array and poping the extra value out, keeping the size constant)

• Segment --> set position to (PosX.at(10*Segment.SpawnNumber),PosYat(10*Segment.SpawnNumber))

(this takes, for each segment, the values for the X and Y coordinates corresponding to its SpawnNumber. Like in WC's version, 10 is just the number used to set the distance, and could be changed as long as the arrays are changed accordingly.)

It was easier than I expected.

• Whiteclaws, I believe this was the original one

and this is a later version

• Thanks a lot!

• I see another issue, the array should be cleaned from time to time.

It increases too quick making the game slower.

Is there a way to remove the older positions from it as the player keep moving?

• jogosgratispro, you have to use a static sized array and push/pop the new positions. Here's a quick example.

https://dl.dropboxusercontent.com/u/895 ... Snake.capx

Edit: Ninja'd by Peter Parker

czar, The later version looks so cool <img src="{SMILIES_PATH}/icon_e_smile.gif" alt=":)" title="Smile">

• If we allow the speed of the snake to increase as it gobbles up more apples, then the distance between adjacent segments will also increase.

I tried to correct this by changing the array update expression to

Pos.At(MaxArrayW - 1,0) + (PlayerHead.X - Pos.At(MaxArrayW - 1,0)) / PlayerHead.Bullet.Speed * InitialSpeed

(in a way to generate a Pos array which tracks the x y values as if the snake's speed remains at InitialSpeed)

but it doesn't seem to do the trick.

Could someone pls give me some suggestion?