the script
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();
}
}
};
}
Read more: http://www.webdesign.org/web/flash-&-swish/flash-tutorials/snake-game.11079.html#ixzz0OCNfWv0Y
the script with explanation
I started by defining the variables I would use later in the game.
var unit = 15; //The size of each square
var uwh = 20; //The number of squares across and down.
var canMove = false; //The snake will not be moving until there is a mouse click to start the game.
var dir = 2; //The snake will start by moving downwards, UP=0, LEFT=1, DOWN=2, RIGHT=3
var score = 0; //Your initial score is 0.
aPieceList = new Array(); //Creates an empty Array to store all of the movie clips that make up the snake.
Now we will define the mouseListener Object. This waits for a mouse click, when there is a mouse click the onMouseDown function is called.
mouseListener = new Object();
mouseListener.onMouseDown = function()
{
if (!canMove) //This prevents you from clicking in the middle of the game and starting a new game. (If you are not moving, start the game).
{
canMove = true; //Let's you move now.
startGame(); //Calls the start game function explained later.
}
};
Mouse.addListener(mouseListener);
Now we define the keyListener. The onKeyDown function activates when a key on the keyboard is pressed.
k = new Object();
k.onKeyDown = function()
{
var k = Key.getCode(); //Gets the ASCII code of the key that is pressed.
if (k == Key.UP & & dir != 2 & & canMove) //If they pressed UP and you are not currently moving down (as to not hit yourself) and you are able to move.
{
dir = 0; //sets your direction to Up
canMove = false; //sets the ability to change direction to false to prevent you from changing directions before the next frame causing some bugs.
}
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);
Now we have the addPiece function. When this function is called a piece is added to the end of the snake to increase its length.
function addPiece()
{
var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length); //attaches a piece to the stage
p._x = aPieceList[aPieceList.length - 1]._x; //sets the x coordinate of the piece attached to the one in front of it.
p._y = aPieceList[aPieceList.length - 1]._y; //sets the y coordinate of the piece attached to the one in front of it.
aPieceList.push(p); //adds the piece to the array of pieces.
}
Now we define the moveFood function which moves the food when the snake touches it.
function moveFood()
{
var moveIt = true;
while (moveIt) //keeps moving the food until it is not touching the snake.
{
food._x = Math.floor(Math.random() * uwh) * unit; //chooses a random x value for the food.
food._y = Math.floor(Math.random() * uwh) * unit; //chooses a random y value for the food.
moveIt = false;
for (var i = 0; i < aPieceList.length; i++)
{
if (aPieceList[i]._x == food._x & & aPieceList[i]._y == food._y) //Checks if you are moving the food to a spot the snake already occupies, if you are then try again.
{
moveIt = true;
}
}
}
}
Now we will define the gameOver function which is called when your snake hits itself.
function gameOver()
{
delete this.onEnterFrame; //deletes the onEnterFrame which was causing the snake to move every frame.
tScore.text = "You Lose. Score: " + score; //Displays your score in the text field.
canMove = false; //Sets it so you cannot move anymore.
}
The startGame function is where the most important parts of the ActionScript are. This includes the function that moves the snake every frame and checks if it is hitting itself, also adding the snake initially to the stage.
function startGame()
{
for (var i = aPieceList.length - 1; i >= 0; i--) //deletes old snake if one exists.
{
aPieceList[i].removeMovieClip();
aPieceList.pop();
}
score = 0; //sets score to 0.
var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length); //Attaches the first piece of the snake.
aPieceList.push(p); //Adds that piece to the array of pieces.
p._x = 10 * unit; //Centers the snake horizontally.
p._y = 10 * unit; //Centers the snake vertically.
var food = this.attachMovie("food", "food", -1); //Attaches the food to the stage.
moveFood(); //Moves the food to a random location on the Stage.
var startingLength = 3; //Sets the initial starting length of the snake to 3.
for (var i = 1; i < startingLength; i++) //Creates the rest of the snake.
{
addPiece();
}
this.onEnterFrame = function() //Declares a function that is called on every frame.
{
canMove = true; //Sets the snake's ability to move to true.
tScore.text = score; //Updates the Score text box with your score.
for (var i = aPieceList.length - 1; i > 0; i--) //Moves the snake (each piece moves to the piece in front of its location.)
{
aPieceList[i]._x = aPieceList[i - 1]._x;
aPieceList[i]._y = aPieceList[i - 1]._y;
}
if (dir == 0) //If you're facing up then move up.
{
aPieceList[0]._y -= unit;
}
else if (dir == 1) //If you're facing left then move left
{
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) //If you are lower than the allowed area, move your front snake piece to the top.
{
aPieceList[0]._y = 0;
}
else if (aPieceList[0]._y / unit == -1) //If you are higher than the allowed area, move your front snake piece to the right side.
{
aPieceList[0]._y = 19 * unit;
}
else if (aPieceList[0]._x / unit == -1) //If you are to the left of the allowed area.
{
aPieceList[0]._x = 19 * unit;
}
else if (aPieceList[0]._x / unit == 20) //If you are to the right of the allowed area.
{
aPieceList[0]._x = 0;
}
if (aPieceList[0]._x == food._x & & aPieceList[0]._y == food._y) //If your front piece is touching food then move the food and add points to the score, also add a piece to the end of the snake.
{
score += 10 * aPieceList.length / 2;
moveFood();
addPiece();
}
for (var i = 1; i < aPieceList.length; i++) //If your front piece is touching any part of your body, game over.
{
if (aPieceList[0]._x == aPieceList[i]._x & & aPieceList[0]._y == aPieceList[i]._y)
{
gameOver();
}
}
};
}
I hope this explanation helped you understand how the ActionScript in the Snake Game tutorial worked. If you are unsure on what a function or field does, use the ActionScript dictionary built into flash.
Read more:
http://www.webdesign.org/web/flash-&-swish/flash-tutorials/snake-game-actionscript-explained.11078.html#ixzz0OCOFDQHu