Snake Game

Browse Pages: 2  > >>

Requirements

  • Flash MX or higher.

Preview

1. Start by creating a 300�350 document in Flash.

2. In the Property Inspector set the Frame Rate to 12. (Set it higher for a faster snake game)

3. Select the Rectangle Tool and draw a square on the stage.

4. Use the Selection Tool to double click on the square and in the Property Inspector set the following:

W: 300
H: 300
X: 0
Y: 0

5. Change the colors how you want, this will be the background of your snake game.

image 1

6. Select the Text Tool and in the Property Inspector set the Text Type to Dynamic.

7. Draw a text box in the white area of the stage, this is where your score will be displayed.

8. Inside the text box write "Click to start" and change the font face and color however you like.

9. With the text box selected, in the Property Inspector set the instance name to "tScore".

image 2

10. Now press Ctrl+F8 and type "piece" for the name and set the type to Movie Clip. Click on Advanced and check the box by "Export for ActionScript", the identifier should automatically be set to "piece", if not, set it as that and press OK.

image 3

11. Select the Rectangle Tool and draw a square on the stage.

12. Use the Selection Tool to double click on the square and in the Property Inspector set the following:

W: 15
H: 15
X: 0
Y: 0

This will be the piece that makes up the body of your snake. If you do not want your snake to be made of squares you can change it here, but keep the width and height to 15.

13. Return to Scene 1 and press Ctrl+F8 again and this time type "food" for the name and set the type to Movie Clip. Click on Advanced and check the box by "Export for ActionScript", the identifier should automatically be set to "food", if not, set it as that and press OK.

14. Select the Oval Tool and draw a circle on the stage.

15. Use the Selection Tool to double click on the circle and in the Property Inspector set the following:

W: 15
H: 15
X: 0
Y: 0

This will be the food that your snake eats to gain length. If you do not want your food to be made of circles you can change it here, but keep the width and height to 15.

16. Return to Scene 1 and click on frame 1 and press F9 to open the Actions Panel and add the following code.

var unit = 15;
var uwh = 20;
var canMove = false;
var dir = 2;
var score = 0;
aPieceList = new Array();
mouseListener = new Object();
mouseListener.onMouseDown = function()
{
        if (!canMove)
        {
                canMove = true;
                startGame();
        }
};
Mouse.addListener(mouseListener);
k = new Object();
k.onKeyDown = function()
{
        var k = Key.getCode();
        if (k == Key.UP && dir != 2 && canMove)
        {
                dir = 0;
                canMove = false;
        }
        else if (k == Key.LEFT && dir != 3 && canMove)
        {
                dir = 1;
                canMove = false;
        }
        else if (k == Key.DOWN && dir != 0 && canMove)
        {
                dir = 2;
                canMove = false;
        }
        else if (k == Key.RIGHT && dir != 1 && canMove)
        {
                dir = 3;
                canMove = false;
        }
};
Key.addListener(k);
function addPiece()
{
        var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length);
        p._x = aPieceList[aPieceList.length - 1]._x;
        p._y = aPieceList[aPieceList.length - 1]._y;
        aPieceList.push(p);
}
function moveFood()
{
        var moveIt = true;
        while (moveIt)
        {
                food._x = Math.floor(Math.random() * uwh) * unit;
                food._y = Math.floor(Math.random() * uwh) * unit;
                moveIt = false;
                for (var i = 0; i < aPieceList.length; i++)
                {
                        if (aPieceList[i]._x == food._x && aPieceList[i]._y == food._y)
                        {
                                moveIt = true;
                        }
                }
        }
}
function gameOver()
{
        delete this.onEnterFrame;
        tScore.text = "You Lose.  Score: " + score;
        canMove = false;
}
function startGame()
{
        for (var i = aPieceList.length - 1; i >= 0; i--)
        {
                aPieceList[i].removeMovieClip();
                aPieceList.pop();
        }
        score = 0;
        var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length);
        aPieceList.push(p);
        p._x = 10 * unit;
        p._y = 10 * unit;
        var food = this.attachMovie("food", "food", -1);
        var c = 0;
        moveFood();
        var startingLength = 3;
        for (var i = 1; i < startingLength; i++)
        {
                addPiece();
        }
        this.onEnterFrame = function()
        {
                canMove = true;
                tScore.text = score;
                for (var i = aPieceList.length - 1; i > 0; i--)
                {
                        aPieceList[i]._x = aPieceList[i - 1]._x;
                        aPieceList[i]._y = aPieceList[i - 1]._y;
                }
                if (dir == 0)
                {
                        aPieceList[0]._y -= unit;
                }
                else if (dir == 1)
                {
                        aPieceList[0]._x -= unit;
                }
                else if (dir == 2)
                {
                        aPieceList[0]._y += unit;
                }
                else if (dir == 3)
                {
                        aPieceList[0]._x += unit;
                }
                if (aPieceList[0]._y / unit == 20)
                {
                        aPieceList[0]._y = 0;
                }
                else if (aPieceList[0]._y / unit == -1)
                {
                        aPieceList[0]._y = 19 * unit;
                }
                else if (aPieceList[0]._x / unit == -1)
                {
                        aPieceList[0]._x = 19 * unit;
                }
                else if (aPieceList[0]._x / unit == 20)
                {
                        aPieceList[0]._x = 0;
                }
                if (aPieceList[0]._x == food._x && aPieceList[0]._y == food._y)
                {
                        score += 10 * aPieceList.length / 2;
                        moveFood();
                        addPiece();
                }
                for (var i = 1; i < aPieceList.length; i++)
                {
                        if (aPieceList[0]._x == aPieceList[i]._x && aPieceList[0]._y == aPieceList[i]._y)
                        {
                                gameOver();
                        }
                }
        };
}

17. You may now test your movie

18. For information on how the ActionScript in this tutorial works, see Snake Game ActionScript Explained


 



Author's URL: Tutorial Scene
Browse Pages: 2  > >>
Final results of our readers
New!
Passed through all the steps? Share your result!
Your result will be premoderated.
Please make sure you choose the right image.
 
 



Captcha

*Required fileds
Our Flash tutorials provide you with step-by-step instructions on how to create beautiful Flash animations and integrate them into your website. More Flash Tutorials: Featured Materials | Fresh Materials | More Flash Tutorials at FlashPerfection.com

Reader's comments
comments webindia July 13, 2011 says:
Great work and well done.
Reply
comments Flet June 28, 2011 says:
Great tuutorial, but what´s is the code for as3
Reply
comments sam June 22, 2011 says:
how do i change the score so it only goes up by 10 each time?
Reply
comments sam June 22, 2011 says:
how do i change the score so it only goes up by 10 each time?
Reply
comments amine December 28, 2010 says:
hi,

yes maybe a simple tutoriel, but how can explan how fonction the code and it's possible to creat class (POO)!!!!!!!

Reply
comments amine December 28, 2010 says:
salut à tous;

oui; bien c'est un bon tutorial mais est ce qu'on peut trouver un code de ce jeu mais avec notion des classes ;

merci

Reply
comments CORY April 19, 2011 says:
dsfadfhwdgsdfgad? DFSDDGSFHERTGXDZSERFSFHS!
Reply
comments josh October 24, 2010 says:
wow its so simple.... but is it possible to add walls? i found that even when sped up its way too easy...
Reply
comments squashiejoshie June 13, 2010 says:
wow! thanks, what a great tutorial! its so simple and easy!
Reply
Add comments to "Snake Game"

Captcha