stop();
// Snake Game by Strille, 2004, www.strille.net
blockSize = 15; // the block width/height in number of pixels
gameHeight = 16; // the game height in number of blocks
gameWidth = 33; // the game width in number of blocks
SNAKE_BLOCK = 15; // holds the number used to mark snake blocks in the map
SCORE_X_BLOCK = 40;
score = 0; // keeps track of the score
snakeBlockCounter = 0; // keeps track of the snake blocks, increased on every frame
foodCounter = 0;
snakeEraseCounter = -1; // increased on every frame, erases the snake tail (setting this to -3 will result in a 3 block long snake at the beginning)
VIDAS = 3;
vida.gotoAndStop('v3');
keyListener = new Object(); // key listener
keyListener.onKeyDown = function() {
var keyCode = Key.getCode(); // get key code
if (keyCode > 36 && keyCode < 41) { // arrow keys pressed (37 = left, 38 = up, 39 = right, 40 = down)...
if (game.onEnterFrame != undefined) { // only allow moves if the game is running, and is not paused
if (keyCode-37 != turnQueue[0]) { // ...and it's different from the last key pressed
turnQueue.unshift(keyCode-37); // save the key (or rather direction) in the turnQueue
}
}
} else if (keyCode == 32) { // start the game if it's not started (32 = SPACE)
if (!gameRunning) {
startGame();
}
} else if (keyCode == 80) { // pause/unpause (80 = 'P')
if (gameRunning) {
if (game.onEnterFrame) { // pause
delete game.onEnterFrame; // remove main loop
textMC.gotoAndStop("paused");
} else { // exit pause mode
game.onEnterFrame = main; // start main loop
textMC.gotoAndStop("hide");
}
}
}
};
Key.addListener(keyListener);
if (!gameRunning) {
startGame();
}
function startGame() {
x = int(gameWidth/2); // x start position in the middle
y = gameHeight-2; // y start position near the bottom
xVelocity = [-1, 0, 1, 0]; // x velocity when moving left, up, right, down
yVelocity = [0, -1, 0, 1]; // y velocity when moving left, up, right, down
map = new Array(); // create an array to store food and snake
for (var n=0;n<gameWidth;n++) { // make map a 2 dimensional array
map[n] = new Array();
}
turnQueue = new Array(); // a queue to store key presses (so that x number of key presses during one frame are spread over x number of frames)
game.createEmptyMovieClip("food", 1); // create MC to store the food
game.createEmptyMovieClip("s", 2); // create MC to store the snake
currentDirection = 1; // holds the direction of movement (0 = left, 1 = up, 2 = right, 3 = down)
if(VIDAS<=0){
score = 0; // keeps track of the score
snakeBlockCounter = 0; // keeps track of the snake blocks, increased on every frame
foodCounter = 0;
snakeEraseCounter = -1; // increased on every frame, erases the snake tail (setting this to -3 will result in a 3 block long snake at the beginning)
VIDAS = 3;
vida.gotoAndStop('v3');
}
scoreTextField.text = "Puntos: "+score+" | Vidas: "+VIDAS; // type out score info
placeFood("new"); // place a new food block
textMC.gotoAndStop("hide"); // make sure no text is visible (like "game over ")
game.onEnterFrame = main; // start the main loop
gameRunning = true; // flag telling if the game is running. If true it does not necessarily mean that main is called (the game could be paused)
}
function main() { // called on every frame if the game is running and it's not paused
if (turnQueue.length > 0) { // if we have a turn to perform...
var dir = turnQueue.pop(); // ...pick the next turn in the queue...
if (dir % 2 != currentDirection % 2) { // not a 180 degree turn (annoying to be able to turn into the snake with one key press)
currentDirection = dir; // change current direction to the new value
}
}
x += xVelocity[currentDirection]; // move the snake position in x
y += yVelocity[currentDirection]; // move the snake position in y
if (map[x][y] != SNAKE_BLOCK && x > -1 && x < gameWidth && y > -1 && y < gameHeight) { // make sure we are not hitting the snake or leaving the game area
game.s.attachMovie("snakeMC", snakeBlockCounter, snakeBlockCounter, {_x: x*blockSize, _y: y*blockSize}); // attach a snake block movie clip
snakeBlockCounter++; // increase the snake counter
if (typeof(map[x][y]) == "movieclip") { // if it's a not a vacant block then there is a food block on the position
score += SCORE_X_BLOCK; // add points to score
scoreTextField.text = "Puntos: "+score+" | Vidas: "+VIDAS; // type out score info
snakeEraseCounter -= 1; // make the snake not remove the tail for five loops
placeFood(map[x][y]); // place the food movie clip which is referenced in the map map[x][y]
}
map[x][y] = SNAKE_BLOCK; // set current position to occupied
var tailMC = game.s[snakeEraseCounter]; // get "last" MC according to snakeEraseCounter (may not exist)
if (tailMC) { // if the snake block exists
delete map[tailMC._x/blockSize][tailMC._y/blockSize]; // delete the value in the array m
tailMC.removeMovieClip(); // delete the MC
}
snakeEraseCounter++; // increase erase snake counter
} else { // GAME OVER if it is on a snake block or outside of the map
gameOver();
}
}
function gameOver() {
VIDAS--;
if(VIDAS<=0){
textMC.gotoAndStop("gameOver"); // show "game over" text
}else{
textMC.gotoAndStop("vidamenos");
vida.gotoAndPlay('v'+VIDAS);
}
delete game.onEnterFrame; // quit looping main function
gameRunning = false; // the game is no longer running
}
function placeFood(foodMC) {
do {
var xFood = random(gameWidth);
var yFood = random(gameHeight);
} while (map[xFood][yFood]); // keep picking a spot until it's a vacant spot (we don't want to place the food on a position occupied by the snake)
if (foodMC == "new") { // create a new food movie clip
foodMC = game.food.attachMovie("foodMC", foodCounter, foodCounter);
foodCounter++;
}
foodMC._x = xFood*blockSize; // place the food
foodMC._y = yFood*blockSize; // place the food
map[xFood][yFood] = foodMC; // save a reference to this food movie clip in the map
}