From 6706bee2774449cc19e261fdeb8d9644a3235884 Mon Sep 17 00:00:00 2001 From: ninecents Date: Tue, 1 Nov 2022 23:52:15 +0800 Subject: [PATCH] JavaScript-Snake --- JavaScript-Snake/LICENSE | 7 + JavaScript-Snake/README.md | 28 + JavaScript-Snake/css/Senura-snake.css | 141 +++ JavaScript-Snake/css/dark-snake.css | 141 +++ JavaScript-Snake/css/green-snake.css | 143 +++ JavaScript-Snake/css/head-snake.css | 142 +++ JavaScript-Snake/css/images/Thumbs.db | Bin 0 -> 3584 bytes .../css/images/dark-snakeblock.png | Bin 0 -> 1030 bytes .../css/images/dead-dark-snakeblock.png | Bin 0 -> 1249 bytes JavaScript-Snake/css/images/deadblock.png | Bin 0 -> 240 bytes .../css/images/deadblock_border.png | Bin 0 -> 117 bytes JavaScript-Snake/css/images/favicon.png | Bin 0 -> 512 bytes .../css/images/green-body-snakeblock.png | Bin 0 -> 5205 bytes .../css/images/green-head-snakeblock.png | Bin 0 -> 5506 bytes .../css/images/matrix-food-block.png | Bin 0 -> 723 bytes .../css/images/matrix-snake-block.png | Bin 0 -> 508 bytes JavaScript-Snake/css/images/snakeblock.png | Bin 0 -> 119 bytes JavaScript-Snake/css/light-snake.css | 124 ++ JavaScript-Snake/css/main-snake.css | 135 ++ JavaScript-Snake/css/matrix-snake.css | 134 ++ JavaScript-Snake/css/teal-snake.css | 144 +++ JavaScript-Snake/index.html | 147 +++ JavaScript-Snake/js/init.js | 5 + JavaScript-Snake/js/snake.js | 1103 +++++++++++++++++ JavaScript-Snake/package.json | 25 + 25 files changed, 2419 insertions(+) create mode 100644 JavaScript-Snake/LICENSE create mode 100644 JavaScript-Snake/README.md create mode 100644 JavaScript-Snake/css/Senura-snake.css create mode 100644 JavaScript-Snake/css/dark-snake.css create mode 100644 JavaScript-Snake/css/green-snake.css create mode 100644 JavaScript-Snake/css/head-snake.css create mode 100644 JavaScript-Snake/css/images/Thumbs.db create mode 100644 JavaScript-Snake/css/images/dark-snakeblock.png create mode 100644 JavaScript-Snake/css/images/dead-dark-snakeblock.png create mode 100644 JavaScript-Snake/css/images/deadblock.png create mode 100644 JavaScript-Snake/css/images/deadblock_border.png create mode 100644 JavaScript-Snake/css/images/favicon.png create mode 100644 JavaScript-Snake/css/images/green-body-snakeblock.png create mode 100644 JavaScript-Snake/css/images/green-head-snakeblock.png create mode 100644 JavaScript-Snake/css/images/matrix-food-block.png create mode 100644 JavaScript-Snake/css/images/matrix-snake-block.png create mode 100644 JavaScript-Snake/css/images/snakeblock.png create mode 100644 JavaScript-Snake/css/light-snake.css create mode 100644 JavaScript-Snake/css/main-snake.css create mode 100644 JavaScript-Snake/css/matrix-snake.css create mode 100644 JavaScript-Snake/css/teal-snake.css create mode 100644 JavaScript-Snake/index.html create mode 100644 JavaScript-Snake/js/init.js create mode 100644 JavaScript-Snake/js/snake.js create mode 100644 JavaScript-Snake/package.json diff --git a/JavaScript-Snake/LICENSE b/JavaScript-Snake/LICENSE new file mode 100644 index 0000000..7cf19bc --- /dev/null +++ b/JavaScript-Snake/LICENSE @@ -0,0 +1,7 @@ +Copyright © Patrick Gillespie, http://patorjk.com + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/JavaScript-Snake/README.md b/JavaScript-Snake/README.md new file mode 100644 index 0000000..7942c5f --- /dev/null +++ b/JavaScript-Snake/README.md @@ -0,0 +1,28 @@ +# JavaScript Snake Game + +This is a DOM-based game of Snake that I wrote in JavaScript over a decade ago. It was made to have sort of a nostalgic feel to it. + +## Play and Edit the Game Online! + +You can now play and edit the game live in codesandbox: + +https://codesandbox.io/s/github/patorjk/JavaScript-Snake?file=/index.html + +On first load sometimes the game frame will not load correctly and you'll need to press the refresh icon above its display panel to get the game to show. + +Original game is located here: + +http://patorjk.com/games/snake + + +## How to use +The index.html file should give an idea of how to use this code. However, below you can see how to initialize it into any div within a webpage. + + var mySnakeBoard = new SNAKE.Board( { + boardContainer: "game-area", + fullScreen: false, + width: 580, + height:400 + }); + +The comments within the source code are formatted a little strange because at the time I was playing around with YUI Doc which generates documentation from code. Kind of sucks that there's so much churn in the JavaScript world. However, I'm glad the rest of the code doesn't use any external libraries, as this game still works the same after over a decade. diff --git a/JavaScript-Snake/css/Senura-snake.css b/JavaScript-Snake/css/Senura-snake.css new file mode 100644 index 0000000..180ea4a --- /dev/null +++ b/JavaScript-Snake/css/Senura-snake.css @@ -0,0 +1,141 @@ +/* +JavaScript Snake +By Patrick Gillespie +http://patorjk.com/games/snake +*/ + +body { + margin:0px; + padding:0px; + background-color:#000000; +} + +.snake-toolbar { + color: #847a87; +} + +#game-area { + margin:10px; + padding:0px; +} + +#game-area:focus { outline: none; } + +#mode-wrapper { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #f6f0f7; +} + +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: #605d61; +} + +a.snake-link:hover { + color: #a500d6; +} + +.snake-pause-screen { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position:absolute; + width:300px; + height:80px; + text-align:center; + top:50%; + left:50%; + margin-top:-40px; + margin-left:-150px; + display:none; + background-color:#ffffff; + color: #938996; +} + +.snake-panel-component { + position: absolute; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #cf6d6d; + text-align: center; + background-color: #550b70; + padding: 8px; + margin: 0px; +} + +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: #eddff2; + position: absolute; + border: 0px solid black; + background-repeat: no-repeat; +} + +.snake-snakebody-alive { + background-image: url('./images/dark-snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./images/dead-dark-snakeblock.png'); +} + +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: black; + border: 2px solid #000000; + position: absolute; +} + +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: #fcfcfc; + border: 3px solid black; +} + +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + background-color: #3E2E44; + position: relative; +} + +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: black; + color: #ab00de; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: black; + color: #ab00de; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/css/dark-snake.css b/JavaScript-Snake/css/dark-snake.css new file mode 100644 index 0000000..ab8728d --- /dev/null +++ b/JavaScript-Snake/css/dark-snake.css @@ -0,0 +1,141 @@ +/* +JavaScript Snake +By Patrick Gillespie +http://patorjk.com/games/snake +*/ + +body { + margin:0px; + padding:0px; + background-color:#3E2E44; +} + +.snake-toolbar { + color: #938996; +} + +#game-area { + margin:10px; + padding:0px; +} + +#game-area:focus { outline: none; } + +#mode-wrapper { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #938996; +} + +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: #938996; +} + +a.snake-link:hover { + color: #938996; +} + +.snake-pause-screen { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position:absolute; + width:300px; + height:80px; + text-align:center; + top:50%; + left:50%; + margin-top:-40px; + margin-left:-150px; + display:none; + background-color:#3E2E44; + color: #938996; +} + +.snake-panel-component { + position: absolute; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #938996; + text-align: center; + background-color: #3E2E44; + padding: 8px; + margin: 0px; +} + +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: #3E2E44; + position: absolute; + border: 0px solid black; + background-repeat: no-repeat; +} + +.snake-snakebody-alive { + background-image: url('./images/dark-snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./images/dead-dark-snakeblock.png'); +} + +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: black; + border: 2px solid #3E2E44; + position: absolute; +} + +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: #312E44; + border: 3px solid black; +} + +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + background-color: #3E2E44; + position: relative; +} + +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: black; + color: #938996; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: black; + color: #938996; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/css/green-snake.css b/JavaScript-Snake/css/green-snake.css new file mode 100644 index 0000000..17a347a --- /dev/null +++ b/JavaScript-Snake/css/green-snake.css @@ -0,0 +1,143 @@ +/* +JavaScript Snake +By Patrick Gillespie +http://patorjk.com/games/snake +*/ +body { + margin:0px; + padding:0px; + background-color: darkgreen; +} + +.snake-toolbar { + background-color: rgba(255,255,255,0.4); + border-radius: 10px; +} + +#game-area { + margin:10px; + padding:0px; + background-color: lightgreen; +} + +#mode-wrapper { + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + +} + +#game-area:focus { outline: none; } + +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: black; +} + +a.snake-link:hover { + color: #FfFf54; +} + +.snake-pause-screen { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position:absolute; + width:300px; + height:80px; + text-align:center; + top:50%; + left:50%; + margin-top:-40px; + margin-left:-150px; + display:none; + background-color:black; + color:white; +} + +.snake-panel-component { + position: absolute; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: black; + text-align: center; + background-color: white; + padding: 8px; + margin: 0px; +} + +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: #FF0000; + position: absolute; + border: 0px solid #000080; + background-repeat: no-repeat; +} + +.snake-snakebody-alive { + background-image: url('./images/snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./images/deadblock.png'); +} + +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: black; + border: 0px solid #000080; + position: absolute; +} + +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: white; + border: 0px solid #0000A8; +} + +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + background-color: #FC5454; + position: relative; +} + +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/css/head-snake.css b/JavaScript-Snake/css/head-snake.css new file mode 100644 index 0000000..dc05c8f --- /dev/null +++ b/JavaScript-Snake/css/head-snake.css @@ -0,0 +1,142 @@ +body { + margin:0px; + padding:0px; + background-color: rgb(0, 0, 0); +} + +#game-area { + margin:10px; + padding:0px; +} + +#mode-wrapper { + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + +} + +#game-area:focus { outline: none; } + +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: #FCFC54; +} + +a.snake-link:hover { + color: #FfFf54; +} + +.snake-pause-screen { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position:absolute; + width:300px; + height:80px; + text-align:center; + top:50%; + left:50%; + margin-top:-40px; + margin-left:-150px; + display:none; + background-color:black; + color:white; +} + +.snake-panel-component { + position: absolute; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #ffffff; + text-align: center; + padding: 8px; + margin: 0px; +} + +#snake-snakehead-alive { + background-image: url('./images/green-head-snakeblock.png'); + margin: 0px; + padding: 0px; + background-color: rgb(10, 173, 10); + position: absolute; + border: 0px solid #000080; + background-repeat: no-repeat; + border-radius: 4px; +} + +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: #FF0000; + position: absolute; + border: 0px solid #000080; + background-repeat: no-repeat; +} + +.snake-snakebody-alive { + background-image: url('./images/green-body-snakeblock.png'), url('./images/green-body-snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./images/deadblock.png'), url('./images/deadblock.png'); +} + +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: rgb(182, 11, 11); + border: 0px solid #000080; + position: absolute; + border-radius: 6px; +} + +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: rgb(245, 245, 220); + border: 0px solid #0000A8; +} + +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + position: relative; +} + +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/css/images/Thumbs.db b/JavaScript-Snake/css/images/Thumbs.db new file mode 100644 index 0000000000000000000000000000000000000000..5892f1859a5a133c474967c2d8fc36ad4cdd72fd GIT binary patch literal 3584 zcmca`Uhu)fjZzO8(10BSGsD0CoD6J8;*3Bx2!nwD0|OI~0pkDr|NlQkkbwcn90fxt z1pWfu3W`4vW&uVbD>OcbkQYXsN;Sr0E8 zaOgbnY+do|O(z(cf$|NY#0CV05Q=P)g@K6?WIw1Z0@1`8bO_`KAaG_#1o}FWAqN-= z=@223NC99e2(lLjh|vrzIvHY!QAng3nB5?nNG)(>pg04C9Vj?p`5#o~vjZ^)5OV@C z7Z7s;F%J;)0x=&D^8+!c%n*dq$Zi;A51kMI#VIHaYy&2M|2O_WU=Z+f^K@fiWMpJW z078cUw}DxNnTZL6fPe*vSUFf(Sy))P*xA`Qc({0YxVgBwdHI9{c=-hRxVZ(y1O$ae zL`6k;_{Al}L?nbnL`6V`Ffs%6u&{EnvT};>a`TFi4E`Tr5aa-+$pmIbB?cxzMrJ|A z|3?_)fzD?Ix)$VMC}3m)x{-~YgOiIJs9-BFhcYrVGqEs(Tn$uR3zTPI5o8roG<0MW z4oqZMDikqloVbuf*=gfJ(V&YTRE(2~nmD<{#3dx9RMpfqG__1j&CD$WKS#c@69{;yl(wme1fGL-^|!0}nGJF!GoM8SEMU@_syH zb!OJtgWD#DM|8Pu=w?>vb+_)k`6u%6tdrCIuRgdWpnJdL*}kcpj+h=(iuao{|KI+l zA_0u1ZR_)wxCy;JQXKD9Zpd5MG5zv#_5Td(UC*{{WSh!$y!4Uf`V{NA%`r)afA7t$ z|Ml-mpaXYl;_BNk3sl16l;?eN-sKL|b^N=#EJ$+tHmSY)=CTyrt>~-X{&VRB(_;el zC58Vg7j-e{uQ9EzwY#UGx5wS~a?PbvQjfX+op}7`@x5NB!x7szZSUe-M<> z?{4ltA9ZOknp*S5apW%8y6dCJr^a28-(wj37o2$cpW)iN_Cw!xwa(j_Ek1vBtb<~5 zvFXq0r*&^=)cySO_Qyq6hWh_EVac96S^!wmgK|y+6#D>aa88={XwGF|U0uYG3alIB u88X4GgH&Mqpcq(RW`Z%O){F<&oQVu6U=`^MdcayVlOYXg$|x8LApijC$|I@( literal 0 HcmV?d00001 diff --git a/JavaScript-Snake/css/images/dark-snakeblock.png b/JavaScript-Snake/css/images/dark-snakeblock.png new file mode 100644 index 0000000000000000000000000000000000000000..66c4f1c2779f4bc29d51e16878bb60124560afd4 GIT binary patch literal 1030 zcmex=_1P|rX?qqI0P zFI~aY%U!`Mz|~!$%*;qrN1?DZF(6Oj-S5fuR$!pIEN!@|nR z%E~Fi%grl7GWdUhL6C#Nh{1@NQHg;`kdaxC@&6G9d7vj*8Nq-73K*GyZe(NU;N;>4 zD%dK(z{JSR%*4VBay3wOEl{3;MUYiU(a@1iI53f2sZhkIapFP_Wv7h?MT0JWP%%y_ zYU1P)6PJ*bQdLve(9|+9H8Z!cv~qTFb#wRd^a>6M4GWKmj7m;PO-s+n%qlJ^Ei136 ztZHs)ZENr7?3y%r%G7DoXUv?nXz`Mz%a*TLxoXqqEnBy3-?4Mop~FXx9y@;G&P778mFHFAhJOR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LNkvXXu0mjfRFY0v literal 0 HcmV?d00001 diff --git a/JavaScript-Snake/css/images/deadblock.png b/JavaScript-Snake/css/images/deadblock.png new file mode 100644 index 0000000000000000000000000000000000000000..e3d078429747ec9553af61ce0ea03a80aa141f21 GIT binary patch literal 240 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1SFYWcSQjymUKs7M+SzCeKo%WCj*q#3U&n!O+0h?`1Bc=Iy!T*8yOTDG%oQ7IEZAjaVT_|FtZ4_&SGThP&&)N=rS>_ sfnkw{u>(Wk5=o+!NoZ=eF);BkyqU{VsoGdH1?UV0Pgg&ebxsLQ00!bb%>V!Z literal 0 HcmV?d00001 diff --git a/JavaScript-Snake/css/images/deadblock_border.png b/JavaScript-Snake/css/images/deadblock_border.png new file mode 100644 index 0000000000000000000000000000000000000000..94b5628e7120bb4105ce8fb1d0d9bd9fab563484 GIT binary patch literal 117 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1SFYWcSQjymUKs7M+SzC{oH>NS%G{VPZ!4! z3;*Ol!a|1}%owul9K;xITCJMdvOt>GK!}NZNr}mUwx|ZiPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!~g&e!~vBn4jTXf0hLKaK~y+TjZ!-- z1Yr>V{@p#VI0@TGM5EJ-L?MSrC~=i2Gzy`n^GIk=Nr*;Cd0Zldf}lK=!Xqw6;&C|l zh(x%PyNvy3*Rs1;+_(96X1@G0JM+y7GJt;&LRj7(Tvu#3l4asCts#YXjuoiLB8fq< zP_%$2d6Om(D?}~E#J8Hpyg2m~P%O)d8#Ia`u>?h^%tkj`<@nI$RrdqgX^LlpHQfP14ri;>s`2)a<`q@xWjG8bFh||kT|7raryrr#TYCg*S@zRZqK{T-STde7LtQlf z+(v=*dX9|_*lNZLdSeYTz856okC>!(Nih`TNq!}86T632p`cRtF`rAB+hDcj9P%f~ zx>h8i2vgJYIu-l<5A4g?W$X5;+)5V1@0{{35#a}u@xCBD+w3<00000L literal 0 HcmV?d00001 diff --git a/JavaScript-Snake/css/images/green-body-snakeblock.png b/JavaScript-Snake/css/images/green-body-snakeblock.png new file mode 100644 index 0000000000000000000000000000000000000000..fa9d23de3d9d531e11aff732b22f1fa5aa2fb5b0 GIT binary patch literal 5205 zcmeHKX;f3!77pNm5u8W`6-$T|MVsVi9weY93~3F7Nw8?;=H?~@$Y2sozzKp-6-5NA zl_GmcDAe%b@xfu*Kv0KOsg)9042;#rsbDOumq}14 zeO-H)JmcAnF@4o;J=WIF<0>9dL`tBd3A5~kvEeqQtg~$G>!4Obl0|Cpt~{Rcb?xeH zy<95?sA6R}}2h^cCzNSos75 zWcsHZ$(`DMq9EIDSlJ}B&$*OKPnxO0eq6`2w2|lIkGBTh=)0Rt-}HRNK%T`CY3ZKo z!)M&3hZ)0OsH`*BaXJ#4EfU(NIEDsCHfauL| zuEd?$s)APx|NGP-!}n7kwWY6k)iI^mFWFMO5F-;7d1ntK}y2%9%E969tj!3B2>DE917?UAPgUY2Gn^c}%*B3RN4%onJ>O zbX$}ka^d%sSC<3Eb3?**u64P*GqBLrkHr7ZZF+rQ)Fa1|mMxk)i_{@^_Uz=B?7O-1 z=$i9?I2ILVq;3t2x>)$t)>-$89fZ-NH-^X51Xc>dWk;v2h<{cSRK0jHu`CVHN8~!l zTJj4vuw}R{l5FzLt3lH3vCg+99y%^A`gvRNZ%>LQpWxmh)>elU{1gSwf~{T@P5!a! zHE4*LJ^rT6`ea|B`>&SPk0Y|;iX4(CZSyHhEEgmX%fr}7)7?f4Z!W~9652hsEU&4$ z_$vHl@=xk(g}ruHSeYGDT2DXenRgVdU;Ogfv3bXAHU?LRtpf6Bk&8V z>pAsRbV|xd+dRfGn_bXP2UKLjxO7+M-kNav)dY{4C~BR0f4MT9J8NlW^j&(c{T{FJ z%~9hs#$>QN`rViN^_)6>=$ka?(UZ_X;g41r)|+kpIe#n$A|5*~$USjjVWtS3m9$5N zu|95WjPL&1n$&0UAOF^X@u%9#6O~;hv&#P@zRJDfnZJB^Q-kJFQ}Wx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|JRH5FaiJd?C9(Gu`tpNNI!%GPDTGc^r?pob%q*m(Wh=)Vj% zQI{e?=Dcw#aw%X^5IN(eKTrXcmYmq+qatY~7d0;vwMuLm1UeTmzXDQtI01XHGa+Y2 zTDAhpU`_=VA}t%qJaghCQ?m5htTmZgWGf#XHIb2to|dLq(un(9S`kzLl{>rN0$BfzB*)GZooEQWuhnFQt(|jX7UQcR#3-D?+oNeEoi(1k`cM9e48S$Qu^7 zK*v}J?RjL+Yz!$pQgO#iS}K9Dha1Vwg(%6Z^tJmz&3oXU4bK*vSelVa`;)>`NtV|n zIL`8J7OPrA5Q|NX_RsQj%U68VtueDLACIV&JYEN$WWB+6>b!vt?37Edu-m};sf*Cy5EPF<=T;yp^?<**7 z5A3+HV1FE)NrvZHa4ME#zOKFUhY%Y@W@`W{kmO;_mv@Hg(ph)OWFGve3^rxoD?LXJ z%BVqlK9!XPzI~=XT)mPug`K;CZRix_xjZ%izxb(fyXTHhrdZpK@Z|BU9P8%#&}xH; zH7M(-NwgyCM2wu|>0cf>u;= literal 0 HcmV?d00001 diff --git a/JavaScript-Snake/css/images/matrix-snake-block.png b/JavaScript-Snake/css/images/matrix-snake-block.png new file mode 100644 index 0000000000000000000000000000000000000000..8416cc80369ba209c759c01f336e2f8386ed0006 GIT binary patch literal 508 zcmV?<-)2 zj#7Z)2QxsLh0~l5*fN;3lfA%aoMEi- z#cWV|zN$yqs#@4vNS%G{#PZ!4! z3;*PlgouLSXYA^>bP0l+XkKDnK4^ literal 0 HcmV?d00001 diff --git a/JavaScript-Snake/css/light-snake.css b/JavaScript-Snake/css/light-snake.css new file mode 100644 index 0000000..069cbd8 --- /dev/null +++ b/JavaScript-Snake/css/light-snake.css @@ -0,0 +1,124 @@ +/* +JavaScript Snake +By Patrick Gillespie +http://patorjk.com/games/snake +*/ + +body { + margin: 0px; + padding: 0px; + background-color: #f73378; +} +#game-area { + margin: 10px; + padding: 0px; +} + +#mode-wrapper { + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; +} +#game-area:focus { + outline: none; +} +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: #FCFC54; +} +a.snake-link:hover { + color: #FfFf54; +} +.snake-pause-screen { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + width: 300px; + height: 80px; + text-align: center; + top: 50%; + left: 50%; + margin-top: -40px; + margin-left: -150px; + display: none; + background-color: black; + color: white; +} +.snake-panel-component { + position: absolute; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #ffffff; + text-align: center; + padding: 8px; + margin: 0px; +} +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: #FF0000; + position: absolute; + border: 0px solid #000080; + background-repeat: no-repeat; +} +.snake-snakebody-alive { + background-image: url('./images/snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./images/deadblock.png'); +} +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: #6cfd6a; + border: 0px solid #000080; + position: absolute; +} +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: #ab003c; + border: 0px solid #ab003c; +} +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + position: relative; +} +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/css/main-snake.css b/JavaScript-Snake/css/main-snake.css new file mode 100644 index 0000000..82a05ad --- /dev/null +++ b/JavaScript-Snake/css/main-snake.css @@ -0,0 +1,135 @@ +/* +JavaScript Snake +By Patrick Gillespie +http://patorjk.com/games/snake +*/ +body { + margin:0px; + padding:0px; + background-color: #FC5454; +} + +#game-area { + margin:10px; + padding:0px; +} + +#mode-wrapper { + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + +} + +#game-area:focus { outline: none; } + +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: #FCFC54; +} + +a.snake-link:hover { + color: #FfFf54; +} + +.snake-pause-screen { + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position:absolute; + width:300px; + height:80px; + text-align:center; + top:50%; + left:50%; + margin-top:-40px; + margin-left:-150px; + display:none; + background-color:black; + color:white; +} + +.snake-panel-component { + position: absolute; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + color: #ffffff; + text-align: center; + padding: 8px; + margin: 0px; +} + +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: #FF0000; + position: absolute; + border: 0px solid #000080; + background-repeat: no-repeat; +} + +.snake-snakebody-alive { + background-image: url('./css/images/snakeblock.png'), url('./images/snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./css/images/deadblock.png'), url('./images/deadblock.png'); +} + +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: #FF0000; + border: 0px solid #000080; + position: absolute; +} + +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: #0000A8; + border: 0px solid #0000A8; +} + +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + position: relative; +} + +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: #000000; + color: #ffffff; + font-family: Verdana, arial, helvetica, sans-serif; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/css/matrix-snake.css b/JavaScript-Snake/css/matrix-snake.css new file mode 100644 index 0000000..5c8c79c --- /dev/null +++ b/JavaScript-Snake/css/matrix-snake.css @@ -0,0 +1,134 @@ +body { +margin:0px; +padding:0px; +background-color: #00ff11; +} + +#game-area { +margin:10px; +padding:0px; +background-color: #00ff11; +} + +#mode-wrapper { +color: #000000; +font-family: Verdana, arial, helvetica, sans-serif; +font-size: 14px; + +} + +#game-area:focus { outline: none; } + +a.snake-link, a.snake-link:link, a.snake-link:visited { +color: #FCFC54; +} + +a.snake-link:hover { +color: #FfFf54; +} + +.snake-pause-screen { +font-family: Verdana, arial, helvetica, sans-serif; +font-size: 14px; +position:absolute; +width:300px; +height:80px; +text-align:center; +top:50%; +left:50%; +margin-top:-40px; +margin-left:-150px; +display:none; +background-color:black; +color:white; +} + +.snake-panel-component { +position: absolute; +font-family: Verdana, arial, helvetica, sans-serif; +font-size: 14px; +color: #000000; +text-align: center; +background-color: #00ff11; +padding: 8px; +margin: 0px; +} + +.snake-snakebody-block { +margin: 0px; +padding: 0px; +background-color: #FF0000; +position: absolute; +border: 0px solid #000080; +background-repeat: no-repeat; +} + +.snake-snakebody-alive { +background-image: url('./images/matrix-snake-block.png'); +} +.snake-snakebody-dead { +background-image: url('./images/deadblock.png'); +} + +.snake-food-block { +margin: 0px; +padding: 0px; +background-color: #FF0000; +border: 0px solid #000080; +position: absolute; +background-image: url("./images/matrix-food-block.png") +} + +.snake-playing-field { +margin: 0px; +padding: 0px; +position: absolute; +background-color: #000000; +border: 0px solid #000000; +} + +.snake-game-container { +margin: 0px; +padding: 0px; +border-width: 0px; +border-style: none; +zoom: 1; +background-color: #00ff11; +position: relative; +} + +.snake-welcome-dialog { +padding: 8px; +margin: 0px; +background-color: #000000; +color: #00ff11; +font-family: Verdana, arial, helvetica, sans-serif; +font-size: 14px; +position: absolute; +top: 50%; +left: 50%; +width: 300px; +/height: 150px;/ +margin-top: -100px; +margin-left: -158px; +text-align: center; +display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { +padding: 8px; +margin: 0px; +background-color: #000000; +color: #ff0000; +font-family: Verdana, arial, helvetica, sans-serif; +font-size: 14px; +position: absolute; +top: 50%; +left: 50%; +width: 300px; +height: 100px; +margin-top: -75px; +margin-left: -158px; +text-align: center; +display: none; +} \ No newline at end of file diff --git a/JavaScript-Snake/css/teal-snake.css b/JavaScript-Snake/css/teal-snake.css new file mode 100644 index 0000000..f3ad07d --- /dev/null +++ b/JavaScript-Snake/css/teal-snake.css @@ -0,0 +1,144 @@ +/* +JavaScript Snake +By Patrick Gillespie +http://patorjk.com/games/snake +*/ +@import url('https://fonts.googleapis.com/css2?family=Electrolize&display=swap'); + + +body { + margin:0px; + padding:0px; + background-color:teal; +} + +.snake-toolbar { + font-family: Electrolize; + color: white; +} + +#game-area { + margin:10px; + padding:0px; +} + +#game-area:focus { outline: none; } + +#mode-wrapper { + font-family: Electrolize; + font-size: 14px; + color: whitesmoke; +} + +a.snake-link, a.snake-link:link, a.snake-link:visited { + color: white; +} + +a.snake-link:hover { + color: white; +} + +.snake-pause-screen { + font-family: Electrolize; + font-size: 16px; + position:absolute; + width:300px; + height:80px; + text-align:center; + top:50%; + left:50%; + margin-top:-40px; + margin-left:-150px; + display:none; + background-color:#3E2E44; + color: whitesmoke; +} + +.snake-panel-component { + position: absolute; + font-family: Electrolize; + font-size: 16px; + color: #938996; + text-align: center; + background-color: #3E2E44; + padding: 8px; + margin: 0px; +} + +.snake-snakebody-block { + margin: 0px; + padding: 0px; + background-color: orange; + position: absolute; + border: 0px solid black; + background-repeat: no-repeat; +} + +.snake-snakebody-alive { + background-image: url('./images/snakeblock.png'); +} +.snake-snakebody-dead { + background-image: url('./images/dead-dark-snakeblock.png'); +} + +.snake-food-block { + margin: 0px; + padding: 0px; + background-color: red; + border: 2px solid black; + position: absolute; +} + +.snake-playing-field { + margin: 0px; + padding: 0px; + position: absolute; + background-color: rgb(0, 180, 180); + border: 3px solid black; +} + +.snake-game-container { + margin: 0px; + padding: 0px; + border-width: 0px; + border-style: none; + zoom: 1; + background-color: #3E2E44; + position: relative; +} + +.snake-welcome-dialog { + padding: 8px; + margin: 0px; + background-color: black; + color: whitesmoke; + font-family: Electrolize; + font-size: 14px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + /*height: 150px;*/ + margin-top: -100px; + margin-left: -158px; + text-align: center; + display: block; +} + +.snake-try-again-dialog, .snake-win-dialog { + padding: 8px; + margin: 0px; + background-color: black; + color: whitesmoke; + font-family: Electrolize; + font-size: 16px; + position: absolute; + top: 50%; + left: 50%; + width: 300px; + height: 100px; + margin-top: -75px; + margin-left: -158px; + text-align: center; + display: none; +} diff --git a/JavaScript-Snake/index.html b/JavaScript-Snake/index.html new file mode 100644 index 0000000..5e95552 --- /dev/null +++ b/JavaScript-Snake/index.html @@ -0,0 +1,147 @@ + + + + + + JavaScript Snake + + + + + + +
+
+ Theme: + +
+
+ Mode: + +
+
+
+ + +
+
+ +
+
+ + + + + diff --git a/JavaScript-Snake/js/init.js b/JavaScript-Snake/js/init.js new file mode 100644 index 0000000..93de8de --- /dev/null +++ b/JavaScript-Snake/js/init.js @@ -0,0 +1,5 @@ +var mySnakeBoard = new SNAKE.Board({ + boardContainer: "game-area", + fullScreen: true, + premoveOnPause: false +}); diff --git a/JavaScript-Snake/js/snake.js b/JavaScript-Snake/js/snake.js new file mode 100644 index 0000000..556637f --- /dev/null +++ b/JavaScript-Snake/js/snake.js @@ -0,0 +1,1103 @@ +/* +JavaScript Snake +First version by Patrick Gillespie - I've since merged in a good number of github pull requests +http://patorjk.com/games/snake +*/ + +/** +* @module Snake +* @class SNAKE +*/ + +var SNAKE = SNAKE || {}; +window.SNAKE = SNAKE; // this will allow us to access the game in other JS files when the app is loaded up in a codesandbox.com sandbox, that's the only reason it's here + +/** +* @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) { + obj.addEventListener(event, funct, evtCapturing); + }; + } else if (window.attachEvent) { + 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() { + if (window.removeEventListener) { + return function(obj, event, funct, evtCapturing) { + obj.removeEventListener(event, funct, evtCapturing); + }; + } else if (window.detachEvent) { + 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; + } + } + 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;} + if (localStorage.jsSnakeHighScore === undefined) localStorage.setItem('jsSnakeHighScore', 0); + + // ----- private variables ----- + + var me = this, + playingBoard = config.playingBoard, + myId = instanceNumber++, + growthIncr = 5, + lastMove = 1, + preMove = -1, + isFirstMove = true, + isFirstGameMove = true, + currentDirection = -1, // 0: up, 1: left, 2: down, 3: right + columnShift = [0, 1, 0, -1], + rowShift = [-1, 0, 1, 0], + xPosShift = [], + yPosShift = [], + snakeSpeed = 80, + isDead = false, + isPaused = false; + + function setModeListener (mode, speed) { + document.getElementById(mode).addEventListener('click', function () { snakeSpeed = speed; }); + } + + var modeDropdown = document.getElementById('selectMode'); + if ( modeDropdown ) { + modeDropdown.addEventListener('change', function(evt) { + evt = evt || {}; + var val = evt.target ? parseInt(evt.target.value) : 75; + + if (isNaN(val)) { + val = 75; + } else if (val < 25) { + val = 75 + } + + snakeSpeed = val; + + setTimeout(function() { + document.getElementById('game-area').focus(); + }, 10); + }); + } + + //setModeListener('Easy', 100); + //setModeListener('Medium', 75); + //setModeListener('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.id = "snake-snakehead-alive"; + 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; + } + + tempBlock = new SnakeBlock(); + tempBlock.elm = tempNode; + playingBoard.getBoardContainer().appendChild( tempBlock.elm ); + blockPool[blockPool.length] = tempBlock; + } + + function recordScore() { + var highScore = localStorage.jsSnakeHighScore; + if (me.snakeLength > highScore) { + alert('Congratulations! You have beaten your previous high score, which was ' + highScore + '.'); + localStorage.setItem('jsSnakeHighScore', me.snakeLength); + } + } + + function handleEndCondition(handleFunc) { + 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; + handleFunc(); + } + + // ----- 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 "currentDirection", 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 && !config.premoveOnPause)) {return;} + + var snakeLength = me.snakeLength; + + //console.log("lastmove="+lastMove); + //console.log("dir="+keyNum); + + let directionFound = -1; + + switch (keyNum) { + case 37: + case 65: + directionFound = 3; + break; + case 38: + case 87: + directionFound = 0; + break; + case 39: + case 68: + directionFound = 1; + break; + case 40: + case 83: + directionFound = 2; + break; + } + if (currentDirection !== lastMove) // Allow a queue of 1 premove so you can turn again before the first turn registers + { + preMove = directionFound; + } + if (Math.abs(directionFound - lastMove) !== 2 && (isFirstMove || isPaused) || isFirstGameMove ) // Prevent snake from turning 180 degrees + { + currentDirection = directionFound; + isFirstMove = false; + isFirstGameMove = false; + } + }; + + /** + * 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, + 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 (currentDirection !== -1){ + lastMove = currentDirection; + if (preMove !== -1) // If the user queued up another move after the current one + { + currentDirection = preMove; // Execute that move next time (unless overwritten) + preMove = -1; + } + } + isFirstMove = true; + + newHead.col = oldHead.col + columnShift[lastMove]; + newHead.row = oldHead.row + rowShift[lastMove]; + newHead.xPos = oldHead.xPos + xPosShift[lastMove]; + newHead.yPos = oldHead.yPos + yPosShift[lastMove]; + + if ( !newHead.elmStyle ) { + newHead.elmStyle = newHead.elm.style; + } + + newHead.elmStyle.left = newHead.xPos + "px"; + newHead.elmStyle.top = newHead.yPos + "px"; + if(me.snakeLength>1){ + newHead.elm.id="snake-snakehead-alive"; + oldHead.elm.id = ""; + } + + + + // 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; + if (!me.eatFood()) { + me.handleWin(); + return; + } + setTimeout(function(){me.go();}, snakeSpeed); + } + }; + + /** + * This method is called when it is determined that the snake has eaten some food. + * @method eatFood + * @return {bool} Whether a new food was able to spawn (true) + * or not (false) after the snake eats food. + */ + 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; + + if (!playingBoard.foodEaten()) { + return false; + } + + //Checks if the current selected option is that of "Rush" + //If so, "increase" the snake speed + var selectDropDown = document.getElementById("selectMode"); + var selectedOption = selectDropDown.options[selectDropDown.selectedIndex]; + + if(selectedOption.text.localeCompare("Rush") == 0) + { + snakeSpeed > 30 ? snakeSpeed -=5 : snakeSpeed = 30; + } + + return true; + }; + + /** + * This method handles what happens when the snake dies. + * @method handleDeath + */ + me.handleDeath = function() { + //Reset speed + var selectedSpeed = document.getElementById("selectMode").value; + snakeSpeed = parseInt(selectedSpeed); + + handleEndCondition(playingBoard.handleDeath); + }; + + /** + * This method handles what happens when the snake wins. + * @method handleDeath + */ + me.handleWin = function() { + handleEndCondition(playingBoard.handleWin); + }; + + /** + * This method sets a flag that lets the snake be alive again. + * @method rebirth + */ + me.rebirth = function() { + isDead = false; + isFirstMove = true; + isFirstGameMove = true; + preMove = -1; + }; + + /** + * 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.elm.id = "snake-snakehead-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 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 + * @return {bool} Whether a food was able to spawn (true) or not (false). + */ + 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; + } + + 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 (and return false to indicate + // that the player beat the game) + numTries++; + if (numTries > 20000){ + return false; + } + } + + playingBoard.grid[row][col] = playingBoard.getGridFoodValue(); + fRow = row; + fColumn = col; + elmFood.style.top = row * playingBoard.getBlockHeight() + "px"; + elmFood.style.left = col * playingBoard.getBlockWidth() + "px"; + return true; + }; + }; +})(); + +/** +* 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; + } + } + 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; + } + + // ------------------------------------------------------------------------- + // 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, elmHighscorePanel, elmWelcome, elmTryAgain, elmWin, 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"; + elmAboutPanel.innerHTML = "more patorjk.com apps - source code - pat's youtube"; + + elmLengthPanel = document.createElement("div"); + elmLengthPanel.className = "snake-panel-component"; + elmLengthPanel.innerHTML = "Length: 1"; + + elmHighscorePanel = document.createElement("div"); + elmHighscorePanel.className = "snake-panel-component"; + elmHighscorePanel.innerHTML = "Highscore: " + (localStorage.jsSnakeHighScore || 0); + + elmWelcome = createWelcomeElement(); + elmTryAgain = createTryAgainElement(); + elmWin = createWinElement(); + + 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(elmHighscorePanel); + elmContainer.appendChild(elmWelcome); + elmContainer.appendChild(elmTryAgain); + elmContainer.appendChild(elmWin); + + mySnake = new SNAKE.Snake({playingBoard:me,startRow:2,startCol:2,premoveOnPause: config.premoveOnPause}); + 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."; + } + 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(1); + me.getBoardContainer().focus(); + }; + + var kbShortcut = function(evt) { + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + 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; + } + + function createGameEndElement(message, elmId, elmClassName) { + var tmpElm = document.createElement("div"); + tmpElm.id = elmId + myId; + tmpElm.className = elmClassName; + + var gameEndTxt = document.createElement("div"); + gameEndTxt.innerHTML = "JavaScript Snake

" + message + "

"; + var gameEndStart = document.createElement("button"); + gameEndStart.appendChild(document.createTextNode("Play Again?")); + + var reloadGame = function () { + tmpElm.style.display = "none"; + me.resetBoard(); + me.setBoardState(1); + me.getBoardContainer().focus(); + }; + + var kbGameEndShortcut = function (evt) { + if (boardState !== 0 || tmpElm.style.display !== "block") { return; } + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + if (keyNum === 32 || keyNum === 13) { + reloadGame(); + } + }; + SNAKE.addEventListener(window, "keyup", kbGameEndShortcut, true); + + SNAKE.addEventListener(gameEndStart, "click", reloadGame, false); + tmpElm.appendChild(gameEndTxt); + tmpElm.appendChild(gameEndStart); + return tmpElm; + } + + function createTryAgainElement() { + return createGameEndElement("You died :(", "sbTryAgain", "snake-try-again-dialog"); + } + + function createWinElement() { + return createGameEndElement("You win! :D", "sbWin", "snake-win-dialog"); + } + + function handleEndCondition(elmDialog) { + var index = Math.max(getNextHighestZIndex(mySnake.snakeBody), getNextHighestZIndex({ tmp: { elm: myFood.getFoodElement() } })); + elmContainer.removeChild(elmDialog); + elmContainer.appendChild(elmDialog); + elmDialog.style.zIndex = index; + elmDialog.style.display = "block"; + me.setBoardState(0); + } + + // --------------------------------------------------------------------- + // 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. There are 3 states: 0 - Welcome or Try Again dialog is present. 1 - User has pressed "Start Game" on the Welcome or Try Again dialog but has not pressed an arrow key to move the snake. 2 - The game is in progress and the snake is moving. + * @method getBoardState + * @return {Number} The state of the board. + */ + me.getBoardState = function() { + 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. + * @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; + var cTop, cLeft; + if (config.fullScreen === true) { + cTop = 0; + cLeft = 0; + cWidth = getClientWidth()-20; + cHeight = getClientHeight()-20; + + } 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 = 30 + "px"; + + elmHighscorePanel.style.top = pLabelTop; + elmHighscorePanel.style.left = cWidth - 140 + "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 + } + } + } + + myFood.randomlyPlaceFood(); + + myKeyListener = function(evt) { + if (!evt) var evt = window.event; + var keyNum = (evt.which) ? evt.which : evt.keyCode; + + if (me.getBoardState() === 1) { + 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; + + //console.log(keyNum); + if (keyNum === 32) { + if(me.getBoardState()!=0) + 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(2); // 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); + }; + + /** + * This method is called when the snake has eaten some food. + * @method foodEaten + * @return {bool} Whether a new food was able to spawn (true) + * or not (false) after the snake eats food. + */ + me.foodEaten = function() { + elmLengthPanel.innerHTML = "Length: " + mySnake.snakeLength; + if (mySnake.snakeLength > localStorage.jsSnakeHighScore) + { + localStorage.setItem("jsSnakeHighScore", mySnake.snakeLength); + elmHighscorePanel.innerHTML = "Highscore: " + localStorage.jsSnakeHighScore; + } + if (!myFood.randomlyPlaceFood()) { + return false; + } + return true; + }; + + /** + * This method is called when the snake dies. + * @method handleDeath + */ + me.handleDeath = function() { + handleEndCondition(elmTryAgain); + }; + + /** + * This method is called when the snake wins. + * @method handleWin + */ + me.handleWin = function () { + handleEndCondition(elmWin); + }; + + // --------------------------------------------------------------------- + // 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; + config.premoveOnPause = (typeof config.premoveOnPause === "undefined") ? false : config.premoveOnPause; + + if (config.fullScreen) { + SNAKE.addEventListener(window,"resize", function() { + me.setupPlayingField(); + }, false); + } + + me.setBoardState(0); + + if (config.boardContainer) { + me.setBoardContainer(config.boardContainer); + } + + }; // end return function +})(); diff --git a/JavaScript-Snake/package.json b/JavaScript-Snake/package.json new file mode 100644 index 0000000..a8e13a4 --- /dev/null +++ b/JavaScript-Snake/package.json @@ -0,0 +1,25 @@ +{ + "name": "javascript-snake", + "version": "1.0.0", + "description": "JavaScript Snake
By Patrick Gillespie
License: MIT
http://patorjk.com/games/snake", + "main": "index.html", + "scripts": { + "start": "parcel index.html --open", + "build": "parcel build index.html", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/patorjk/JavaScript-Snake.git" + }, + "author": "", + "license": "MIT", + "bugs": { + "url": "https://github.com/patorjk/JavaScript-Snake/issues" + }, + "homepage": "https://github.com/patorjk/JavaScript-Snake#readme", + "dependencies": {}, + "devDependencies": { + "parcel-bundler": "^1.6.1" + } +} -- GitLab