From 359699eb9ace5f85846f17c99b0c07539759a7f0 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 12 Jul 2017 14:18:27 -0400 Subject: [PATCH 01/66] Small improvements --- .idea/JavaScript-Snake.iml | 9 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 683 +++++++++++++++++++++++++++++++++++++ index.html | 6 +- js/snake.js | 46 +-- 6 files changed, 733 insertions(+), 25 deletions(-) create mode 100644 .idea/JavaScript-Snake.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/JavaScript-Snake.iml b/.idea/JavaScript-Snake.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/.idea/JavaScript-Snake.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..5a03e45f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..9c53b18c --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,683 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + down + left + 32 + handleArrowKeys + getBoardState + setBoardState + set + boardState + mode + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1499879325478 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/index.html b/index.html index 04762927..f29a0194 100755 --- a/index.html +++ b/index.html @@ -1,4 +1,4 @@ - + +--> JavaScript Snake @@ -26,7 +26,7 @@ } var index = document.getElementById("select").selectedIndex; switch (index) { - case 0: + case 0: changeTheme('css/dark-snake.css'); break; case 1: changeTheme('css/main-snake.css'); diff --git a/js/snake.js b/js/snake.js index 3c9dabe0..fc46d039 100644 --- a/js/snake.js +++ b/js/snake.js @@ -4,6 +4,11 @@ By Patrick Gillespie http://patorjk.com/games/snake */ + +var BOARD_STATE_GAME_INITIAL = 0; +var BOARD_STATE_GAME_STARTING = 1; +var BOARD_STATE_GAME_STARTED = 2; + /** * @module Snake * @class SNAKE @@ -208,10 +213,8 @@ SNAKE.Snake = SNAKE.Snake || (function() { var snakeLength = me.snakeLength; var lastMove = moveQueue[0] || currentDirection; - //console.log("lastmove="+lastMove); - //console.log("dir="+keyNum); - switch (keyNum) { + case 37: case 65: if ( lastMove !== 1 || snakeLength === 1 ) { @@ -610,11 +613,10 @@ SNAKE.Board = SNAKE.Board || (function() { elmPauseScreen = document.createElement("div"); elmPauseScreen.className = "snake-pause-screen"; elmPauseScreen.innerHTML = "
[Paused]

Press [space] to unpause.

"; - + elmAboutPanel = document.createElement("div"); elmAboutPanel.className = "snake-panel-component"; - elmAboutPanel.innerHTML = "more patorjk.com apps - source code"; - + elmLengthPanel = document.createElement("div"); elmLengthPanel.className = "snake-panel-component"; elmLengthPanel.innerHTML = "Length: 1"; @@ -668,13 +670,14 @@ SNAKE.Board = SNAKE.Board || (function() { var loadGame = function() { SNAKE.removeEventListener(window, "keyup", kbShortcut, false); tmpElm.style.display = "none"; - me.setBoardState(1); + me.setBoardState(BOARD_STATE_GAME_STARTING); me.getBoardContainer().focus(); }; var kbShortcut = function(evt) { if (!evt) var evt = window.event; var keyNum = (evt.which) ? evt.which : evt.keyCode; + //32 = Space, 13 = Enter if (keyNum === 32 || keyNum === 13) { loadGame(); } @@ -700,14 +703,15 @@ SNAKE.Board = SNAKE.Board || (function() { var reloadGame = function() { tmpElm.style.display = "none"; me.resetBoard(); - me.setBoardState(1); + me.setBoardState(BOARD_STATE_GAME_STARTING); me.getBoardContainer().focus(); }; var kbTryAgainShortcut = function(evt) { - if (boardState !== 0 || tmpElm.style.display !== "block") {return;} + if (me.getBoardState() !== BOARD_STATE_INITIAL || tmpElm.style.display !== "block") {return;} if (!evt) var evt = window.event; var keyNum = (evt.which) ? evt.which : evt.keyCode; + //32 = Space, 13 = Enter if (keyNum === 32 || keyNum === 13) { reloadGame(); } @@ -747,15 +751,14 @@ SNAKE.Board = SNAKE.Board || (function() { me.setupPlayingField(); }; /** - * 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 + * Gets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. * @return {Number} The state of the board. - */ + */ me.getBoardState = function() { return boardState; }; /** - * 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. + * Sets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. * @method setBoardState * @param {Number} state The state of the board. */ @@ -896,7 +899,7 @@ SNAKE.Board = SNAKE.Board || (function() { if (!evt) var evt = window.event; var keyNum = (evt.which) ? evt.which : evt.keyCode; - if (me.getBoardState() === 1) { + if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { if ( !(keyNum >= 37 && keyNum <= 40) && !(keyNum === 87 || keyNum === 65 || keyNum === 83 || keyNum === 68)) {return;} // if not an arrow key, leave // This removes the listener added at the #listenerX line @@ -905,11 +908,10 @@ SNAKE.Board = SNAKE.Board || (function() { myKeyListener = function(evt) { if (!evt) var evt = window.event; var keyNum = (evt.which) ? evt.which : evt.keyCode; - - //console.log(keyNum); - if (keyNum === 32) { - if(me.getBoardState()!=0) - me.setPaused(!me.getPaused()); + + //32 = Enter + if (keyNum === 32 && me.getBoardState() != BOARD_STATE_GAME_INITIAL) { + me.setPaused(!me.getPaused()); } mySnake.handleArrowKeys(keyNum); @@ -923,7 +925,7 @@ SNAKE.Board = SNAKE.Board || (function() { mySnake.rebirth(); mySnake.handleArrowKeys(keyNum); - me.setBoardState(2); // start the game! + me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); } @@ -956,7 +958,7 @@ SNAKE.Board = SNAKE.Board || (function() { elmContainer.appendChild(elmTryAgain); elmTryAgain.style.zIndex = index; elmTryAgain.style.display = "block"; - me.setBoardState(0); + me.setBoardState(BOARD_STATE_GAME_INITIAL); }; // --------------------------------------------------------------------- @@ -975,7 +977,7 @@ SNAKE.Board = SNAKE.Board || (function() { }, false); } - me.setBoardState(0); + me.setBoardState(BOARD_STATE_GAME_INITIAL); if (config.boardContainer) { me.setBoardContainer(config.boardContainer); From 0e29cbbdc5e5e7697ada3a6361668123045204fd Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 12 Jul 2017 14:19:02 -0400 Subject: [PATCH 02/66] Removed analytics --- .idea/workspace.xml | 13 ++++++------- index.html | 11 ----------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9c53b18c..9b20fd66 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,6 @@ - - @@ -673,8 +672,8 @@ - - + + diff --git a/index.html b/index.html index f29a0194..a6953d1c 100755 --- a/index.html +++ b/index.html @@ -32,17 +32,6 @@ case 1: changeTheme('css/main-snake.css'); } } -if (navigator.onLine && window.location.hostname === 'patorjk.com') { - var _gaq = _gaq || []; - _gaq.push(['_setAccount', 'UA-3312460-1']); - _gaq.push(['_trackPageview']); - - (function() { - var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; - ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; - var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); - })(); -} From 7d1794508861c87d11436903577c04de0c997628 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 12 Jul 2017 14:19:45 -0400 Subject: [PATCH 03/66] Formatting code --- .idea/workspace.xml | 44 +- js/snake.js | 1881 ++++++++++++++++++++++--------------------- 2 files changed, 986 insertions(+), 939 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9b20fd66..a8b60527 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + - + - - - - - - + + + - + - - - + + + + + + diff --git a/js/snake.js b/js/snake.js index fc46d039..68481393 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1,8 +1,8 @@ /* -JavaScript Snake -By Patrick Gillespie -http://patorjk.com/games/snake -*/ + JavaScript Snake + By Patrick Gillespie + http://patorjk.com/games/snake + */ var BOARD_STATE_GAME_INITIAL = 0; @@ -10,985 +10,1032 @@ var BOARD_STATE_GAME_STARTING = 1; var BOARD_STATE_GAME_STARTED = 2; /** -* @module Snake -* @class SNAKE -*/ + * @module Snake + * @class SNAKE + */ var SNAKE = SNAKE || {}; /** -* @method addEventListener -* @param {Object} obj The object to add an event listener to. -* @param {String} event The event to listen for. -* @param {Function} funct The function to execute when the event is triggered. -* @param {Boolean} evtCapturing True to do event capturing, false to do event bubbling. -*/ -SNAKE.addEventListener = (function() { + * @method addEventListener + * @param {Object} obj The object to add an event listener to. + * @param {String} event The event to listen for. + * @param {Function} funct The function to execute when the event is triggered. + * @param {Boolean} evtCapturing True to do event capturing, false to do event bubbling. + */ +SNAKE.addEventListener = (function () { if (window.addEventListener) { - return function(obj, event, funct, evtCapturing) { + return function (obj, event, funct, evtCapturing) { obj.addEventListener(event, funct, evtCapturing); }; } else if (window.attachEvent) { - return function(obj, event, funct) { + return function (obj, event, funct) { obj.attachEvent("on" + event, funct); }; } })(); /** -* @method removeEventListener -* @param {Object} obj The object to remove an event listener from. -* @param {String} event The event that was listened for. -* @param {Function} funct The function that was executed when the event is triggered. -* @param {Boolean} evtCapturing True if event capturing was done, false otherwise. -*/ - -SNAKE.removeEventListener = (function() { + * @method removeEventListener + * @param {Object} obj The object to remove an event listener from. + * @param {String} event The event that was listened for. + * @param {Function} funct The function that was executed when the event is triggered. + * @param {Boolean} evtCapturing True if event capturing was done, false otherwise. + */ + +SNAKE.removeEventListener = (function () { if (window.removeEventListener) { - return function(obj, event, funct, evtCapturing) { + return function (obj, event, funct, evtCapturing) { obj.removeEventListener(event, funct, evtCapturing); }; } else if (window.detachEvent) { - return function(obj, event, funct) { + return function (obj, event, funct) { obj.detachEvent("on" + event, funct); }; } })(); /** -* 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. -*/ -SNAKE.Snake = SNAKE.Snake || (function() { - - // ------------------------------------------------------------------------- - // Private static variables and methods - // ------------------------------------------------------------------------- - - var instanceNumber = 0; - var blockPool = []; - - var SnakeBlock = function() { - this.elm = null; - this.elmStyle = null; - this.row = -1; - this.col = -1; - this.xPos = -1000; - this.yPos = -1000; - this.next = null; - this.prev = null; - }; - - // this function is adapted from the example at http://greengeckodesign.com/blog/2007/07/get-highest-z-index-in-javascript.html - function getNextHighestZIndex(myObj) { - var highestIndex = 0, - currentIndex = 0, - ii; - for (ii in myObj) { - if (myObj[ii].elm.currentStyle){ - currentIndex = parseFloat(myObj[ii].elm.style["z-index"],10); - }else if(window.getComputedStyle) { - currentIndex = parseFloat(document.defaultView.getComputedStyle(myObj[ii].elm,null).getPropertyValue("z-index"),10); - } - if(!isNaN(currentIndex) && currentIndex > highestIndex){ - highestIndex = currentIndex; + * 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. + */ +SNAKE.Snake = SNAKE.Snake || (function () { + + // ------------------------------------------------------------------------- + // Private static variables and methods + // ------------------------------------------------------------------------- + + var instanceNumber = 0; + var blockPool = []; + + var SnakeBlock = function () { + this.elm = null; + this.elmStyle = null; + this.row = -1; + this.col = -1; + this.xPos = -1000; + this.yPos = -1000; + this.next = null; + this.prev = null; + }; + + // this function is adapted from the example at http://greengeckodesign.com/blog/2007/07/get-highest-z-index-in-javascript.html + function getNextHighestZIndex(myObj) { + var highestIndex = 0, + currentIndex = 0, + ii; + for (ii in myObj) { + if (myObj[ii].elm.currentStyle) { + currentIndex = parseFloat(myObj[ii].elm.style["z-index"], 10); + } else if (window.getComputedStyle) { + currentIndex = parseFloat(document.defaultView.getComputedStyle(myObj[ii].elm, null).getPropertyValue("z-index"), 10); + } + if (!isNaN(currentIndex) && currentIndex > highestIndex) { + highestIndex = currentIndex; + } } + return (highestIndex + 1); } - return(highestIndex+1); - } - - // ------------------------------------------------------------------------- - // Contructor + public and private definitions - // ------------------------------------------------------------------------- - - /* - config options: - playingBoard - the SnakeBoard that this snake belongs too. - startRow - The row the snake should start on. - startCol - The column the snake should start on. - */ - return function(config) { - - if (!config||!config.playingBoard) {return;} - - // ----- private variables ----- - - var me = this, - playingBoard = config.playingBoard, - myId = instanceNumber++, - growthIncr = 5, - moveQueue = [], // a queue that holds the next moves of the snake - currentDirection = 1, // 0: up, 1: left, 2: down, 3: right - columnShift = [0, 1, 0, -1], - rowShift = [-1, 0, 1, 0], - xPosShift = [], - yPosShift = [], - snakeSpeed = 75, - isDead = false, - isPaused = false; - function getMode (mode, speed) { - document.getElementById(mode).addEventListener('click', function () { snakeSpeed = speed; }); -} + + // ------------------------------------------------------------------------- + // Contructor + public and private definitions + // ------------------------------------------------------------------------- + + /* + config options: + playingBoard - the SnakeBoard that this snake belongs too. + startRow - The row the snake should start on. + startCol - The column the snake should start on. + */ + return function (config) { + + if (!config || !config.playingBoard) { + return; + } + + // ----- private variables ----- + + var me = this, + playingBoard = config.playingBoard, + myId = instanceNumber++, + growthIncr = 5, + moveQueue = [], // a queue that holds the next moves of the snake + currentDirection = 1, // 0: up, 1: left, 2: down, 3: right + columnShift = [0, 1, 0, -1], + rowShift = [-1, 0, 1, 0], + xPosShift = [], + yPosShift = [], + snakeSpeed = 75, + isDead = false, + isPaused = false; + + function getMode(mode, speed) { + document.getElementById(mode).addEventListener('click', function () { + snakeSpeed = speed; + }); + } + getMode('Easy', 100); getMode('Medium', 75); getMode('Difficult', 50); - // ----- public variables ----- - me.snakeBody = {}; - me.snakeBody["b0"] = new SnakeBlock(); // create snake head - me.snakeBody["b0"].row = config.startRow || 1; - me.snakeBody["b0"].col = config.startCol || 1; - me.snakeBody["b0"].xPos = me.snakeBody["b0"].row * playingBoard.getBlockWidth(); - me.snakeBody["b0"].yPos = me.snakeBody["b0"].col * playingBoard.getBlockHeight(); - me.snakeBody["b0"].elm = createSnakeElement(); - me.snakeBody["b0"].elmStyle = me.snakeBody["b0"].elm.style; - playingBoard.getBoardContainer().appendChild( me.snakeBody["b0"].elm ); - me.snakeBody["b0"].elm.style.left = me.snakeBody["b0"].xPos + "px"; - me.snakeBody["b0"].elm.style.top = me.snakeBody["b0"].yPos + "px"; - me.snakeBody["b0"].next = me.snakeBody["b0"]; - me.snakeBody["b0"].prev = me.snakeBody["b0"]; - - me.snakeLength = 1; - me.snakeHead = me.snakeBody["b0"]; - me.snakeTail = me.snakeBody["b0"]; - me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,''); - me.snakeHead.elm.className += " snake-snakebody-alive"; - - // ----- private methods ----- - - function createSnakeElement() { - var tempNode = document.createElement("div"); - tempNode.className = "snake-snakebody-block"; - tempNode.style.left = "-1000px"; - tempNode.style.top = "-1000px"; - tempNode.style.width = playingBoard.getBlockWidth() + "px"; - tempNode.style.height = playingBoard.getBlockHeight() + "px"; - return tempNode; - } - - function createBlocks(num) { - var tempBlock; - var tempNode = createSnakeElement(); + // ----- public variables ----- + me.snakeBody = {}; + me.snakeBody["b0"] = new SnakeBlock(); // create snake head + me.snakeBody["b0"].row = config.startRow || 1; + me.snakeBody["b0"].col = config.startCol || 1; + me.snakeBody["b0"].xPos = me.snakeBody["b0"].row * playingBoard.getBlockWidth(); + me.snakeBody["b0"].yPos = me.snakeBody["b0"].col * playingBoard.getBlockHeight(); + me.snakeBody["b0"].elm = createSnakeElement(); + me.snakeBody["b0"].elmStyle = me.snakeBody["b0"].elm.style; + playingBoard.getBoardContainer().appendChild(me.snakeBody["b0"].elm); + me.snakeBody["b0"].elm.style.left = me.snakeBody["b0"].xPos + "px"; + me.snakeBody["b0"].elm.style.top = me.snakeBody["b0"].yPos + "px"; + me.snakeBody["b0"].next = me.snakeBody["b0"]; + me.snakeBody["b0"].prev = me.snakeBody["b0"]; + + me.snakeLength = 1; + me.snakeHead = me.snakeBody["b0"]; + me.snakeTail = me.snakeBody["b0"]; + me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/, ''); + me.snakeHead.elm.className += " snake-snakebody-alive"; + + // ----- private methods ----- + + function createSnakeElement() { + var tempNode = document.createElement("div"); + tempNode.className = "snake-snakebody-block"; + tempNode.style.left = "-1000px"; + tempNode.style.top = "-1000px"; + tempNode.style.width = playingBoard.getBlockWidth() + "px"; + tempNode.style.height = playingBoard.getBlockHeight() + "px"; + return tempNode; + } + + function createBlocks(num) { + var tempBlock; + var tempNode = createSnakeElement(); + + for (var ii = 1; ii < num; ii++) { + tempBlock = new SnakeBlock(); + tempBlock.elm = tempNode.cloneNode(true); + tempBlock.elmStyle = tempBlock.elm.style; + playingBoard.getBoardContainer().appendChild(tempBlock.elm); + blockPool[blockPool.length] = tempBlock; + } - for (var ii = 1; ii < num; ii++){ tempBlock = new SnakeBlock(); - tempBlock.elm = tempNode.cloneNode(true); - tempBlock.elmStyle = tempBlock.elm.style; - playingBoard.getBoardContainer().appendChild( tempBlock.elm ); + tempBlock.elm = tempNode; + playingBoard.getBoardContainer().appendChild(tempBlock.elm); blockPool[blockPool.length] = tempBlock; } - - tempBlock = new SnakeBlock(); - tempBlock.elm = tempNode; - playingBoard.getBoardContainer().appendChild( tempBlock.elm ); - blockPool[blockPool.length] = tempBlock; - } - - // ----- public methods ----- - - me.setPaused = function(val) { - isPaused = val; - }; - me.getPaused = function() { - return isPaused; - }; - - /** - * 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;} - - var snakeLength = me.snakeLength; - var lastMove = moveQueue[0] || currentDirection; - - switch (keyNum) { - - case 37: - case 65: - if ( lastMove !== 1 || snakeLength === 1 ) { - moveQueue.unshift(3); //SnakeDirection = 3; - } - break; - case 38: - case 87: - if ( lastMove !== 2 || snakeLength === 1 ) { - moveQueue.unshift(0);//SnakeDirection = 0; - } - break; - case 39: - case 68: - if ( lastMove !== 3 || snakeLength === 1 ) { - moveQueue.unshift(1); //SnakeDirection = 1; - } - break; - case 40: - case 83: - if ( lastMove !== 0 || snakeLength === 1 ) { - moveQueue.unshift(2);//SnakeDirection = 2; + + // ----- public methods ----- + + me.setPaused = function (val) { + isPaused = val; + }; + me.getPaused = function () { + return isPaused; + }; + + /** + * 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; + } + + var snakeLength = me.snakeLength; + var lastMove = moveQueue[0] || currentDirection; + + switch (keyNum) { + + case 37: + case 65: + if (lastMove !== 1 || snakeLength === 1) { + moveQueue.unshift(3); //SnakeDirection = 3; + } + break; + case 38: + case 87: + if (lastMove !== 2 || snakeLength === 1) { + moveQueue.unshift(0);//SnakeDirection = 0; + } + break; + case 39: + case 68: + if (lastMove !== 3 || snakeLength === 1) { + moveQueue.unshift(1); //SnakeDirection = 1; + } + break; + case 40: + case 83: + 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 () { + + var oldHead = 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]) { + grid[newHead.row][newHead.col] = 0; + } + + if (moveQueue.length) { + myDirection = currentDirection = moveQueue.pop(); + } + + newHead.col = oldHead.col + columnShift[myDirection]; + newHead.row = oldHead.row + rowShift[myDirection]; + newHead.xPos = oldHead.xPos + xPosShift[myDirection]; + newHead.yPos = oldHead.yPos + yPosShift[myDirection]; + + if (!newHead.elmStyle) { + newHead.elmStyle = newHead.elm.style; + } + + newHead.elmStyle.left = newHead.xPos + "px"; + newHead.elmStyle.top = newHead.yPos + "px"; + + // check the new spot the snake moved into + + if (grid[newHead.row][newHead.col] === 0) { + grid[newHead.row][newHead.col] = 1; + setTimeout(function () { + me.go(); + }, snakeSpeed); + } else if (grid[newHead.row][newHead.col] > 0) { + me.handleDeath(); + } else if (grid[newHead.row][newHead.col] === playingBoard.getGridFoodValue()) { + grid[newHead.row][newHead.col] = 1; + me.eatFood(); + setTimeout(function () { + me.go(); + }, snakeSpeed); + } + }; + + /** + * This method is called when it is determined that the snake has eaten some food. + * @method eatFood + */ + me.eatFood = function () { + if (blockPool.length <= growthIncr) { + createBlocks(growthIncr * 2); + } + var blocks = blockPool.splice(0, growthIncr); + + var ii = blocks.length, + index, + prevNode = me.snakeTail; + while (ii--) { + index = "b" + me.snakeLength++; + me.snakeBody[index] = blocks[ii]; + me.snakeBody[index].prev = prevNode; + me.snakeBody[index].elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/, '') + me.snakeBody[index].elm.className += " snake-snakebody-alive"; + prevNode.next = me.snakeBody[index]; + prevNode = me.snakeBody[index]; + } + me.snakeTail = me.snakeBody[index]; + me.snakeTail.next = me.snakeHead; + me.snakeHead.prev = me.snakeTail; + + playingBoard.foodEaten(); + }; + + /** + * This method handles what happens when the snake dies. + * @method handleDeath + */ + me.handleDeath = function () { + function recordScore() { + var highScore = localStorage.jsSnakeHighScore; + if (highScore == undefined) localStorage.setItem('jsSnakeHighScore', me.snakeLength); + if (me.snakeLength > highScore) { + alert('Congratulations! You have beaten your previous high score, which was ' + highScore + '.'); + localStorage.setItem('jsSnakeHighScore', me.snakeLength); } - break; - } + } + + recordScore(); + me.snakeHead.elm.style.zIndex = getNextHighestZIndex(me.snakeBody); + me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-alive\b/, '') + me.snakeHead.elm.className += " snake-snakebody-dead"; + + isDead = true; + playingBoard.handleDeath(); + moveQueue.length = 0; + }; + + /** + * This method sets a flag that lets the snake be alive again. + * @method rebirth + */ + me.rebirth = function () { + isDead = false; + }; + + /** + * This method reset the snake so it is ready for a new game. + * @method reset + */ + me.reset = function () { + if (isDead === false) { + return; + } + + var blocks = [], + curNode = me.snakeHead.next, + nextNode; + while (curNode !== me.snakeHead) { + nextNode = curNode.next; + curNode.prev = null; + curNode.next = null; + blocks.push(curNode); + curNode = nextNode; + } + me.snakeHead.next = me.snakeHead; + me.snakeHead.prev = me.snakeHead; + me.snakeTail = me.snakeHead; + me.snakeLength = 1; + + for (var ii = 0; ii < blocks.length; ii++) { + blocks[ii].elm.style.left = "-1000px"; + blocks[ii].elm.style.top = "-1000px"; + blocks[ii].elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/, '') + blocks[ii].elm.className += " snake-snakebody-alive"; + } + + blockPool.concat(blocks); + me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/, '') + me.snakeHead.elm.className += " snake-snakebody-alive"; + me.snakeHead.row = config.startRow || 1; + me.snakeHead.col = config.startCol || 1; + me.snakeHead.xPos = me.snakeHead.row * playingBoard.getBlockWidth(); + me.snakeHead.yPos = me.snakeHead.col * playingBoard.getBlockHeight(); + me.snakeHead.elm.style.left = me.snakeHead.xPos + "px"; + me.snakeHead.elm.style.top = me.snakeHead.yPos + "px"; + }; + + // --------------------------------------------------------------------- + // Initialize + // --------------------------------------------------------------------- + createBlocks(growthIncr * 2); + xPosShift[0] = 0; + xPosShift[1] = playingBoard.getBlockWidth(); + xPosShift[2] = 0; + xPosShift[3] = -1 * playingBoard.getBlockWidth(); + + yPosShift[0] = -1 * playingBoard.getBlockHeight(); + yPosShift[1] = 0; + yPosShift[2] = playingBoard.getBlockHeight(); + yPosShift[3] = 0; }; - - /** - * 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() { - - var oldHead = me.snakeHead, - newHead = me.snakeTail, - myDirection = currentDirection, - grid = playingBoard.grid; // cache grid for quicker lookup - - if (isPaused === true) { - setTimeout(function(){me.go();}, snakeSpeed); + })(); + +/** + * This class manages the food which the snake will eat. + * @class Food + * @constructor + * @namespace SNAKE + * @param {Object} config The configuration object for the class. Contains playingBoard (the SNAKE.Board that this food resides in). + */ + +SNAKE.Food = SNAKE.Food || (function () { + + // ------------------------------------------------------------------------- + // Private static variables and methods + // ------------------------------------------------------------------------- + + var instanceNumber = 0; + + function getRandomPosition(x, y) { + return Math.floor(Math.random() * (y + 1 - x)) + x; + } + + // ------------------------------------------------------------------------- + // Contructor + public and private definitions + // ------------------------------------------------------------------------- + + /* + config options: + playingBoard - the SnakeBoard that this object belongs too. + */ + return function (config) { + + if (!config || !config.playingBoard) { return; } - - me.snakeTail = newHead.prev; - me.snakeHead = newHead; - - // clear the old board position - if ( grid[newHead.row] && grid[newHead.row][newHead.col] ) { - grid[newHead.row][newHead.col] = 0; - } - - if (moveQueue.length){ - myDirection = currentDirection = moveQueue.pop(); - } - - newHead.col = oldHead.col + columnShift[myDirection]; - newHead.row = oldHead.row + rowShift[myDirection]; - newHead.xPos = oldHead.xPos + xPosShift[myDirection]; - newHead.yPos = oldHead.yPos + yPosShift[myDirection]; - - if ( !newHead.elmStyle ) { - newHead.elmStyle = newHead.elm.style; - } - - newHead.elmStyle.left = newHead.xPos + "px"; - newHead.elmStyle.top = newHead.yPos + "px"; - - // check the new spot the snake moved into - - if (grid[newHead.row][newHead.col] === 0) { - grid[newHead.row][newHead.col] = 1; - setTimeout(function(){me.go();}, snakeSpeed); - } else if (grid[newHead.row][newHead.col] > 0) { - me.handleDeath(); - } else if (grid[newHead.row][newHead.col] === playingBoard.getGridFoodValue()) { - grid[newHead.row][newHead.col] = 1; - me.eatFood(); - setTimeout(function(){me.go();}, snakeSpeed); - } - }; - - /** - * This method is called when it is determined that the snake has eaten some food. - * @method eatFood - */ - me.eatFood = function() { - if (blockPool.length <= growthIncr) { - createBlocks(growthIncr*2); - } - var blocks = blockPool.splice(0, growthIncr); - - var ii = blocks.length, - index, - prevNode = me.snakeTail; - while (ii--) { - index = "b" + me.snakeLength++; - me.snakeBody[index] = blocks[ii]; - me.snakeBody[index].prev = prevNode; - me.snakeBody[index].elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,'') - me.snakeBody[index].elm.className += " snake-snakebody-alive"; - prevNode.next = me.snakeBody[index]; - prevNode = me.snakeBody[index]; - } - me.snakeTail = me.snakeBody[index]; - me.snakeTail.next = me.snakeHead; - me.snakeHead.prev = me.snakeTail; - playingBoard.foodEaten(); - }; - - /** - * This method handles what happens when the snake dies. - * @method handleDeath - */ - me.handleDeath = function() { - function recordScore () { - var highScore = localStorage.jsSnakeHighScore; - if (highScore == undefined) localStorage.setItem('jsSnakeHighScore', me.snakeLength); - if (me.snakeLength > highScore) { - alert('Congratulations! You have beaten your previous high score, which was ' + highScore + '.'); - localStorage.setItem('jsSnakeHighScore', me.snakeLength); + // ----- private variables ----- + + var me = this; + var playingBoard = config.playingBoard; + var fRow, fColumn; + var myId = instanceNumber++; + + var elmFood = document.createElement("div"); + elmFood.setAttribute("id", "snake-food-" + myId); + elmFood.className = "snake-food-block"; + elmFood.style.width = playingBoard.getBlockWidth() + "px"; + elmFood.style.height = playingBoard.getBlockHeight() + "px"; + elmFood.style.left = "-1000px"; + elmFood.style.top = "-1000px"; + playingBoard.getBoardContainer().appendChild(elmFood); + + // ----- public methods ----- + + /** + * @method getFoodElement + * @return {DOM Element} The div the represents the food. + */ + me.getFoodElement = function () { + return elmFood; + }; + + /** + * Randomly places the food onto an available location on the playing board. + * @method randomlyPlaceFood + */ + me.randomlyPlaceFood = function () { + // if there exist some food, clear its presence from the board + if (playingBoard.grid[fRow] && playingBoard.grid[fRow][fColumn] === playingBoard.getGridFoodValue()) { + playingBoard.grid[fRow][fColumn] = 0; } -} - recordScore(); - me.snakeHead.elm.style.zIndex = getNextHighestZIndex(me.snakeBody); - me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-alive\b/,'') - me.snakeHead.elm.className += " snake-snakebody-dead"; - - isDead = true; - playingBoard.handleDeath(); - moveQueue.length = 0; - }; - /** - * This method sets a flag that lets the snake be alive again. - * @method rebirth - */ - me.rebirth = function() { - isDead = false; + var row = 0, col = 0, numTries = 0; + + var maxRows = playingBoard.grid.length - 1; + var maxCols = playingBoard.grid[0].length - 1; + + while (playingBoard.grid[row][col] !== 0) { + row = getRandomPosition(1, maxRows); + col = getRandomPosition(1, maxCols); + + // in some cases there may not be any room to put food anywhere + // instead of freezing, exit out + numTries++; + if (numTries > 20000) { + row = -1; + col = -1; + break; + } + } + + playingBoard.grid[row][col] = playingBoard.getGridFoodValue(); + fRow = row; + fColumn = col; + elmFood.style.top = row * playingBoard.getBlockHeight() + "px"; + elmFood.style.left = col * playingBoard.getBlockWidth() + "px"; + }; }; - - /** - * This method reset the snake so it is ready for a new game. - * @method reset - */ - me.reset = function() { - if (isDead === false) {return;} - - var blocks = [], - curNode = me.snakeHead.next, - nextNode; - while (curNode !== me.snakeHead) { - nextNode = curNode.next; - curNode.prev = null; - curNode.next = null; - blocks.push(curNode); - curNode = nextNode; + })(); + +/** + * This class manages playing board for the game. + * @class Board + * @constructor + * @namespace SNAKE + * @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. + */ + +SNAKE.Board = SNAKE.Board || (function () { + + // ------------------------------------------------------------------------- + // Private static variables and methods + // ------------------------------------------------------------------------- + + var instanceNumber = 0; + + // this function is adapted from the example at http://greengeckodesign.com/blog/2007/07/get-highest-z-index-in-javascript.html + function getNextHighestZIndex(myObj) { + var highestIndex = 0, + currentIndex = 0, + ii; + for (ii in myObj) { + if (myObj[ii].elm.currentStyle) { + currentIndex = parseFloat(myObj[ii].elm.style["z-index"], 10); + } else if (window.getComputedStyle) { + currentIndex = parseFloat(document.defaultView.getComputedStyle(myObj[ii].elm, null).getPropertyValue("z-index"), 10); + } + if (!isNaN(currentIndex) && currentIndex > highestIndex) { + highestIndex = currentIndex; + } } - me.snakeHead.next = me.snakeHead; - me.snakeHead.prev = me.snakeHead; - me.snakeTail = me.snakeHead; - me.snakeLength = 1; - - for (var ii = 0; ii < blocks.length; ii++) { - blocks[ii].elm.style.left = "-1000px"; - blocks[ii].elm.style.top = "-1000px"; - blocks[ii].elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,'') - blocks[ii].elm.className += " snake-snakebody-alive"; + return (highestIndex + 1); + } + + /* + This function returns the width of the available screen real estate that we have + */ + function getClientWidth() { + var myWidth = 0; + if (typeof window.innerWidth === "number") { + myWidth = window.innerWidth;//Non-IE + } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) { + myWidth = document.documentElement.clientWidth;//IE 6+ in 'standards compliant mode' + } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) { + myWidth = document.body.clientWidth;//IE 4 compatible } - - blockPool.concat(blocks); - me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,'') - me.snakeHead.elm.className += " snake-snakebody-alive"; - me.snakeHead.row = config.startRow || 1; - me.snakeHead.col = config.startCol || 1; - me.snakeHead.xPos = me.snakeHead.row * playingBoard.getBlockWidth(); - me.snakeHead.yPos = me.snakeHead.col * playingBoard.getBlockHeight(); - me.snakeHead.elm.style.left = me.snakeHead.xPos + "px"; - me.snakeHead.elm.style.top = me.snakeHead.yPos + "px"; - }; - - // --------------------------------------------------------------------- - // Initialize - // --------------------------------------------------------------------- - createBlocks(growthIncr*2); - xPosShift[0] = 0; - xPosShift[1] = playingBoard.getBlockWidth(); - xPosShift[2] = 0; - xPosShift[3] = -1 * playingBoard.getBlockWidth(); - - yPosShift[0] = -1 * playingBoard.getBlockHeight(); - yPosShift[1] = 0; - yPosShift[2] = playingBoard.getBlockHeight(); - yPosShift[3] = 0; - }; -})(); + return myWidth; + } -/** -* This class manages the food which the snake will eat. -* @class Food -* @constructor -* @namespace SNAKE -* @param {Object} config The configuration object for the class. Contains playingBoard (the SNAKE.Board that this food resides in). -*/ - -SNAKE.Food = SNAKE.Food || (function() { - - // ------------------------------------------------------------------------- - // Private static variables and methods - // ------------------------------------------------------------------------- - - var instanceNumber = 0; - - function getRandomPosition(x, y){ - return Math.floor(Math.random()*(y+1-x)) + x; - } - - // ------------------------------------------------------------------------- - // Contructor + public and private definitions - // ------------------------------------------------------------------------- - - /* - config options: - playingBoard - the SnakeBoard that this object belongs too. - */ - return function(config) { - - if (!config||!config.playingBoard) {return;} - - // ----- private variables ----- - - var me = this; - var playingBoard = config.playingBoard; - var fRow, fColumn; - var myId = instanceNumber++; - - var elmFood = document.createElement("div"); - elmFood.setAttribute("id", "snake-food-"+myId); - elmFood.className = "snake-food-block"; - elmFood.style.width = playingBoard.getBlockWidth() + "px"; - elmFood.style.height = playingBoard.getBlockHeight() + "px"; - elmFood.style.left = "-1000px"; - elmFood.style.top = "-1000px"; - playingBoard.getBoardContainer().appendChild(elmFood); - - // ----- public methods ----- - - /** - * @method getFoodElement - * @return {DOM Element} The div the represents the food. - */ - me.getFoodElement = function() { - return elmFood; - }; - - /** - * Randomly places the food onto an available location on the playing board. - * @method randomlyPlaceFood - */ - me.randomlyPlaceFood = function() { - // if there exist some food, clear its presence from the board - if (playingBoard.grid[fRow] && playingBoard.grid[fRow][fColumn] === playingBoard.getGridFoodValue()){ - playingBoard.grid[fRow][fColumn] = 0; + /* + This function returns the height of the available screen real estate that we have + */ + function getClientHeight() { + var myHeight = 0; + if (typeof window.innerHeight === "number") { + myHeight = window.innerHeight;//Non-IE + } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) { + myHeight = document.documentElement.clientHeight;//IE 6+ in 'standards compliant mode' + } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) { + myHeight = document.body.clientHeight;//IE 4 compatible } + return myHeight; + } + + // ------------------------------------------------------------------------- + // Contructor + public and private definitions + // ------------------------------------------------------------------------- + + return function (inputConfig) { + + // --- private variables --- + var me = this, + myId = instanceNumber++, + config = inputConfig || {}, + MAX_BOARD_COLS = 250, + MAX_BOARD_ROWS = 250, + blockWidth = 20, + blockHeight = 20, + GRID_FOOD_VALUE = -1, // the value of a spot on the board that represents snake food, MUST BE NEGATIVE + myFood, + mySnake, + boardState = 1, // 0: in active; 1: awaiting game start; 2: playing game + myKeyListener, + isPaused = false,//note: both the board and the snake can be paused + // Board components + elmContainer, elmPlayingField, elmAboutPanel, elmLengthPanel, elmWelcome, elmTryAgain, elmPauseScreen; + + // --- public variables --- + me.grid = []; + + // --------------------------------------------------------------------- + // private functions + // --------------------------------------------------------------------- + + function createBoardElements() { + elmPlayingField = document.createElement("div"); + elmPlayingField.setAttribute("id", "playingField"); + elmPlayingField.className = "snake-playing-field"; + + SNAKE.addEventListener(elmPlayingField, "click", function () { + elmContainer.focus(); + }, false); - var row = 0, col = 0, numTries = 0; - - var maxRows = playingBoard.grid.length-1; - var maxCols = playingBoard.grid[0].length-1; - - while (playingBoard.grid[row][col] !== 0){ - row = getRandomPosition(1, maxRows); - col = getRandomPosition(1, maxCols); - - // in some cases there may not be any room to put food anywhere - // instead of freezing, exit out - numTries++; - if (numTries > 20000){ - row = -1; - col = -1; - break; - } + elmPauseScreen = document.createElement("div"); + elmPauseScreen.className = "snake-pause-screen"; + elmPauseScreen.innerHTML = "
[Paused]

Press [space] to unpause.

"; + + elmAboutPanel = document.createElement("div"); + elmAboutPanel.className = "snake-panel-component"; + + elmLengthPanel = document.createElement("div"); + elmLengthPanel.className = "snake-panel-component"; + elmLengthPanel.innerHTML = "Length: 1"; + + elmWelcome = createWelcomeElement(); + elmTryAgain = createTryAgainElement(); + + SNAKE.addEventListener(elmContainer, "keyup", function (evt) { + if (!evt) var evt = window.event; + evt.cancelBubble = true; + if (evt.stopPropagation) { + evt.stopPropagation(); + } + if (evt.preventDefault) { + evt.preventDefault(); + } + return false; + }, false); + + elmContainer.className = "snake-game-container"; + + elmPauseScreen.style.zIndex = 10000; + elmContainer.appendChild(elmPauseScreen); + elmContainer.appendChild(elmPlayingField); + elmContainer.appendChild(elmAboutPanel); + elmContainer.appendChild(elmLengthPanel); + elmContainer.appendChild(elmWelcome); + elmContainer.appendChild(elmTryAgain); + + mySnake = new SNAKE.Snake({playingBoard: me, startRow: 2, startCol: 2}); + myFood = new SNAKE.Food({playingBoard: me}); + + elmWelcome.style.zIndex = 1000; } - playingBoard.grid[row][col] = playingBoard.getGridFoodValue(); - fRow = row; - fColumn = col; - elmFood.style.top = row * playingBoard.getBlockHeight() + "px"; - elmFood.style.left = col * playingBoard.getBlockWidth() + "px"; - }; - }; -})(); + function maxBoardWidth() { + return MAX_BOARD_COLS * me.getBlockWidth(); + } -/** -* This class manages playing board for the game. -* @class Board -* @constructor -* @namespace SNAKE -* @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. -*/ - -SNAKE.Board = SNAKE.Board || (function() { - - // ------------------------------------------------------------------------- - // Private static variables and methods - // ------------------------------------------------------------------------- - - var instanceNumber = 0; - - // this function is adapted from the example at http://greengeckodesign.com/blog/2007/07/get-highest-z-index-in-javascript.html - function getNextHighestZIndex(myObj) { - var highestIndex = 0, - currentIndex = 0, - ii; - for (ii in myObj) { - if (myObj[ii].elm.currentStyle){ - currentIndex = parseFloat(myObj[ii].elm.style["z-index"],10); - }else if(window.getComputedStyle) { - currentIndex = parseFloat(document.defaultView.getComputedStyle(myObj[ii].elm,null).getPropertyValue("z-index"),10); + function maxBoardHeight() { + return MAX_BOARD_ROWS * me.getBlockHeight(); } - if(!isNaN(currentIndex) && currentIndex > highestIndex){ - highestIndex = currentIndex; + + function createWelcomeElement() { + var tmpElm = document.createElement("div"); + tmpElm.id = "sbWelcome" + myId; + tmpElm.className = "snake-welcome-dialog"; + + var welcomeTxt = document.createElement("div"); + var fullScreenText = ""; + if (config.fullScreen) { + fullScreenText = "On Windows, press F11 to play in Full Screen mode."; + } + welcomeTxt.innerHTML = "JavaScript Snake

Use the arrow keys on your keyboard to play the game. " + fullScreenText + "

"; + var welcomeStart = document.createElement("button"); + welcomeStart.appendChild(document.createTextNode("Play Game")); + var loadGame = function () { + SNAKE.removeEventListener(window, "keyup", kbShortcut, false); + tmpElm.style.display = "none"; + me.setBoardState(BOARD_STATE_GAME_STARTING); + me.getBoardContainer().focus(); + }; + + var kbShortcut = function (evt) { + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + //32 = Space, 13 = Enter + if (keyNum === 32 || keyNum === 13) { + loadGame(); + } + }; + SNAKE.addEventListener(window, "keyup", kbShortcut, false); + SNAKE.addEventListener(welcomeStart, "click", loadGame, false); + + tmpElm.appendChild(welcomeTxt); + tmpElm.appendChild(welcomeStart); + return tmpElm; } - } - return(highestIndex+1); - } - /* - This function returns the width of the available screen real estate that we have - */ - function getClientWidth(){ - var myWidth = 0; - if( typeof window.innerWidth === "number" ) { - myWidth = window.innerWidth;//Non-IE - } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { - myWidth = document.documentElement.clientWidth;//IE 6+ in 'standards compliant mode' - } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { - myWidth = document.body.clientWidth;//IE 4 compatible - } - return myWidth; - } - /* - This function returns the height of the available screen real estate that we have - */ - function getClientHeight(){ - var myHeight = 0; - if( typeof window.innerHeight === "number" ) { - myHeight = window.innerHeight;//Non-IE - } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { - myHeight = document.documentElement.clientHeight;//IE 6+ in 'standards compliant mode' - } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { - myHeight = document.body.clientHeight;//IE 4 compatible - } - return myHeight; - } + function createTryAgainElement() { + var tmpElm = document.createElement("div"); + tmpElm.id = "sbTryAgain" + myId; + tmpElm.className = "snake-try-again-dialog"; - // ------------------------------------------------------------------------- - // Contructor + public and private definitions - // ------------------------------------------------------------------------- - - return function(inputConfig) { - - // --- private variables --- - var me = this, - myId = instanceNumber++, - config = inputConfig || {}, - MAX_BOARD_COLS = 250, - MAX_BOARD_ROWS = 250, - blockWidth = 20, - blockHeight = 20, - GRID_FOOD_VALUE = -1, // the value of a spot on the board that represents snake food, MUST BE NEGATIVE - myFood, - mySnake, - boardState = 1, // 0: in active; 1: awaiting game start; 2: playing game - myKeyListener, - isPaused = false,//note: both the board and the snake can be paused - // Board components - elmContainer, elmPlayingField, elmAboutPanel, elmLengthPanel, elmWelcome, elmTryAgain, elmPauseScreen; - - // --- public variables --- - me.grid = []; - - // --------------------------------------------------------------------- - // private functions - // --------------------------------------------------------------------- - - function createBoardElements() { - elmPlayingField = document.createElement("div"); - elmPlayingField.setAttribute("id", "playingField"); - elmPlayingField.className = "snake-playing-field"; - - SNAKE.addEventListener(elmPlayingField, "click", function() { - elmContainer.focus(); - }, false); - - elmPauseScreen = document.createElement("div"); - elmPauseScreen.className = "snake-pause-screen"; - elmPauseScreen.innerHTML = "
[Paused]

Press [space] to unpause.

"; - - elmAboutPanel = document.createElement("div"); - elmAboutPanel.className = "snake-panel-component"; - - elmLengthPanel = document.createElement("div"); - elmLengthPanel.className = "snake-panel-component"; - elmLengthPanel.innerHTML = "Length: 1"; - - elmWelcome = createWelcomeElement(); - elmTryAgain = createTryAgainElement(); - - SNAKE.addEventListener( elmContainer, "keyup", function(evt) { - if (!evt) var evt = window.event; - evt.cancelBubble = true; - if (evt.stopPropagation) {evt.stopPropagation();} - if (evt.preventDefault) {evt.preventDefault();} - return false; - }, false); - - elmContainer.className = "snake-game-container"; - - elmPauseScreen.style.zIndex = 10000; - elmContainer.appendChild(elmPauseScreen); - elmContainer.appendChild(elmPlayingField); - elmContainer.appendChild(elmAboutPanel); - elmContainer.appendChild(elmLengthPanel); - elmContainer.appendChild(elmWelcome); - elmContainer.appendChild(elmTryAgain); - - mySnake = new SNAKE.Snake({playingBoard:me,startRow:2,startCol:2}); - myFood = new SNAKE.Food({playingBoard: me}); - - elmWelcome.style.zIndex = 1000; - } - function maxBoardWidth() { - return MAX_BOARD_COLS * me.getBlockWidth(); - } - function maxBoardHeight() { - return MAX_BOARD_ROWS * me.getBlockHeight(); - } - - function createWelcomeElement() { - var tmpElm = document.createElement("div"); - tmpElm.id = "sbWelcome" + myId; - tmpElm.className = "snake-welcome-dialog"; - - var welcomeTxt = document.createElement("div"); - var fullScreenText = ""; - if (config.fullScreen) { - fullScreenText = "On Windows, press F11 to play in Full Screen mode."; + var tryAgainTxt = document.createElement("div"); + tryAgainTxt.innerHTML = "JavaScript Snake

You died :(.

"; + var tryAgainStart = document.createElement("button"); + tryAgainStart.appendChild(document.createTextNode("Play Again?")); + + var reloadGame = function () { + tmpElm.style.display = "none"; + me.resetBoard(); + me.setBoardState(BOARD_STATE_GAME_STARTING); + me.getBoardContainer().focus(); + }; + + var kbTryAgainShortcut = function (evt) { + if (me.getBoardState() !== BOARD_STATE_INITIAL || tmpElm.style.display !== "block") { + return; + } + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + //32 = Space, 13 = Enter + if (keyNum === 32 || keyNum === 13) { + reloadGame(); + } + }; + SNAKE.addEventListener(window, "keyup", kbTryAgainShortcut, true); + + SNAKE.addEventListener(tryAgainStart, "click", reloadGame, false); + tmpElm.appendChild(tryAgainTxt); + tmpElm.appendChild(tryAgainStart); + return tmpElm; } - welcomeTxt.innerHTML = "JavaScript Snake

Use the arrow keys on your keyboard to play the game. " + fullScreenText + "

"; - var welcomeStart = document.createElement("button"); - welcomeStart.appendChild(document.createTextNode("Play Game")); - var loadGame = function() { - SNAKE.removeEventListener(window, "keyup", kbShortcut, false); - tmpElm.style.display = "none"; - me.setBoardState(BOARD_STATE_GAME_STARTING); - me.getBoardContainer().focus(); - }; - - var kbShortcut = function(evt) { - if (!evt) var evt = window.event; - var keyNum = (evt.which) ? evt.which : evt.keyCode; - //32 = Space, 13 = Enter - if (keyNum === 32 || keyNum === 13) { - loadGame(); + + // --------------------------------------------------------------------- + // public functions + // --------------------------------------------------------------------- + + me.setPaused = function (val) { + isPaused = val; + mySnake.setPaused(val); + if (isPaused) { + elmPauseScreen.style.display = "block"; + } else { + elmPauseScreen.style.display = "none"; } }; - SNAKE.addEventListener(window, "keyup", kbShortcut, false); - SNAKE.addEventListener(welcomeStart, "click", loadGame, false); - - tmpElm.appendChild(welcomeTxt); - tmpElm.appendChild(welcomeStart); - return tmpElm; - } - - function createTryAgainElement() { - var tmpElm = document.createElement("div"); - tmpElm.id = "sbTryAgain" + myId; - tmpElm.className = "snake-try-again-dialog"; - - var tryAgainTxt = document.createElement("div"); - tryAgainTxt.innerHTML = "JavaScript Snake

You died :(.

"; - var tryAgainStart = document.createElement("button"); - tryAgainStart.appendChild( document.createTextNode("Play Again?")); - - var reloadGame = function() { - tmpElm.style.display = "none"; - me.resetBoard(); - me.setBoardState(BOARD_STATE_GAME_STARTING); - me.getBoardContainer().focus(); + me.getPaused = function () { + return isPaused; + }; + + /** + * Resets the playing board for a new game. + * @method resetBoard + */ + me.resetBoard = function () { + SNAKE.removeEventListener(elmContainer, "keydown", myKeyListener, false); + mySnake.reset(); + elmLengthPanel.innerHTML = "Length: 1"; + me.setupPlayingField(); + }; + /** + * Gets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. + * @return {Number} The state of the board. + */ + me.getBoardState = function () { + return boardState; }; - - var kbTryAgainShortcut = function(evt) { - if (me.getBoardState() !== BOARD_STATE_INITIAL || tmpElm.style.display !== "block") {return;} - if (!evt) var evt = window.event; - var keyNum = (evt.which) ? evt.which : evt.keyCode; - //32 = Space, 13 = Enter - if (keyNum === 32 || keyNum === 13) { - reloadGame(); + /** + * Sets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. + * @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 () { + return GRID_FOOD_VALUE; + }; + /** + * @method getPlayingFieldElement + * @return {DOM Element} The div representing the playing field (this is where the snake can move). + */ + me.getPlayingFieldElement = function () { + return elmPlayingField; + }; + /** + * @method setBoardContainer + * @param {DOM Element or String} myContainer Sets the container element for the game. + */ + me.setBoardContainer = function (myContainer) { + if (typeof myContainer === "string") { + myContainer = document.getElementById(myContainer); + } + if (myContainer === elmContainer) { + return; } + elmContainer = myContainer; + elmPlayingField = null; + + me.setupPlayingField(); }; - SNAKE.addEventListener(window, "keyup", kbTryAgainShortcut, true); - - SNAKE.addEventListener(tryAgainStart, "click", reloadGame, false); - tmpElm.appendChild(tryAgainTxt); - tmpElm.appendChild(tryAgainStart); - return tmpElm; - } - // --------------------------------------------------------------------- - // public functions - // --------------------------------------------------------------------- - - me.setPaused = function(val) { - isPaused = val; - mySnake.setPaused(val); - if (isPaused) { - elmPauseScreen.style.display = "block"; - } else { - elmPauseScreen.style.display = "none"; - } - }; - me.getPaused = function() { - return isPaused; - }; - - /** - * Resets the playing board for a new game. - * @method resetBoard - */ - me.resetBoard = function() { - SNAKE.removeEventListener(elmContainer, "keydown", myKeyListener, false); - mySnake.reset(); - elmLengthPanel.innerHTML = "Length: 1"; - me.setupPlayingField(); - }; - /** - * Gets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. - * @return {Number} The state of the board. - */ - me.getBoardState = function() { - return boardState; - }; - /** - * Sets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. - * @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() { - return GRID_FOOD_VALUE; - }; - /** - * @method getPlayingFieldElement - * @return {DOM Element} The div representing the playing field (this is where the snake can move). - */ - me.getPlayingFieldElement = function() { - return elmPlayingField; - }; - /** - * @method setBoardContainer - * @param {DOM Element or String} myContainer Sets the container element for the game. - */ - me.setBoardContainer = function(myContainer) { - if (typeof myContainer === "string") { - myContainer = document.getElementById(myContainer); - } - if (myContainer === elmContainer) {return;} - elmContainer = myContainer; - elmPlayingField = null; - - me.setupPlayingField(); - }; - /** - * @method getBoardContainer - * @return {DOM Element} - */ - me.getBoardContainer = function() { - return elmContainer; - }; - /** - * @method getBlockWidth - * @return {Number} - */ - me.getBlockWidth = function() { - return blockWidth; - }; - /** - * @method getBlockHeight - * @return {Number} - */ - me.getBlockHeight = function() { - return blockHeight; - }; - /** - * Sets up the playing field. - * @method setupPlayingField - */ - me.setupPlayingField = function () { - - if (!elmPlayingField) {createBoardElements();} // create playing field - - // calculate width of our game container - var cWidth, 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 - var wEdgeSpace = me.getBlockWidth()*2 + (cWidth % me.getBlockWidth()); - var fWidth = Math.min(maxBoardWidth()-wEdgeSpace,cWidth-wEdgeSpace); - var hEdgeSpace = me.getBlockHeight()*3 + (cHeight % me.getBlockHeight()); - var fHeight = Math.min(maxBoardHeight()-hEdgeSpace,cHeight-hEdgeSpace); - - elmContainer.style.left = cLeft + "px"; - elmContainer.style.top = cTop + "px"; - elmContainer.style.width = cWidth + "px"; - elmContainer.style.height = cHeight + "px"; - elmPlayingField.style.left = me.getBlockWidth() + "px"; - elmPlayingField.style.top = me.getBlockHeight() + "px"; - elmPlayingField.style.width = fWidth + "px"; - elmPlayingField.style.height = fHeight + "px"; - - // the math for this will need to change depending on font size, padding, etc - // assuming height of 14 (font size) + 8 (padding) - var bottomPanelHeight = hEdgeSpace - me.getBlockHeight(); - var pLabelTop = me.getBlockHeight() + fHeight + Math.round((bottomPanelHeight - 30)/2) + "px"; - - elmAboutPanel.style.top = pLabelTop; - elmAboutPanel.style.width = "450px"; - elmAboutPanel.style.left = Math.round(cWidth/2) - Math.round(450/2) + "px"; - - elmLengthPanel.style.top = pLabelTop; - elmLengthPanel.style.left = cWidth - 120 + "px"; - - // if width is too narrow, hide the about panel - if (cWidth < 700) { - elmAboutPanel.style.display = "none"; - } else { - elmAboutPanel.style.display = "block"; - } - - me.grid = []; - var numBoardCols = fWidth / me.getBlockWidth() + 2; - var numBoardRows = fHeight / me.getBlockHeight() + 2; - - for (var row = 0; row < numBoardRows; row++) { - me.grid[row] = []; - for (var col = 0; col < numBoardCols; col++) { - if (col === 0 || row === 0 || col === (numBoardCols-1) || row === (numBoardRows-1)) { - me.grid[row][col] = 1; // an edge - } else { - me.grid[row][col] = 0; // empty space - } + /** + * @method getBoardContainer + * @return {DOM Element} + */ + me.getBoardContainer = function () { + return elmContainer; + }; + /** + * @method getBlockWidth + * @return {Number} + */ + me.getBlockWidth = function () { + return blockWidth; + }; + /** + * @method getBlockHeight + * @return {Number} + */ + me.getBlockHeight = function () { + return blockHeight; + }; + /** + * Sets up the playing field. + * @method setupPlayingField + */ + me.setupPlayingField = function () { + + if (!elmPlayingField) { + createBoardElements(); + } // create playing field + + // calculate width of our game container + var cWidth, 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; } - } - - myFood.randomlyPlaceFood(); - - // setup event listeners - function getMode (mode, speed) { - document.getElementById(mode).addEventListener('click', function () { snakeSpeed = speed; }); -} - getMode('Easy', 100); - getMode('Medium', 75); - getMode('Difficult', 50); - myKeyListener = function(evt) { - if (!evt) var evt = window.event; - var keyNum = (evt.which) ? evt.which : evt.keyCode; - - if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { - if ( !(keyNum >= 37 && keyNum <= 40) && !(keyNum === 87 || keyNum === 65 || keyNum === 83 || keyNum === 68)) {return;} // if not an arrow key, leave - - // This removes the listener added at the #listenerX line - SNAKE.removeEventListener(elmContainer, "keydown", myKeyListener, false); - - myKeyListener = function(evt) { - if (!evt) var evt = window.event; - var keyNum = (evt.which) ? evt.which : evt.keyCode; - - //32 = Enter - if (keyNum === 32 && me.getBoardState() != BOARD_STATE_GAME_INITIAL) { - me.setPaused(!me.getPaused()); + + // define the dimensions of the board and playing field + var wEdgeSpace = me.getBlockWidth() * 2 + (cWidth % me.getBlockWidth()); + var fWidth = Math.min(maxBoardWidth() - wEdgeSpace, cWidth - wEdgeSpace); + var hEdgeSpace = me.getBlockHeight() * 3 + (cHeight % me.getBlockHeight()); + var fHeight = Math.min(maxBoardHeight() - hEdgeSpace, cHeight - hEdgeSpace); + + elmContainer.style.left = cLeft + "px"; + elmContainer.style.top = cTop + "px"; + elmContainer.style.width = cWidth + "px"; + elmContainer.style.height = cHeight + "px"; + elmPlayingField.style.left = me.getBlockWidth() + "px"; + elmPlayingField.style.top = me.getBlockHeight() + "px"; + elmPlayingField.style.width = fWidth + "px"; + elmPlayingField.style.height = fHeight + "px"; + + // the math for this will need to change depending on font size, padding, etc + // assuming height of 14 (font size) + 8 (padding) + var bottomPanelHeight = hEdgeSpace - me.getBlockHeight(); + var pLabelTop = me.getBlockHeight() + fHeight + Math.round((bottomPanelHeight - 30) / 2) + "px"; + + elmAboutPanel.style.top = pLabelTop; + elmAboutPanel.style.width = "450px"; + elmAboutPanel.style.left = Math.round(cWidth / 2) - Math.round(450 / 2) + "px"; + + elmLengthPanel.style.top = pLabelTop; + elmLengthPanel.style.left = cWidth - 120 + "px"; + + // if width is too narrow, hide the about panel + if (cWidth < 700) { + elmAboutPanel.style.display = "none"; + } else { + elmAboutPanel.style.display = "block"; + } + + me.grid = []; + var numBoardCols = fWidth / me.getBlockWidth() + 2; + var numBoardRows = fHeight / me.getBlockHeight() + 2; + + for (var row = 0; row < numBoardRows; row++) { + me.grid[row] = []; + for (var col = 0; col < numBoardCols; col++) { + if (col === 0 || row === 0 || col === (numBoardCols - 1) || row === (numBoardRows - 1)) { + me.grid[row][col] = 1; // an edge + } else { + me.grid[row][col] = 0; // empty space } - - mySnake.handleArrowKeys(keyNum); - - evt.cancelBubble = true; - if (evt.stopPropagation) {evt.stopPropagation();} - if (evt.preventDefault) {evt.preventDefault();} - return false; - }; - SNAKE.addEventListener( elmContainer, "keydown", myKeyListener, false); - - mySnake.rebirth(); - mySnake.handleArrowKeys(keyNum); - me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! - mySnake.go(); + } + } + + myFood.randomlyPlaceFood(); + + // setup event listeners + function getMode(mode, speed) { + document.getElementById(mode).addEventListener('click', function () { + snakeSpeed = speed; + }); } - - evt.cancelBubble = true; - if (evt.stopPropagation) {evt.stopPropagation();} - if (evt.preventDefault) {evt.preventDefault();} - return false; + + getMode('Easy', 100); + getMode('Medium', 75); + getMode('Difficult', 50); + myKeyListener = function (evt) { + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + + if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { + if (!(keyNum >= 37 && keyNum <= 40) && !(keyNum === 87 || keyNum === 65 || keyNum === 83 || keyNum === 68)) { + return; + } // if not an arrow key, leave + + // This removes the listener added at the #listenerX line + SNAKE.removeEventListener(elmContainer, "keydown", myKeyListener, false); + + myKeyListener = function (evt) { + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + + //32 = Enter + if (keyNum === 32 && me.getBoardState() != BOARD_STATE_GAME_INITIAL) { + me.setPaused(!me.getPaused()); + } + + mySnake.handleArrowKeys(keyNum); + + evt.cancelBubble = true; + if (evt.stopPropagation) { + evt.stopPropagation(); + } + if (evt.preventDefault) { + evt.preventDefault(); + } + return false; + }; + SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); + + mySnake.rebirth(); + mySnake.handleArrowKeys(keyNum); + me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! + mySnake.go(); + } + + evt.cancelBubble = true; + if (evt.stopPropagation) { + evt.stopPropagation(); + } + if (evt.preventDefault) { + evt.preventDefault(); + } + return false; + }; + + // Search for #listenerX to see where this is removed + SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); }; - - // Search for #listenerX to see where this is removed - SNAKE.addEventListener( elmContainer, "keydown", myKeyListener, false); - }; - - /** - * This method is called when the snake has eaten some food. - * @method foodEaten - */ - me.foodEaten = function() { - elmLengthPanel.innerHTML = "Length: " + mySnake.snakeLength; - myFood.randomlyPlaceFood(); - }; - - /** - * This method is called when the snake dies. - * @method handleDeath - */ - me.handleDeath = function() { - var index = Math.max(getNextHighestZIndex( mySnake.snakeBody), getNextHighestZIndex( {tmp:{elm:myFood.getFoodElement()}} )); - elmContainer.removeChild(elmTryAgain); - elmContainer.appendChild(elmTryAgain); - elmTryAgain.style.zIndex = index; - elmTryAgain.style.display = "block"; + + /** + * This method is called when the snake has eaten some food. + * @method foodEaten + */ + me.foodEaten = function () { + elmLengthPanel.innerHTML = "Length: " + mySnake.snakeLength; + myFood.randomlyPlaceFood(); + }; + + /** + * This method is called when the snake dies. + * @method handleDeath + */ + me.handleDeath = function () { + var index = Math.max(getNextHighestZIndex(mySnake.snakeBody), getNextHighestZIndex({tmp: {elm: myFood.getFoodElement()}})); + elmContainer.removeChild(elmTryAgain); + elmContainer.appendChild(elmTryAgain); + elmTryAgain.style.zIndex = index; + elmTryAgain.style.display = "block"; + me.setBoardState(BOARD_STATE_GAME_INITIAL); + }; + + // --------------------------------------------------------------------- + // Initialize + // --------------------------------------------------------------------- + + config.fullScreen = (typeof config.fullScreen === "undefined") ? false : config.fullScreen; + config.top = (typeof config.top === "undefined") ? 0 : config.top; + config.left = (typeof config.left === "undefined") ? 0 : config.left; + config.width = (typeof config.width === "undefined") ? 400 : config.width; + config.height = (typeof config.height === "undefined") ? 400 : config.height; + + if (config.fullScreen) { + SNAKE.addEventListener(window, "resize", function () { + me.setupPlayingField(); + }, false); + } + me.setBoardState(BOARD_STATE_GAME_INITIAL); - }; - - // --------------------------------------------------------------------- - // Initialize - // --------------------------------------------------------------------- - - config.fullScreen = (typeof config.fullScreen === "undefined") ? false : config.fullScreen; - config.top = (typeof config.top === "undefined") ? 0 : config.top; - config.left = (typeof config.left === "undefined") ? 0 : config.left; - config.width = (typeof config.width === "undefined") ? 400 : config.width; - config.height = (typeof config.height === "undefined") ? 400 : config.height; - - if (config.fullScreen) { - SNAKE.addEventListener(window,"resize", function() { - me.setupPlayingField(); - }, false); - } - - me.setBoardState(BOARD_STATE_GAME_INITIAL); - - if (config.boardContainer) { - me.setBoardContainer(config.boardContainer); - } - - }; // end return function -})(); -function getHighScore () { + + if (config.boardContainer) { + me.setBoardContainer(config.boardContainer); + } + + }; // end return function + })(); +function getHighScore() { document.getElementById('high-score').addEventListener('click', function () { if (localStorage.jsSnakeHighScore == undefined) alert('You have not played this game yet!'); else - alert('Your current high score is ' + localStorage.jsSnakeHighScore + '.'); }); + alert('Your current high score is ' + localStorage.jsSnakeHighScore + '.'); + }); } getHighScore(); From 20893709d780f26d00288c80ed68ffab3ae3c20c Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 12 Jul 2017 14:42:52 -0400 Subject: [PATCH 04/66] Improved direction --- .idea/workspace.xml | 113 ++++++++++++++++++++++++++++---------------- js/snake.js | 27 ++++++----- 2 files changed, 86 insertions(+), 54 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a8b60527..de4ed77f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + - + - - + + - - + @@ -30,8 +30,8 @@ - - + + @@ -50,6 +50,12 @@ set boardState mode + boardContainer + fullScreen + config + currentDirection + direc + lastMove @@ -75,10 +81,10 @@ DEFINITION_ORDER - @@ -95,9 +101,8 @@ - - + @@ -126,12 +131,14 @@ + + - - + - + - - - - - - + + + + + + - - - - - + + + + + - + - - + + - + - - + + @@ -651,29 +666,45 @@ + + + + + + + + + + + + + + + + + + - - - + + - - + + - - + diff --git a/js/snake.js b/js/snake.js index 68481393..c6b87cbe 100644 --- a/js/snake.js +++ b/js/snake.js @@ -9,6 +9,11 @@ var BOARD_STATE_GAME_INITIAL = 0; var BOARD_STATE_GAME_STARTING = 1; var BOARD_STATE_GAME_STARTED = 2; +var SNAKE_DIRECTION_UP = 0; +var SNAKE_DIRECTION_LEFT = 3; +var SNAKE_DIRECTION_RIGHT = 1; +var SNAKE_DIRECTION_DOWN = 2; + /** * @module Snake * @class SNAKE @@ -123,7 +128,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { myId = instanceNumber++, growthIncr = 5, moveQueue = [], // a queue that holds the next moves of the snake - currentDirection = 1, // 0: up, 1: left, 2: down, 3: right + currentDirection = SNAKE_DIRECTION_LEFT, columnShift = [0, 1, 0, -1], rowShift = [-1, 0, 1, 0], xPosShift = [], @@ -208,10 +213,6 @@ SNAKE.Snake = SNAKE.Snake || (function () { */ /* 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) { @@ -225,26 +226,26 @@ SNAKE.Snake = SNAKE.Snake || (function () { case 37: case 65: - if (lastMove !== 1 || snakeLength === 1) { - moveQueue.unshift(3); //SnakeDirection = 3; + if (lastMove !== SNAKE_DIRECTION_RIGHT || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_LEFT); } break; case 38: case 87: - if (lastMove !== 2 || snakeLength === 1) { - moveQueue.unshift(0);//SnakeDirection = 0; + if (lastMove !== SNAKE_DIRECTION_DOWN || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_UP); } break; case 39: case 68: - if (lastMove !== 3 || snakeLength === 1) { - moveQueue.unshift(1); //SnakeDirection = 1; + if (lastMove !== SNAKE_DIRECTION_LEFT || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_RIGHT); } break; case 40: case 83: - if (lastMove !== 0 || snakeLength === 1) { - moveQueue.unshift(2);//SnakeDirection = 2; + if (lastMove !== SNAKE_DIRECTION_UP || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_DOWN); } break; } From 7b4ac83d8131cf437e748f314838149e047603f7 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 12 Jul 2017 15:16:24 -0400 Subject: [PATCH 05/66] Added events --- .idea/workspace.xml | 22 ++++++++++----------- js/snake.js | 47 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index de4ed77f..3be8e75f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -614,12 +614,12 @@ - @@ -671,7 +671,7 @@ - + @@ -701,10 +701,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index c6b87cbe..01a611ff 100644 --- a/js/snake.js +++ b/js/snake.js @@ -251,6 +251,51 @@ SNAKE.Snake = SNAKE.Snake || (function () { } }; + me.handleDeviceOrientation = function(directionEvent){ + if (isDead || isPaused) { + return; + } + + var snakeLength = me.snakeLength; + var lastMove = moveQueue[0] || currentDirection; + + var orientationX = e.accelerationIncludingGravity.x; + var orientationY = e.accelerationIncludingGravity.y; + + var absOrientationX = abs(orientationX); + var absOrientationY = abs(orientationY); + + if (absOrientationX > 3 || absOrientationY > 3){ + if (absOrientationX > absOrientationY){ + // Up/Down movement + if (orientationX > 0){ + if (lastMove !== SNAKE_DIRECTION_UP || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_DOWN); + } + }else{ + if (lastMove !== SNAKE_DIRECTION_DOWN || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_UP); + } + } + } + else{ + // Left/Right movement + if (orientationY > 0){ + if (lastMove !== SNAKE_DIRECTION_LEFT || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_RIGHT); + } + }else{ + if (lastMove !== SNAKE_DIRECTION_RIGHT || snakeLength === 1) { + moveQueue.unshift(SNAKE_DIRECTION_LEFT); + } + } + } + } + + + + } + /** * 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 @@ -609,7 +654,7 @@ SNAKE.Board = SNAKE.Board || (function () { GRID_FOOD_VALUE = -1, // the value of a spot on the board that represents snake food, MUST BE NEGATIVE myFood, mySnake, - boardState = 1, // 0: in active; 1: awaiting game start; 2: playing game + boardState = BOARD_STATE_GAME_STARTING, myKeyListener, isPaused = false,//note: both the board and the snake can be paused // Board components From 74bc8c06e5b4ace7eee0dbff63d1a37a841bd280 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Sun, 16 Jul 2017 15:46:52 -0400 Subject: [PATCH 06/66] Improvements --- .idea/workspace.xml | 47 +++++++++++++++++++++++++++++++++------------ js/snake.js | 35 ++++++++++++++++++++++++++++----- 2 files changed, 65 insertions(+), 17 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3be8e75f..9ab17f05 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -101,8 +105,9 @@ - + + @@ -131,7 +136,6 @@ - @@ -615,11 +619,12 @@ 1499879325478 + - @@ -641,6 +646,7 @@ + @@ -652,7 +658,6 @@ - @@ -671,7 +676,25 @@ - + + + + + + + + + + + + + + + + + + + @@ -701,10 +724,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 01a611ff..dd676fb9 100644 --- a/js/snake.js +++ b/js/snake.js @@ -259,11 +259,11 @@ SNAKE.Snake = SNAKE.Snake || (function () { var snakeLength = me.snakeLength; var lastMove = moveQueue[0] || currentDirection; - var orientationX = e.accelerationIncludingGravity.x; - var orientationY = e.accelerationIncludingGravity.y; + var orientationX = directionEvent.accelerationIncludingGravity.x; + var orientationY = directionEvent.accelerationIncludingGravity.y; - var absOrientationX = abs(orientationX); - var absOrientationY = abs(orientationY); + var absOrientationX = Math.abs(orientationX); + var absOrientationY = Math.abs(orientationY); if (absOrientationX > 3 || absOrientationY > 3){ if (absOrientationX > absOrientationY){ @@ -294,7 +294,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { - } + }; /** * 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. @@ -656,6 +656,7 @@ SNAKE.Board = SNAKE.Board || (function () { mySnake, boardState = BOARD_STATE_GAME_STARTING, myKeyListener, + myDeviceOrientationListener, isPaused = false,//note: both the board and the snake can be paused // Board components elmContainer, elmPlayingField, elmAboutPanel, elmLengthPanel, elmWelcome, elmTryAgain, elmPauseScreen; @@ -1031,6 +1032,30 @@ SNAKE.Board = SNAKE.Board || (function () { SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); }; + myDeviceOrientationListener = function(evt) { + if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { + var orientationX = evt.accelerationIncludingGravity.x; + var orientationY = evt.accelerationIncludingGravity.y; + if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)); + me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! + mySnake.go(); + } + + evt.cancelBubble = true; + if (evt.stopPropagation) { + evt.stopPropagation(); + } + if (evt.preventDefault) { + evt.preventDefault(); + } + return false; + }; + + SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); + }; + + + /** * This method is called when the snake has eaten some food. * @method foodEaten From 554df391024822018f69a82f10fcc715fd8bfedb Mon Sep 17 00:00:00 2001 From: Cydrick Date: Sun, 16 Jul 2017 15:49:34 -0400 Subject: [PATCH 07/66] Improvements --- .idea/workspace.xml | 21 ++++++++++----------- js/snake.js | 17 +++++++---------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 9ab17f05..74224c14 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - - @@ -676,7 +675,7 @@ - + @@ -694,7 +693,7 @@ - + @@ -724,10 +723,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index dd676fb9..b0f57e95 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1028,15 +1028,11 @@ SNAKE.Board = SNAKE.Board || (function () { return false; }; - // Search for #listenerX to see where this is removed - SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); - }; - - myDeviceOrientationListener = function(evt) { - if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { - var orientationX = evt.accelerationIncludingGravity.x; - var orientationY = evt.accelerationIncludingGravity.y; - if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)); + myDeviceOrientationListener = function(evt) { + if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { + var orientationX = evt.accelerationIncludingGravity.x; + var orientationY = evt.accelerationIncludingGravity.y; + if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)); me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); } @@ -1051,11 +1047,12 @@ SNAKE.Board = SNAKE.Board || (function () { return false; }; + // Search for #listenerX to see where this is removed + SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); }; - /** * This method is called when the snake has eaten some food. * @method foodEaten From 3f509ed3403b889a752622866a744ff8b7ee4ef7 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Sun, 16 Jul 2017 15:56:05 -0400 Subject: [PATCH 08/66] Improvements --- .idea/workspace.xml | 20 ++++++++++---------- js/snake.js | 1 + 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 74224c14..ed94258c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -618,12 +618,12 @@ 1499879325478 - + - @@ -675,7 +675,7 @@ - + @@ -693,7 +693,7 @@ - + @@ -723,10 +723,10 @@
- - + + - + diff --git a/js/snake.js b/js/snake.js index b0f57e95..594cd0f6 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1033,6 +1033,7 @@ SNAKE.Board = SNAKE.Board || (function () { var orientationX = evt.accelerationIncludingGravity.x; var orientationY = evt.accelerationIncludingGravity.y; if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)); + alert("Started"); me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); } From a98731b136ab90a6783a4b701e30fba7197605a8 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Sun, 16 Jul 2017 15:56:44 -0400 Subject: [PATCH 09/66] Improvements --- .idea/workspace.xml | 20 ++++++++++---------- js/snake.js | 1 + 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index ed94258c..f1e5d19c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -618,12 +618,12 @@ 1499879325478 - +
- @@ -675,7 +675,7 @@ - + @@ -693,7 +693,7 @@ - + @@ -723,10 +723,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 594cd0f6..81196518 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1014,6 +1014,7 @@ SNAKE.Board = SNAKE.Board || (function () { mySnake.rebirth(); mySnake.handleArrowKeys(keyNum); + alert("Started keys"); me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); } From 4894765064009e3b1bfdcd7bec8a86f826282406 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Sun, 16 Jul 2017 19:41:27 -0400 Subject: [PATCH 10/66] Improvements --- .idea/workspace.xml | 115 ++++++++++++++++++++++++++++++++++++++++---- js/snake.js | 10 +++- 2 files changed, 114 insertions(+), 11 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f1e5d19c..2bf53e52 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -59,6 +59,7 @@ kbTryAgainShortcut addEventListener myKeyListener + BOARD_STATE_GAME_STARTED @@ -142,6 +143,9 @@ + + + - @@ -675,7 +695,7 @@ - + @@ -693,7 +713,7 @@ - + @@ -723,13 +743,88 @@ - - + + - + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + JavaScript-Snake + + + + + + + + + + + + + + \ No newline at end of file diff --git a/js/snake.js b/js/snake.js index 81196518..bcd6f8f8 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1051,7 +1051,15 @@ SNAKE.Board = SNAKE.Board || (function () { // Search for #listenerX to see where this is removed SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); - SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); + var gyroPresent = false; + SNAKE.addEventListener(window, "devicemotion", function(event){ + if(event.rotationRate.alpha || event.rotationRate.beta || event.rotationRate.gamma){ + SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); + alert("Yes"); + }else{ + alert("No"); + } + }); }; From c7fcf5f03e784661c0e7f372a23ee5b5b031a0aa Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 15:19:11 -0400 Subject: [PATCH 11/66] Improvements --- .idea/workspace.xml | 45 +++++++++++++++++++++++++++++++++------------ js/snake.js | 15 ++++++--------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2bf53e52..cfeda008 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -105,8 +107,8 @@ - + @@ -638,12 +640,13 @@ 1499879325478 - + + - @@ -665,7 +668,6 @@ - @@ -677,6 +679,7 @@ + @@ -695,7 +698,25 @@ - + + + + + + + + + + + + + + + + + + + @@ -713,7 +734,7 @@ - + @@ -743,10 +764,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index bcd6f8f8..a92824cd 100644 --- a/js/snake.js +++ b/js/snake.js @@ -268,12 +268,10 @@ SNAKE.Snake = SNAKE.Snake || (function () { if (absOrientationX > 3 || absOrientationY > 3){ if (absOrientationX > absOrientationY){ // Up/Down movement - if (orientationX > 0){ - if (lastMove !== SNAKE_DIRECTION_UP || snakeLength === 1) { + if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN) || snakeLength === 1) { + if (orientationX > 0) { moveQueue.unshift(SNAKE_DIRECTION_DOWN); - } - }else{ - if (lastMove !== SNAKE_DIRECTION_DOWN || snakeLength === 1) { + } else { moveQueue.unshift(SNAKE_DIRECTION_UP); } } @@ -281,11 +279,11 @@ SNAKE.Snake = SNAKE.Snake || (function () { else{ // Left/Right movement if (orientationY > 0){ - if (lastMove !== SNAKE_DIRECTION_LEFT || snakeLength === 1) { + if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT) || snakeLength === 1) { moveQueue.unshift(SNAKE_DIRECTION_RIGHT); } }else{ - if (lastMove !== SNAKE_DIRECTION_RIGHT || snakeLength === 1) { + if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT) || snakeLength === 1) { moveQueue.unshift(SNAKE_DIRECTION_LEFT); } } @@ -995,7 +993,7 @@ SNAKE.Board = SNAKE.Board || (function () { var keyNum = (evt.which) ? evt.which : evt.keyCode; //32 = Enter - if (keyNum === 32 && me.getBoardState() != BOARD_STATE_GAME_INITIAL) { + if (keyNum === 32 && me.getBoardState() !== BOARD_STATE_GAME_INITIAL) { me.setPaused(!me.getPaused()); } @@ -1051,7 +1049,6 @@ SNAKE.Board = SNAKE.Board || (function () { // Search for #listenerX to see where this is removed SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); - var gyroPresent = false; SNAKE.addEventListener(window, "devicemotion", function(event){ if(event.rotationRate.alpha || event.rotationRate.beta || event.rotationRate.gamma){ SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); From c166ca9469b04e6486a58a2dca468bef62f62cfd Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 15:34:01 -0400 Subject: [PATCH 12/66] Improvements --- .idea/workspace.xml | 24 ++++++++++++------------ js/snake.js | 13 +++++++------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index cfeda008..37d10762 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -641,12 +641,12 @@ - + - @@ -698,7 +698,7 @@ - + @@ -716,7 +716,7 @@ - + @@ -734,7 +734,7 @@ - + @@ -764,10 +764,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index a92824cd..a2eba23a 100644 --- a/js/snake.js +++ b/js/snake.js @@ -820,6 +820,7 @@ SNAKE.Board = SNAKE.Board || (function () { */ me.resetBoard = function () { SNAKE.removeEventListener(elmContainer, "keydown", myKeyListener, false); + SNAKE.removeEventListener(window, "devicemotion", myDeviceOrientationListener, false); mySnake.reset(); elmLengthPanel.innerHTML = "Length: 1"; me.setupPlayingField(); @@ -1029,12 +1030,14 @@ SNAKE.Board = SNAKE.Board || (function () { myDeviceOrientationListener = function(evt) { if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { + SNAKE.removeEventListener(elmContainer, "keydown", myDeviceOrientationListener, false); var orientationX = evt.accelerationIncludingGravity.x; var orientationY = evt.accelerationIncludingGravity.y; - if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)); - alert("Started"); - me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! - mySnake.go(); + if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)){ + alert("Started"); + me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! + mySnake.go(); + } } evt.cancelBubble = true; @@ -1053,8 +1056,6 @@ SNAKE.Board = SNAKE.Board || (function () { if(event.rotationRate.alpha || event.rotationRate.beta || event.rotationRate.gamma){ SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); alert("Yes"); - }else{ - alert("No"); } }); }; From 1e3bbf5325a302e3a1edee977947c85b3f93253a Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 15:37:50 -0400 Subject: [PATCH 13/66] Improvements --- .idea/workspace.xml | 23 ++++++++++++----------- js/snake.js | 7 +------ 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 37d10762..2fccdd2c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -62,6 +62,7 @@ BOARD_STATE_GAME_STARTED started removeEventListener + No @@ -641,12 +642,12 @@ - + - @@ -698,7 +699,7 @@ - + @@ -716,7 +717,7 @@ - + @@ -734,7 +735,7 @@ - + @@ -764,10 +765,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index a2eba23a..063fdb6f 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1052,12 +1052,7 @@ SNAKE.Board = SNAKE.Board || (function () { // Search for #listenerX to see where this is removed SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); - SNAKE.addEventListener(window, "devicemotion", function(event){ - if(event.rotationRate.alpha || event.rotationRate.beta || event.rotationRate.gamma){ - SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); - alert("Yes"); - } - }); + SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); }; From 53981173a3e982214a87962a5c98fefe1605856d Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 15:56:43 -0400 Subject: [PATCH 14/66] Improvements --- .idea/workspace.xml | 23 ++++++++++++----------- js/snake.js | 20 +++++++++++++------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2fccdd2c..33b19ecd 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -63,6 +63,7 @@ started removeEventListener No + > 3 @@ -642,12 +643,12 @@ - + - @@ -699,7 +700,7 @@ - + @@ -717,7 +718,7 @@ - + @@ -735,7 +736,7 @@ - + @@ -765,10 +766,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 063fdb6f..843a22f1 100644 --- a/js/snake.js +++ b/js/snake.js @@ -255,7 +255,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { if (isDead || isPaused) { return; } - + console.log("Initial."); var snakeLength = me.snakeLength; var lastMove = moveQueue[0] || currentDirection; @@ -265,25 +265,31 @@ SNAKE.Snake = SNAKE.Snake || (function () { var absOrientationX = Math.abs(orientationX); var absOrientationY = Math.abs(orientationY); - if (absOrientationX > 3 || absOrientationY > 3){ + if (absOrientationX >= 3 || absOrientationY >= 3){ + console.log("Okay 1"); if (absOrientationX > absOrientationY){ // Up/Down movement + console.log("Up/Down"); if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN) || snakeLength === 1) { if (orientationX > 0) { + console.log("Down"); moveQueue.unshift(SNAKE_DIRECTION_DOWN); } else { + console.log("Up"); moveQueue.unshift(SNAKE_DIRECTION_UP); } } } - else{ + else { // Left/Right movement - if (orientationY > 0){ - if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT) || snakeLength === 1) { + console.log("Left/Right"); + if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT) || snakeLength === 1) { + if (orientationY > 0) { + console.log("Right"); moveQueue.unshift(SNAKE_DIRECTION_RIGHT); } - }else{ - if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT) || snakeLength === 1) { + else + console.log("Left"); moveQueue.unshift(SNAKE_DIRECTION_LEFT); } } From 3efc7c24fb2a8e6b6ee019a702024db2006f75c0 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:06:43 -0400 Subject: [PATCH 15/66] Improvements --- .idea/workspace.xml | 24 ++++++++++++------------ js/snake.js | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 33b19ecd..aa677fcc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -55,7 +55,6 @@ direc lastMove me.handleArrowKeys - handleArrowKeys kbTryAgainShortcut addEventListener myKeyListener @@ -64,6 +63,7 @@ removeEventListener No > 3 + handleArrowKeys @@ -643,12 +643,12 @@ - + - @@ -700,7 +700,7 @@ - + @@ -718,7 +718,7 @@ - + @@ -736,7 +736,7 @@ - + @@ -766,10 +766,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 843a22f1..d357f98f 100644 --- a/js/snake.js +++ b/js/snake.js @@ -288,16 +288,13 @@ SNAKE.Snake = SNAKE.Snake || (function () { console.log("Right"); moveQueue.unshift(SNAKE_DIRECTION_RIGHT); } - else + else{ console.log("Left"); moveQueue.unshift(SNAKE_DIRECTION_LEFT); } } } } - - - }; /** @@ -1043,7 +1040,10 @@ SNAKE.Board = SNAKE.Board || (function () { alert("Started"); me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); + SNAKE.addEventListener(window, "devicemotion", mySnake.handleDeviceOrientation, false); + alert("That version"); } + } evt.cancelBubble = true; From 107568c7ef8d12ad66a40a8d2991a4ae406b62b8 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:11:45 -0400 Subject: [PATCH 16/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index aa677fcc..08c55dc0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -643,12 +643,12 @@ - + - @@ -700,7 +700,7 @@ - + @@ -718,7 +718,7 @@ - + @@ -736,7 +736,7 @@ - + @@ -766,10 +766,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index d357f98f..26e0c5fe 100644 --- a/js/snake.js +++ b/js/snake.js @@ -128,7 +128,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { myId = instanceNumber++, growthIncr = 5, moveQueue = [], // a queue that holds the next moves of the snake - currentDirection = SNAKE_DIRECTION_LEFT, + currentDirection = SNAKE_DIRECTION_RIGHT, columnShift = [0, 1, 0, -1], rowShift = [-1, 0, 1, 0], xPosShift = [], From e56d421e421b46f547f58ecb655f20c4f7a7b1b7 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:20:48 -0400 Subject: [PATCH 17/66] Improvements --- .idea/workspace.xml | 23 ++++++++++++----------- js/snake.js | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 08c55dc0..f49b24c4 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -64,6 +64,7 @@ No > 3 handleArrowKeys + moveQueue @@ -643,12 +644,12 @@ - + - @@ -700,7 +701,7 @@ - + @@ -718,7 +719,7 @@ - + @@ -736,7 +737,7 @@ - + @@ -766,10 +767,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 26e0c5fe..d357f98f 100644 --- a/js/snake.js +++ b/js/snake.js @@ -128,7 +128,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { myId = instanceNumber++, growthIncr = 5, moveQueue = [], // a queue that holds the next moves of the snake - currentDirection = SNAKE_DIRECTION_RIGHT, + currentDirection = SNAKE_DIRECTION_LEFT, columnShift = [0, 1, 0, -1], rowShift = [-1, 0, 1, 0], xPosShift = [], From 9ed7071b1638a20fec7cb3d441ac3c19e2575378 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:26:08 -0400 Subject: [PATCH 18/66] Improvements --- .idea/workspace.xml | 23 ++++++++++++----------- js/snake.js | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f49b24c4..af905014 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + - @@ -701,7 +702,7 @@ - + @@ -719,7 +720,7 @@ - + @@ -737,7 +738,7 @@ - + @@ -767,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index d357f98f..26e0c5fe 100644 --- a/js/snake.js +++ b/js/snake.js @@ -128,7 +128,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { myId = instanceNumber++, growthIncr = 5, moveQueue = [], // a queue that holds the next moves of the snake - currentDirection = SNAKE_DIRECTION_LEFT, + currentDirection = SNAKE_DIRECTION_RIGHT, columnShift = [0, 1, 0, -1], rowShift = [-1, 0, 1, 0], xPosShift = [], From 58dcb614faf5eeefd94784650399c77b2f4405f3 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:43:33 -0400 Subject: [PATCH 19/66] Improvements --- .idea/workspace.xml | 24 ++++++++++++------------ js/snake.js | 9 ++++----- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index af905014..515d5a88 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 26e0c5fe..840dc1c7 100644 --- a/js/snake.js +++ b/js/snake.js @@ -10,9 +10,9 @@ var BOARD_STATE_GAME_STARTING = 1; var BOARD_STATE_GAME_STARTED = 2; var SNAKE_DIRECTION_UP = 0; -var SNAKE_DIRECTION_LEFT = 3; var SNAKE_DIRECTION_RIGHT = 1; var SNAKE_DIRECTION_DOWN = 2; +var SNAKE_DIRECTION_LEFT = 3; /** * @module Snake @@ -255,13 +255,15 @@ SNAKE.Snake = SNAKE.Snake || (function () { if (isDead || isPaused) { return; } - console.log("Initial."); var snakeLength = me.snakeLength; var lastMove = moveQueue[0] || currentDirection; + console.log("Last Move = " + lastMove); var orientationX = directionEvent.accelerationIncludingGravity.x; var orientationY = directionEvent.accelerationIncludingGravity.y; + console.log(orientationX + " - " + orientationY); + var absOrientationX = Math.abs(orientationX); var absOrientationY = Math.abs(orientationY); @@ -1016,7 +1018,6 @@ SNAKE.Board = SNAKE.Board || (function () { mySnake.rebirth(); mySnake.handleArrowKeys(keyNum); - alert("Started keys"); me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); } @@ -1037,11 +1038,9 @@ SNAKE.Board = SNAKE.Board || (function () { var orientationX = evt.accelerationIncludingGravity.x; var orientationY = evt.accelerationIncludingGravity.y; if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)){ - alert("Started"); me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); SNAKE.addEventListener(window, "devicemotion", mySnake.handleDeviceOrientation, false); - alert("That version"); } } From 9e85d5c6a448f323f17c3deb956c8dce408aa3af Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:43:58 -0400 Subject: [PATCH 20/66] Improvements --- .idea/workspace.xml | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 515d5a88..bff1f369 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,7 @@ - - - + - @@ -768,8 +766,8 @@ - - + + From 947f01cdfe7976b3c7a0a85dba1efd3f97647b64 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 16:57:46 -0400 Subject: [PATCH 21/66] Improvements --- .idea/workspace.xml | 26 ++++++++++++++------------ js/snake.js | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index bff1f369..6253bd7b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,9 @@ - + + + - @@ -700,7 +702,7 @@ - + @@ -718,7 +720,7 @@ - + @@ -736,7 +738,7 @@ - + @@ -766,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 840dc1c7..0bf002f9 100644 --- a/js/snake.js +++ b/js/snake.js @@ -1034,7 +1034,7 @@ SNAKE.Board = SNAKE.Board || (function () { myDeviceOrientationListener = function(evt) { if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { - SNAKE.removeEventListener(elmContainer, "keydown", myDeviceOrientationListener, false); + SNAKE.removeEventListener(elmContainer, "devicemotion", mySnake.handleDeviceOrientation, false); var orientationX = evt.accelerationIncludingGravity.x; var orientationY = evt.accelerationIncludingGravity.y; if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)){ From 885723d1db85c4bf8c0b507a8450e15537c6f646 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 17:06:35 -0400 Subject: [PATCH 22/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6253bd7b..a6e33c8f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 0bf002f9..820a6ab0 100644 --- a/js/snake.js +++ b/js/snake.js @@ -272,7 +272,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { if (absOrientationX > absOrientationY){ // Up/Down movement console.log("Up/Down"); - if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN) || snakeLength === 1) { + if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN)) { if (orientationX > 0) { console.log("Down"); moveQueue.unshift(SNAKE_DIRECTION_DOWN); @@ -285,7 +285,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { else { // Left/Right movement console.log("Left/Right"); - if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT) || snakeLength === 1) { + if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT)) { if (orientationY > 0) { console.log("Right"); moveQueue.unshift(SNAKE_DIRECTION_RIGHT); From 675c5413f955023bd63a4926665f1356f1abbd00 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 17:12:13 -0400 Subject: [PATCH 23/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index a6e33c8f..d153eb1f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 820a6ab0..e8246802 100644 --- a/js/snake.js +++ b/js/snake.js @@ -271,7 +271,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { console.log("Okay 1"); if (absOrientationX > absOrientationY){ // Up/Down movement - console.log("Up/Down"); + console.log("Up/Down" + lastMove !== SNAKE_DIRECTION_UP + lastMove !== SNAKE_DIRECTION_DOWN); if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN)) { if (orientationX > 0) { console.log("Down"); @@ -284,7 +284,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { } else { // Left/Right movement - console.log("Left/Right"); + console.log("Left/Right" + lastMove !== SNAKE_DIRECTION_LEFT + lastMove !== SNAKE_DIRECTION_RIGHT); if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT)) { if (orientationY > 0) { console.log("Right"); From 60e6eaef0aa027490d174dc5aa0eb2f958976ff4 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 17:25:40 -0400 Subject: [PATCH 24/66] Improvements --- .idea/workspace.xml | 18 +++++++++--------- js/snake.js | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index d153eb1f..510d78ec 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -18,9 +18,9 @@ - + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -769,9 +769,9 @@ - + - + diff --git a/js/snake.js b/js/snake.js index e8246802..82c49510 100644 --- a/js/snake.js +++ b/js/snake.js @@ -271,7 +271,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { console.log("Okay 1"); if (absOrientationX > absOrientationY){ // Up/Down movement - console.log("Up/Down" + lastMove !== SNAKE_DIRECTION_UP + lastMove !== SNAKE_DIRECTION_DOWN); + console.log("Up/Down " + (lastMove !== SNAKE_DIRECTION_UP) + " - " + (lastMove !== SNAKE_DIRECTION_DOWN)); if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN)) { if (orientationX > 0) { console.log("Down"); @@ -284,7 +284,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { } else { // Left/Right movement - console.log("Left/Right" + lastMove !== SNAKE_DIRECTION_LEFT + lastMove !== SNAKE_DIRECTION_RIGHT); + console.log("Left/Right" + (lastMove !== SNAKE_DIRECTION_LEFT) + " - " + (lastMove !== SNAKE_DIRECTION_RIGHT)); if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT)) { if (orientationY > 0) { console.log("Right"); From 25b08215640afe296601441f66f89f76fa4dc0e3 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 17:26:32 -0400 Subject: [PATCH 25/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 510d78ec..522dd9ec 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 82c49510..9a6e45ab 100644 --- a/js/snake.js +++ b/js/snake.js @@ -271,7 +271,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { console.log("Okay 1"); if (absOrientationX > absOrientationY){ // Up/Down movement - console.log("Up/Down " + (lastMove !== SNAKE_DIRECTION_UP) + " - " + (lastMove !== SNAKE_DIRECTION_DOWN)); + console.log("Up/Down " + (lastMove !== SNAKE_DIRECTION_UP).toString() + " - " + (lastMove !== SNAKE_DIRECTION_DOWN).toString()); if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN)) { if (orientationX > 0) { console.log("Down"); @@ -284,7 +284,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { } else { // Left/Right movement - console.log("Left/Right" + (lastMove !== SNAKE_DIRECTION_LEFT) + " - " + (lastMove !== SNAKE_DIRECTION_RIGHT)); + console.log("Left/Right" + (lastMove !== SNAKE_DIRECTION_LEFT).toString() + " - " + (lastMove !== SNAKE_DIRECTION_RIGHT).toString()); if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT)) { if (orientationY > 0) { console.log("Right"); From f64c56336778bb193a246ebf861ba3187fd7dba0 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 18:23:26 -0400 Subject: [PATCH 26/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 8 ++++---- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 522dd9ec..33b40dac 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 9a6e45ab..bc741e90 100644 --- a/js/snake.js +++ b/js/snake.js @@ -271,8 +271,8 @@ SNAKE.Snake = SNAKE.Snake || (function () { console.log("Okay 1"); if (absOrientationX > absOrientationY){ // Up/Down movement - console.log("Up/Down " + (lastMove !== SNAKE_DIRECTION_UP).toString() + " - " + (lastMove !== SNAKE_DIRECTION_DOWN).toString()); - if ((lastMove !== SNAKE_DIRECTION_UP && lastMove !== SNAKE_DIRECTION_DOWN)) { + console.log("Up/Down " + (lastMove === SNAKE_DIRECTION_LEFT).toString() + " - " + (lastMove === SNAKE_DIRECTION_RIGHT).toString()); + if ((lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)) { if (orientationX > 0) { console.log("Down"); moveQueue.unshift(SNAKE_DIRECTION_DOWN); @@ -284,8 +284,8 @@ SNAKE.Snake = SNAKE.Snake || (function () { } else { // Left/Right movement - console.log("Left/Right" + (lastMove !== SNAKE_DIRECTION_LEFT).toString() + " - " + (lastMove !== SNAKE_DIRECTION_RIGHT).toString()); - if ((lastMove !== SNAKE_DIRECTION_LEFT && lastMove !== SNAKE_DIRECTION_RIGHT)) { + console.log("Left/Right " + (lastMove === SNAKE_DIRECTION_UP).toString() + " - " + (lastMove === SNAKE_DIRECTION_DOWN).toString()); + if ((lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)) { if (orientationY > 0) { console.log("Right"); moveQueue.unshift(SNAKE_DIRECTION_RIGHT); From 34dadf2c0abeaea5113d7ee9075bd6be499200e1 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 18:27:12 -0400 Subject: [PATCH 27/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 1 + 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 33b40dac..3c89327e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index bc741e90..a243c55e 100644 --- a/js/snake.js +++ b/js/snake.js @@ -258,6 +258,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { var snakeLength = me.snakeLength; var lastMove = moveQueue[0] || currentDirection; console.log("Last Move = " + lastMove); + console.log("Queue = " + moveQueue); var orientationX = directionEvent.accelerationIncludingGravity.x; var orientationY = directionEvent.accelerationIncludingGravity.y; From a8b09ed48a4113245871bae158306f32fa62ec57 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 18:35:36 -0400 Subject: [PATCH 28/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 3 --- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 3c89327e..f2c133bc 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index a243c55e..541201e2 100644 --- a/js/snake.js +++ b/js/snake.js @@ -269,10 +269,8 @@ SNAKE.Snake = SNAKE.Snake || (function () { var absOrientationY = Math.abs(orientationY); if (absOrientationX >= 3 || absOrientationY >= 3){ - console.log("Okay 1"); if (absOrientationX > absOrientationY){ // Up/Down movement - console.log("Up/Down " + (lastMove === SNAKE_DIRECTION_LEFT).toString() + " - " + (lastMove === SNAKE_DIRECTION_RIGHT).toString()); if ((lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)) { if (orientationX > 0) { console.log("Down"); @@ -285,7 +283,6 @@ SNAKE.Snake = SNAKE.Snake || (function () { } else { // Left/Right movement - console.log("Left/Right " + (lastMove === SNAKE_DIRECTION_UP).toString() + " - " + (lastMove === SNAKE_DIRECTION_DOWN).toString()); if ((lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)) { if (orientationY > 0) { console.log("Right"); From b8b9eb0c07bf1fd43ff09141171378ceac9f4098 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 18:48:10 -0400 Subject: [PATCH 29/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f2c133bc..1add4b2a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 541201e2..1cb811f4 100644 --- a/js/snake.js +++ b/js/snake.js @@ -271,26 +271,26 @@ SNAKE.Snake = SNAKE.Snake || (function () { if (absOrientationX >= 3 || absOrientationY >= 3){ if (absOrientationX > absOrientationY){ // Up/Down movement - if ((lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)) { + if ((lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)) { if (orientationX > 0) { - console.log("Down"); - moveQueue.unshift(SNAKE_DIRECTION_DOWN); + console.log("Right"); + moveQueue.unshift(SNAKE_DIRECTION_LEFT); } else { - console.log("Up"); - moveQueue.unshift(SNAKE_DIRECTION_UP); + console.log("Left"); + moveQueue.unshift(SNAKE_DIRECTION_RIGHT); } } } else { // Left/Right movement - if ((lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)) { + if ((lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)) { if (orientationY > 0) { - console.log("Right"); - moveQueue.unshift(SNAKE_DIRECTION_RIGHT); + console.log("Down"); + moveQueue.unshift(SNAKE_DIRECTION_DOWN); } else{ - console.log("Left"); - moveQueue.unshift(SNAKE_DIRECTION_LEFT); + console.log("Up"); + moveQueue.unshift(SNAKE_DIRECTION_UP); } } } From c27ad3731f34c744d7a83b65410a7ce8725440a9 Mon Sep 17 00:00:00 2001 From: Cydrick Date: Mon, 17 Jul 2017 18:49:01 -0400 Subject: [PATCH 30/66] Improvements --- .idea/workspace.xml | 22 +++++++++++----------- js/snake.js | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1add4b2a..610d4cb7 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -645,12 +645,12 @@ - + - @@ -702,7 +702,7 @@ - + @@ -720,7 +720,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -768,10 +768,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 1cb811f4..20eec240 100644 --- a/js/snake.js +++ b/js/snake.js @@ -268,7 +268,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { var absOrientationX = Math.abs(orientationX); var absOrientationY = Math.abs(orientationY); - if (absOrientationX >= 3 || absOrientationY >= 3){ + if (absOrientationX >= 2 || absOrientationY >= 2){ if (absOrientationX > absOrientationY){ // Up/Down movement if ((lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)) { From 3f96ec84440d88bb560964a868108ef797e152c5 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 19 Jul 2017 13:30:32 -0400 Subject: [PATCH 31/66] Improvements --- .idea/workspace.xml | 186 +++++++++++++++++++++++++++++++++++++------- js/snake.js | 9 ++- 2 files changed, 161 insertions(+), 34 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 610d4cb7..4a92aa02 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + - down - left - 32 - getBoardState - setBoardState set boardState mode @@ -64,8 +60,17 @@ No > 3 handleArrowKeys - moveQueue alert( + moveQueue + fWidth + Use th + Easy + Use the + snakeSpeed + use the + playingBoard + isDead + play @@ -91,10 +96,10 @@ DEFINITION_ORDER - @@ -111,9 +116,8 @@ - - + @@ -142,6 +146,7 @@ + @@ -650,10 +655,10 @@ - - + @@ -662,28 +667,50 @@ - + - + - + - + + + + + + + + + + + + + + + + + + + + + + + @@ -693,7 +720,9 @@ - + + @@ -702,7 +731,104 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -720,7 +846,7 @@ - + @@ -738,7 +864,7 @@ - + @@ -760,7 +886,7 @@ - + @@ -768,10 +894,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 20eec240..891cc400 100644 --- a/js/snake.js +++ b/js/snake.js @@ -252,13 +252,13 @@ SNAKE.Snake = SNAKE.Snake || (function () { }; me.handleDeviceOrientation = function(directionEvent){ + console.log("IsDead: " + isDead.toString() + " - IsPaused: " + isPaused.toString()); if (isDead || isPaused) { return; } - var snakeLength = me.snakeLength; + console.log("Inside"); + var lastMove = moveQueue[0] || currentDirection; - console.log("Last Move = " + lastMove); - console.log("Queue = " + moveQueue); var orientationX = directionEvent.accelerationIncludingGravity.x; var orientationY = directionEvent.accelerationIncludingGravity.y; @@ -776,13 +776,14 @@ SNAKE.Board = SNAKE.Board || (function () { var reloadGame = function () { tmpElm.style.display = "none"; + me.rebirth() me.resetBoard(); me.setBoardState(BOARD_STATE_GAME_STARTING); me.getBoardContainer().focus(); }; var kbTryAgainShortcut = function (evt) { - if (me.getBoardState() !== BOARD_STATE_INITIAL || tmpElm.style.display !== "block") { + if (me.getBoardState() !== BOARD_STATE_GAME_INITIAL || tmpElm.style.display !== "block") { return; } if (!evt) var evt = window.event; From a651dde1dccca45a55ad30402127b25739b5729e Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 19 Jul 2017 13:39:49 -0400 Subject: [PATCH 32/66] Improving --- .idea/workspace.xml | 30 +++++++++++++++--------------- js/snake.js | 4 +--- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 4a92aa02..1005106c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -19,9 +19,9 @@ - + - + @@ -41,7 +41,6 @@ - set boardState mode boardContainer @@ -69,8 +68,9 @@ snakeSpeed use the playingBoard - isDead play + me.rebirth + isDead @@ -655,7 +655,7 @@ - @@ -731,7 +731,7 @@ - + @@ -749,7 +749,7 @@ - + @@ -767,7 +767,7 @@ - + @@ -785,7 +785,7 @@ - + @@ -818,7 +818,7 @@ - + @@ -828,7 +828,7 @@ - + @@ -846,7 +846,7 @@ - + @@ -864,7 +864,7 @@ - + @@ -895,9 +895,9 @@ - + - + diff --git a/js/snake.js b/js/snake.js index 891cc400..0cb69384 100644 --- a/js/snake.js +++ b/js/snake.js @@ -252,11 +252,9 @@ SNAKE.Snake = SNAKE.Snake || (function () { }; me.handleDeviceOrientation = function(directionEvent){ - console.log("IsDead: " + isDead.toString() + " - IsPaused: " + isPaused.toString()); if (isDead || isPaused) { return; } - console.log("Inside"); var lastMove = moveQueue[0] || currentDirection; @@ -776,7 +774,6 @@ SNAKE.Board = SNAKE.Board || (function () { var reloadGame = function () { tmpElm.style.display = "none"; - me.rebirth() me.resetBoard(); me.setBoardState(BOARD_STATE_GAME_STARTING); me.getBoardContainer().focus(); @@ -828,6 +825,7 @@ SNAKE.Board = SNAKE.Board || (function () { mySnake.reset(); elmLengthPanel.innerHTML = "Length: 1"; me.setupPlayingField(); + mySnake.rebirth(); }; /** * Gets the current state of the playing board. Use BOARD_STATE_GAME_INITIAL, BOARD_STATE_GAME_STARTING and BOARD_STATE_GAME_STARTED. From 6ec3449a13c21d790ba90244ef1a566e65fafb84 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 19 Jul 2017 14:19:37 -0400 Subject: [PATCH 33/66] Improving --- .idea/workspace.xml | 41 +++++++++++----------- js/snake.js | 85 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 98 insertions(+), 28 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1005106c..41f19948 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,6 @@ - - boardState - mode boardContainer fullScreen config @@ -71,6 +68,8 @@ play me.rebirth isDead + head + handleDeviceTouch @@ -655,7 +654,7 @@ - @@ -675,7 +674,7 @@ - + @@ -731,7 +730,7 @@ - + @@ -749,7 +748,7 @@ - + @@ -767,7 +766,7 @@ - + @@ -785,7 +784,7 @@ - + @@ -818,7 +817,7 @@ - + @@ -828,7 +827,7 @@ - + @@ -846,7 +845,7 @@ - + @@ -864,7 +863,7 @@ - + @@ -886,7 +885,7 @@ - + @@ -894,10 +893,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 0cb69384..0e37c28d 100644 --- a/js/snake.js +++ b/js/snake.js @@ -261,8 +261,6 @@ SNAKE.Snake = SNAKE.Snake || (function () { var orientationX = directionEvent.accelerationIncludingGravity.x; var orientationY = directionEvent.accelerationIncludingGravity.y; - console.log(orientationX + " - " + orientationY); - var absOrientationX = Math.abs(orientationX); var absOrientationY = Math.abs(orientationY); @@ -271,10 +269,8 @@ SNAKE.Snake = SNAKE.Snake || (function () { // Up/Down movement if ((lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)) { if (orientationX > 0) { - console.log("Right"); moveQueue.unshift(SNAKE_DIRECTION_LEFT); } else { - console.log("Left"); moveQueue.unshift(SNAKE_DIRECTION_RIGHT); } } @@ -283,11 +279,9 @@ SNAKE.Snake = SNAKE.Snake || (function () { // Left/Right movement if ((lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)) { if (orientationY > 0) { - console.log("Down"); moveQueue.unshift(SNAKE_DIRECTION_DOWN); } else{ - console.log("Up"); moveQueue.unshift(SNAKE_DIRECTION_UP); } } @@ -295,6 +289,71 @@ SNAKE.Snake = SNAKE.Snake || (function () { } }; + + me.handleDeviceTouch = function(touchEvent) { + + if (isDead || isPaused) { + return; + } + else if (touchEvent.touches.length !== 1) { + return; + } + + console.log(getClientWidth().toString() + " - " + getClientHeight().toString()); + + var lastMove = moveQueue[0] || currentDirection; + + var touchX = touchEvent.touches[0].clientX; + var touchY = touchEvent.touches[0].clientY; + + if (touchX < getClientWidth() * 0.25 && (lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)){ + moveQueue.unshift(SNAKE_DIRECTION_LEFT); + } + else if (touchX > getClientWidth() * 0.75 && (lastMove === SNAKE_DIRECTION_UP || lastMove === SNAKE_DIRECTION_DOWN)){ + moveQueue.unshift(SNAKE_DIRECTION_RIGHT); + } + else if (touchY < getClientHeight() * 0.3 && (lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)){ + moveQueue.unshift(SNAKE_DIRECTION_UP); + } + else if (touchY > getClientHeight() * 0.6 && (lastMove === SNAKE_DIRECTION_LEFT || lastMove === SNAKE_DIRECTION_RIGHT)){ + moveQueue.unshift(SNAKE_DIRECTION_DOWN); + } + + }; + + + /* + This function returns the width of the available screen real estate that we have + */ + function getClientWidth() { + var myWidth = 0; + if (typeof window.innerWidth === "number") { + myWidth = window.innerWidth;//Non-IE + } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) { + myWidth = document.documentElement.clientWidth;//IE 6+ in 'standards compliant mode' + } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) { + myWidth = document.body.clientWidth;//IE 4 compatible + } + return myWidth; + } + + /* + This function returns the height of the available screen real estate that we have + */ + function getClientHeight() { + var myHeight = 0; + if (typeof window.innerHeight === "number") { + myHeight = window.innerHeight;//Non-IE + } else if (document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight )) { + myHeight = document.documentElement.clientHeight;//IE 6+ in 'standards compliant mode' + } else if (document.body && ( document.body.clientWidth || document.body.clientHeight )) { + myHeight = document.body.clientHeight;//IE 4 compatible + } + return myHeight; + } + + + /** * 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 @@ -1034,7 +1093,7 @@ SNAKE.Board = SNAKE.Board || (function () { SNAKE.removeEventListener(elmContainer, "devicemotion", mySnake.handleDeviceOrientation, false); var orientationX = evt.accelerationIncludingGravity.x; var orientationY = evt.accelerationIncludingGravity.y; - if (me.getBoardState() === BOARD_STATE_GAME_STARTING && (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3)){ + if (Math.abs(orientationX) > 3 || Math.abs(orientationY) > 3){ me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); SNAKE.addEventListener(window, "devicemotion", mySnake.handleDeviceOrientation, false); @@ -1052,9 +1111,21 @@ SNAKE.Board = SNAKE.Board || (function () { return false; }; + myTouchListener = function(evt){ + if(evt.touches.length == 1){ + if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { + me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! + mySnake.go(); + console.log("Added"); + SNAKE.addEventListener(elmContainer, "touchstart", mySnake.handleDeviceTouch, false); + } + } + }; + // Search for #listenerX to see where this is removed SNAKE.addEventListener(elmContainer, "keydown", myKeyListener, false); SNAKE.addEventListener(window, "devicemotion", myDeviceOrientationListener, false); + SNAKE.addEventListener(elmContainer, "touchstart", myTouchListener, false) }; From ee9890ea7732dcb05726958086cebc4b5bb84ac1 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 19 Jul 2017 14:20:56 -0400 Subject: [PATCH 34/66] Improving --- .idea/workspace.xml | 30 +++++++++++++++--------------- js/snake.js | 3 ++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 41f19948..5376340a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -17,10 +17,10 @@ - - + + - + @@ -654,7 +654,7 @@ - @@ -730,7 +730,7 @@ - + @@ -748,7 +748,7 @@ - + @@ -766,7 +766,7 @@ - + @@ -784,7 +784,7 @@ - + @@ -817,7 +817,7 @@ - + @@ -827,7 +827,7 @@ - + @@ -845,7 +845,7 @@ - + @@ -863,7 +863,7 @@ - + @@ -893,10 +893,10 @@ - - + + - + diff --git a/js/snake.js b/js/snake.js index 0e37c28d..04cb9d86 100644 --- a/js/snake.js +++ b/js/snake.js @@ -287,6 +287,7 @@ SNAKE.Snake = SNAKE.Snake || (function () { } } } + directionEvent.preventDefault(); }; @@ -1116,8 +1117,8 @@ SNAKE.Board = SNAKE.Board || (function () { if (me.getBoardState() === BOARD_STATE_GAME_STARTING) { me.setBoardState(BOARD_STATE_GAME_STARTED); // start the game! mySnake.go(); - console.log("Added"); SNAKE.addEventListener(elmContainer, "touchstart", mySnake.handleDeviceTouch, false); + mySnake.handleDeviceTouch(evt); } } }; From ee68f5627c4682d216e34d70ace8a5c3ae2afb26 Mon Sep 17 00:00:00 2001 From: CydrickT Date: Wed, 19 Jul 2017 14:25:45 -0400 Subject: [PATCH 35/66] Improving --- .idea/workspace.xml | 34 +++++++++++++++++----------------- index.html | 1 + 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 5376340a..136e8421 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,7 +2,7 @@ - + - - - - - - - - - + @@ -901,6 +893,14 @@ + + + + + + + + diff --git a/index.html b/index.html index a6953d1c..5c511075 100755 --- a/index.html +++ b/index.html @@ -11,6 +11,7 @@ --> JavaScript Snake + - - - -
Select which mode you would like to play in.


- + + +
+ + + + +