Skip to Main Content
Mr. Johnson's Art

Flashy Snake Game IN ANIMATE


In this Adobe ANIMATE lesson you will learn
how to make a simple GAME called Flashy Snake.

Since this program has been updated, it may not work now with the coding.
Things will look slightly different.
Let me know if you have any problems.

Make sure to follow all instructions carefully.
 

STEP #1

First, Open Animate.

Step #2

Follow these steps:

Select the RECTANGLE TOOL from the Tools panel.

Set the fill color to a shade of green (You can pick what shade).
Stroke Color should have a red line through it.

Draw a square (It doesn’t have to be perfect!)

Click the Select tool

Click to select the Whole shape.


Step #3

In the Properties Panel, Resize the shape to 25x25.

It should now look like this:

Control+Click on the square and Click Convert to Symbol.

Name it “SnakePart”
(make sure it’s exactly the same!!! NAME IT FIRST THEN EXPORT)


See Settings Below:

You may get a "pop-up" message about an error - if so, ignore it and click OK.

Set the Instance Name in the Properties tab to the same name if you want (Optional).

Now select the “SnakePart” square and delete it!


 Step #4

Now select the Oval Tool.

Set the color to Red.
Make sure the stroke color has a red line in it.

Draw a circle somewhere!

Click to select all of it.

In the Properties Panel Resize it to 25x25.

It should look like this

Control+Click and Convert it to a Symbol.

Name it Food (Yes the capital F is needed.) Click ok.
(NOTE: Name the symbol FIRST!!!!)

Look at the other settings, too.

Click OK. Then delete the circle.
You may get a "pop-up" message about an error - if so, ignore it and click OK.


Step #5 

Click onto the white screen after the circle is deleted.
In the Publish tab find the CLASS text box.

Fill that in with the word “Main”

Now go FILE>NEW and select ActionScript File.

  


Copy the code below, and paste it into that new window.
This will make the game work

package{
            import flash.display.MovieClip;
            import flash.events.KeyboardEvent;
            import flash.ui.Keyboard;
            import flash.events.Event; //used for ENTER_FRAME event
           
            public class Main extends MovieClip{
                        const speed:int = 12;//speed of the snake
                        var score:int;
                        var vx:int;
                        var vy:int;
                        var gFood:Food;
                        var head:SnakePart;
                        var SnakeDirection:String;
                        var snake:Array;
             
                         public function Main(){
                                    init();
                          }
                          function init():void {
                                    //Initialize everything!
                                    vx = 1; vy = 0;
                                    score = 0;
                                    snake = new Array();
                                    SnakeDirection = "";
                                    //add food to the stage
                                    addFood();
                                    //add snakes head to the stage
                                    head = new SnakePart();
                                    head.x = stage.stageWidth/2;
                                    head.y = stage.stageHeight/2;
                                    snake.push(head);
                                    addChild(head);

                                    stage.addEventListener(KeyboardEvent.KEY_UP , onKeyUp);
                                    stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown);
                                    addEventListener(Event.ENTER_FRAME , onEnterFrame);

                                   //ENTER_FRAME listener is attached to main class and not to the stage directly
                        }
                        //This function will add food to the stage
                        function addFood():void {
                                    gFood = new Food();
                                    gFood.x = 50 + Math.random()*(stage.stageWidth-100);
                                    gFood.y = 50 + Math.random()*(stage.stageHeight-100);
                                    addChild(gFood);
                         }
                         //this function will reset the game
                         function reset():void {
                                    removeChild(gFood);
                                    addFood();
                                    head.x = stage.stageWidth/2;
                                    head.y = stage.stageHeight/2;
                                    vx = 1;vy = 0;

                                   for(var i = snake.length-1;i>0;--i){
                                               removeChild(snake[i]);
                                               snake.splice(i,1);
                                   }
                        }
                        function onKeyDown(event:KeyboardEvent):void{
                                      if(event.keyCode == Keyboard.LEFT){
                                      SnakeDirection = "left";
                                       }else if (event.keyCode == Keyboard.RIGHT) {
                                      SnakeDirection = "right";
                                      }else if (event.keyCode == Keyboard.UP) {
                                                SnakeDirection = "up";
                                    }else if (event.keyCode == Keyboard.DOWN) {
                                                SnakeDirection = "down";
                                     }
                       }
                       function onKeyUp(event:KeyboardEvent):void {
                                   if(event.keyCode == Keyboard.LEFT) {
                                        SnakeDirection = "";
                                    }else if(event.keyCode == Keyboard.RIGHT) {
                                        SnakeDirection = "";
                                    }else if(event.keyCode == Keyboard.UP ) {
                                         SnakeDirection = "";
                                     }else if(event.keyCode == Keyboard.DOWN){
                                         SnakeDirection = "";
                                      }
                        }
                        function onEnterFrame(event:Event):void {
                                    //setting direction of velocity
                                    if(SnakeDirection == "left" && vx != 1) {
                                                vx = -1;
                                                vy = 0;
                                     }else if(SnakeDirection == "right" && vx != -1) {
                                                 vx = 1;
                                                 vy = 0;
                                     }else if(SnakeDirection == "up" && vy != 1) {
                                                 vx = 0;
                                                 vy = -1;
                                      }else if(SnakeDirection == "down" && vy != -1) {
                                                 vx = 0;
                                                 vy = 1;
                                       }
                                      //collison with stage
                                      if(head.x - head.width/2 <= 0){
                                               score = 0;
                                               reset();
                                        }
                                        if(head.x + head.width/2 >= stage.stageWidth){
                                               score = 0;
                                               reset();
                                        }
                                        if(head.y - head.height/2 <= 0){
                                               score = 0;
                                               reset();
                                         }
                                         if(head.y + head.height/2 >= stage.stageHeight){
                                                score = 0;
                                                reset();
                                           }

                                    //move body of the snake
                                    for(var i = snake.length-1;i>0;--i){
                                                snake[i].x = snake[i-1].x;
                                                 snake[i].y = snake[i-1].y;
                                     }
                                     //changing the position of snake's head
                                     head.x += vx*speed;
                                     head.y += vy*speed;
                                     //collision with tail
                                     for(var i = snake.length-1;i>=1;--i){
                                                if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
                                                            reset();
                                                            break;
                                                 }
                                     }
                                    //collision with food
                                    if(head.hitTestObject(gFood)){
                                                score += 1;
                                                removeChild(gFood);
                                                addFood();
                                                var bodyPart = new SnakePart();
                                                bodyPart.x = snake[snake.length - 1].x;
                                                bodyPart.y = snake[snake.length - 1].y;
                                                snake.push(bodyPart);
                                                addChild(bodyPart);
                                     }
                                    }
                      }
}

It should now look like this:

 Go ahead and save that to the desktop as Main.as

 

Now that your actionscript is saved, save your Animate file, too!
Make sure you save it to the desktop. (See below)!

 

 

Now press Command+Enter and test out your game!

You may see some error messages, but the game should work!

How to play: Use the arrow key to move. The object is to collect the little red balls, raising your score!

From your program, go to FILE>SAVE AS and choose DESKTOP, then drag from the DESKTOP to GOOGLE DRIVE and the Correct Class Folder. Save your final file in Google Classroom. Just add the file from Google Drive.

Upcoming Events

May 6

Teacher Appreciation Week

Start: May 6, 2024 End: May 10, 2024

Multi-Day Event

May 7

Interagency Meeting

10:30 AM - 12:00 PM

May 9

Ready for Kinder

5:30 PM - 7:30 PM

This site provides information using PDF, visit this link to download the Adobe Acrobat Reader DC software.