Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions Snake Video Game – Random positioning

  • Snake Video Game – Random positioning

  • Jason Hearne

    February 24, 2020 at 11:14 pm

    I’m trying to recreate an effect that mimics the snake video game.

    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
    Senior Designer/Animator

    Some contents or functionalities here are not available due to your cookie preferences!

    This happens because the functionality/content marked as “Google Youtube” uses cookies that you choosed to keep disabled. In order to view this content or use this functionality, please enable cookies: click here to open your cookie preferences.

  • Alex Printz

    February 25, 2020 at 4:02 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][0];
    b[i] = Arr[i][1];
    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

    A.push(random(Astart, Aend));

    //splits the array into X and Y coordinates;
    splitA = splitArray(A);
    Xcor = splitA[0];
    Ycor = splitA[1];

    P = [];
    i = 1;
    //offsets the coordinates to X/Y right angles based on even/odd calcs
    if(P.length % 2 == 0)
    J = [Xcor[i-(S==1?1:0)],Ycor[i-(S!=1?1:0)]];
    J = [Xcor[i] ,Ycor[i]];


    Alex Printz
    Mograph Designer

  • Jason Hearne

    February 25, 2020 at 8: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!


    Jason Hearne
    Senior Designer/Animator

  • Alex Printz

    February 25, 2020 at 9:16 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

Viewing 1 - 4 of 4 posts

Log in to reply.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy