* This class manages the snake which will reside inside of a SNAKE.Board object.
* @class Snake
* @constructor
* @namespace SNAKE
* @param {Object} config The configuration object for the class. Contains playingBoard (the SNAKE.Board that this snake resides in), startRow and startCol.
* This method is called when a user presses a key. It logs arrow key presses in "moveQueue", which is used when the snake needs to make its next move.
* @method handleArrowKeys
* @param {Number} keyNum A number representing the key that was pressed.
*/
/*
Handles what happens when an arrow key is pressed.
Direction explained (0 = up, etc etc)
0
3 1
2
*/
me.handleArrowKeys=function(keyNum){
if (isDead||isPaused){return;}
varsnakeLength=me.snakeLength;
varlastMove=moveQueue[0]||currentDirection;
//console.log("lastmove="+lastMove);
//console.log("dir="+keyNum);
switch (keyNum){
case37:
case65:
if (lastMove!==1||snakeLength===1){
moveQueue.unshift(3);//SnakeDirection = 3;
}
break;
case38:
case87:
if (lastMove!==2||snakeLength===1){
moveQueue.unshift(0);//SnakeDirection = 0;
}
break;
case39:
case68:
if (lastMove!==3||snakeLength===1){
moveQueue.unshift(1);//SnakeDirection = 1;
}
break;
case40:
case83:
if (lastMove!==0||snakeLength===1){
moveQueue.unshift(2);//SnakeDirection = 2;
}
break;
}
};
/**
* This method is executed for each move of the snake. It determines where the snake will go and what will happen to it. This method needs to run quickly.
* @method go
*/
me.go=function(){
varoldHead=me.snakeHead,
newHead=me.snakeTail,
myDirection=currentDirection,
grid=playingBoard.grid;// cache grid for quicker lookup
if (isPaused===true){
setTimeout(function(){me.go();},snakeSpeed);
return;
}
me.snakeTail=newHead.prev;
me.snakeHead=newHead;
// clear the old board position
if (grid[newHead.row]&&grid[newHead.row][newHead.col]){
* @param {Object} config The configuration object for the class. Set fullScreen equal to true if you want the game to take up the full screen, otherwise, set the top, left, width and height parameters.
* Gets the current state of the playing board. There are 3 states: 0 - Welcome or Try Again dialog is present. 1 - User has pressed "Start Game" on the Welcome or Try Again dialog but has not pressed an arrow key to move the snake. 2 - The game is in progress and the snake is moving.
* @method getBoardState
* @return {Number} The state of the board.
*/
me.getBoardState=function(){
returnboardState;
};
/**
* Sets the current state of the playing board. There are 3 states: 0 - Welcome or Try Again dialog is present. 1 - User has pressed "Start Game" on the Welcome or Try Again dialog but has not pressed an arrow key to move the snake. 2 - The game is in progress and the snake is moving.
* @method setBoardState
* @param {Number} state The state of the board.
*/
me.setBoardState=function(state){
boardState=state;
};
/**
* @method getGridFoodValue
* @return {Number} A number that represents food on a number representation of the playing board.
*/
me.getGridFoodValue=function(){
returnGRID_FOOD_VALUE;
};
/**
* @method getPlayingFieldElement
* @return {DOM Element} The div representing the playing field (this is where the snake can move).
*/
me.getPlayingFieldElement=function(){
returnelmPlayingField;
};
/**
* @method setBoardContainer
* @param {DOM Element or String} myContainer Sets the container element for the game.
*/
me.setBoardContainer=function(myContainer){
if (typeofmyContainer==="string"){
myContainer=document.getElementById(myContainer);
}
if (myContainer===elmContainer){return;}
elmContainer=myContainer;
elmPlayingField=null;
me.setupPlayingField();
};
/**
* @method getBoardContainer
* @return {DOM Element}
*/
me.getBoardContainer=function(){
returnelmContainer;
};
/**
* @method getBlockWidth
* @return {Number}
*/
me.getBlockWidth=function(){
returnblockWidth;
};
/**
* @method getBlockHeight
* @return {Number}
*/
me.getBlockHeight=function(){
returnblockHeight;
};
/**
* Sets up the playing field.
* @method setupPlayingField
*/
me.setupPlayingField=function (){
if (!elmPlayingField){createBoardElements();}// create playing field
// calculate width of our game container
varcWidth,cHeight;
if (config.fullScreen===true){
cTop=0;
cLeft=0;
cWidth=getClientWidth()-5;
cHeight=getClientHeight()-5;
document.body.style.backgroundColor="#FC5454";
}else{
cTop=config.top;
cLeft=config.left;
cWidth=config.width;
cHeight=config.height;
}
// define the dimensions of the board and playing field