ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# Snake Video Game - Random positioning

FAQ   •   VIEW ALL Snake Video Game - Random positioning on Feb 24, 2020 at 11:14:08 pm

I'm trying to recreate an effect that mimics the snake video game. https://youtu.be/dXx2RQ30vCQ

I need it to travel from off screen a certain distance on X and then change mid way to Y (continue toggling) until it's off screen.
It needs to travel in a grid pattern like pattern (never diagonal) from specified Point A and B.

I'm trying to find ways of doing this so I may vary the seed and change it's pattern while sticking to a determined grid size. Animating along a spline takes too much time to output the number of different grids I need to do.

perhaps Echo and Mosaic can be involved?

Jason Hearne
Portfolio
Senior Designer/Animator Re: Snake Video Game - Random positioningon Feb 25, 2020 at 4:02:42 pm

This wouldn't be super easy but not too difficult either; you should look at generating paths with random numbers inside an array, then build an if/or statement that will push coordinates from [X[n],Y[n]] or [X[n-1],Y[n]] whether the point is even or odd to get a right-angles pattern. You might have to figure out a way to step the incremental random numbers to a grid; say, round to the nearest integer and then multiply the offset by a standardized number.

Then add a trim path and a white stroke, that will be the snake layer; look at the stroke for the width, then measure the total length of your randomly generated path. calculate the distance and make a proportion and add that to the end to adjust the length. Keyframe the offset from 0 to 100;

Then make another path that grabs the first path and add a very short trim to that and a red stroke, that will be the food. You will need to build a randomized-step based on time that checks if the snake's offset is equal to this offset for all previous frames, and if it is step forward.

Then to the snake's trim end, you will need to check again if and when the offset was equal between the head and the food, and when it is add one incremental end equal to the width of the snake stroke.

I've stuck some code to the bottom for as far as I took this; this is for generating the path. After this, you're on your own.

```function splitArray(Arr){ //function to seperated X and Y coordinates; a = []; b = []; for (i=0;i<Arr.length;i++){ a[i] = Arr[i]; b[i] = Arr[i]; } return [a,b]; } S = 1; //flips the direction of the split; Astart = [-100,100]; //the start point Aend = [2000,900]; //the end point Aseed = 12345; //a random seed Anum = 8; //number of turns the snake will take A=[Astart]; //creates array seedRandom(Aseed,true); for(i=0;i<Anum;i++){ //BUILD THE STEPPER CODE HERE A.push(random(Astart, Aend)); } //splits the array into X and Y coordinates; splitA = splitArray(A); Xcor = splitA; Ycor = splitA; P = []; i = 1; //offsets the coordinates to X/Y right angles based on even/odd calcs while(i<A.length){ if(P.length % 2 == 0) J = [Xcor[i-(S==1?1:0)],Ycor[i-(S!=1?1:0)]]; else{ J = [Xcor[i] ,Ycor[i]]; i++; }; P.push(J); } createPath(P,[],[],false)```

Alex Printz
Mograph Designer Re: Snake Video Game - Random positioningon Feb 25, 2020 at 8:32:32 pm

This is some pretty nifty math!

This works out extremely well. For now I am blowing it out with Mosaic and Levels and it is sticking to my grid. This will help me get across the finish line.

I will circle back around and try to figure out the grid snap later. In order to do so do you mind explaining this part:

`J = [Xcor[i-(S==1?1:0)],Ycor[i-(S!=1?1:0)]];`

Many many kudos!

J

Jason Hearne
Portfolio
Senior Designer/Animator Re: Snake Video Game - Random positioningon Feb 25, 2020 at 9:16:36 pm

Sure, depending on if you've switched S to 0 or 1, it will orientate the turns; keeping either the next path's X or Y the same as the previous point. Essentially, it doesn't allow the snake to change both X and Y coordinates at a time, it only allows one to change, then change them both (which means only the one that didn't initially change does).

so it looks like this:

J = [ Xcor[index - (if S equals 1, 1, else 0), Ycor[index-(if S does not equal 1, 1, else 0)];

Alex Printz
Mograph Designer