Simple JavaScript Game with Source Code: You have no idea what you can do with javascript. JavaScript is one of the most demanding programming languages right now, there are so many libraries of JavaScript. So, There is a snake game built with JavaScript, HTML & CSS little bit. This is a very basic program. This snake game is like the legend game came with a Nokia Keypad phones.

You can call this a coding game or game with coding. In other words, this is javascript coding game. When I was learning web development, always I asked my instructor how can I create games in javascript. Now I am able to create very small programs in javascript. I am still learning, & And as much as I know, I share with you all. Because I believe sharing knowledge is the best way to increase knowledge.

Let’s come to our topic, How to make a basic snake game in javascript? The answer is in the form of source code. First, you must have good knowledge of HTML canvas, CSS & Javascript. Then you can understand this program easily.

You May Also Like:

Registration Form with Validation Example and Source Code

Basic JavaScript Calculator Source Code | HTML, CSS

Requirement For Create Snake Game

Knowledge of HTML.

Basic Knowledge Of CSS.

Good knowledge of JavaScript.

An IDE or notepad++

Note: If you don’t have knowledge in javascript, Then first learn. If you are learning javascript or want to learn, this demo also will be useful for you for the understanding the whole program.

Preview:

Let’s see a review of this game in GIF format.

JavaScript Snake Game Source Code

I have created just one file for this program because this program is small in length and size. I used little bit CSS, that’s why I put CSS in between <head> tag. Create an HTML file named “snakegame.html” or any name you want. Copy codes form here below and paste on HTML file.

snakegame.html <!doctype html> <!--Code By WebDevTrick (https://webdevtrick.com)--> <html lang="en"> <head> <meta charset="UTF-8"> <title>Simple JavaScript Snake Game| Webdevtrick.com</title> <style> canvas { display: block; position: absolute; border: 5px solid #009BFF; margin: auto; top: 0; bottom: 0; right: 0; left: 0; } </style> </head> <body> <script> var COLS = 26, ROWS = 26, EMPTY = 0, SNAKE = 1, FRUIT = 2, LEFT = 0, UP = 1, RIGHT = 2, DOWN = 3, KEY_LEFT = 37, KEY_UP = 38, KEY_RIGHT = 39, KEY_DOWN = 40, canvas, ctx, keystate, frames, score; grid = { width: null, height: null, _grid: null, init: function(d, c, r) { this.width = c; this.height = r; this._grid = []; for (var x=0; x < c; x++) { this._grid.push([]); for (var y=0; y < r; y++) { this._grid[x].push(d); } } }, set: function(val, x, y) { this._grid[x][y] = val; }, get: function(x, y) { return this._grid[x][y]; } } snake = { direction: null, last: null, _queue: null, init: function(d, x, y) { this.direction = d; this._queue = []; this.insert(x, y); }, insert: function(x, y) { this._queue.unshift({x:x, y:y}); this.last = this._queue[0]; }, remove: function() { return this._queue.pop(); } }; function setFood() { var empty = []; for (var x=0; x < grid.width; x++) { for (var y=0; y < grid.height; y++) { if (grid.get(x, y) === EMPTY) { empty.push({x:x, y:y}); } } } var randpos = empty[Math.round(Math.random()*(empty.length - 1))]; grid.set(FRUIT, randpos.x, randpos.y); } function main() { canvas = document.createElement("canvas"); canvas.width = COLS*20; canvas.height = ROWS*20; ctx = canvas.getContext("2d"); document.body.appendChild(canvas); ctx.font = "12px Helvetica"; frames = 0; keystate = {}; document.addEventListener("keydown", function(evt) { keystate[evt.keyCode] = true; }); document.addEventListener("keyup", function(evt) { delete keystate[evt.keyCode]; }); init(); loop(); } function init() { score = 0; grid.init(EMPTY, COLS, ROWS); var sp = {x:Math.floor(COLS/2), y:ROWS-1}; snake.init(UP, sp.x, sp.y); grid.set(SNAKE, sp.x, sp.y); setFood(); } function loop() { update(); draw(); window.requestAnimationFrame(loop, canvas); } function update() { frames++; if (keystate[KEY_LEFT] && snake.direction !== RIGHT) { snake.direction = LEFT; } if (keystate[KEY_UP] && snake.direction !== DOWN) { snake.direction = UP; } if (keystate[KEY_RIGHT] && snake.direction !== LEFT) { snake.direction = RIGHT; } if (keystate[KEY_DOWN] && snake.direction !== UP) { snake.direction = DOWN; } if (frames%7 === 0) { var nx = snake.last.x; var ny = snake.last.y; switch (snake.direction) { case LEFT: nx--; break; case UP: ny--; break; case RIGHT: nx++; break; case DOWN: ny++; break; } if (0 > nx || nx > grid.width-1 || 0 > ny || ny > grid.height-1 || grid.get(nx, ny) === SNAKE ) { return init(); } if (grid.get(nx, ny) === FRUIT) { score++; setFood(); } else { var tail = snake.remove(); grid.set(EMPTY, tail.x, tail.y); } grid.set(SNAKE, nx, ny); snake.insert(nx, ny); } } function draw() { var tw = canvas.width/grid.width; var th = canvas.height/grid.height; for (var x=0; x < grid.width; x++) { for (var y=0; y < grid.height; y++) { switch (grid.get(x, y)) { case EMPTY: ctx.fillStyle = "#fff"; break; case SNAKE: ctx.fillStyle = "#333"; break; case FRUIT: ctx.fillStyle = "#009BFF"; break; } ctx.fillRect(x*tw, y*th, tw, th); } } ctx.fillStyle = "#000"; ctx.fillText("SCORE: " + score, 10, canvas.height-10); } main(); </script> </body> </html> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 <!doctype html> <!--Code By WebDevTrick (https://webdevtrick.com)--> <html lang = "en" > <head> <meta charset = "UTF-8" > <title> Simple JavaScript Snake Game| Webdevtrick.com </title> <style> canvas { display : block ; position : absolute ; border : 5px solid #009BFF ; margin : auto ; top : 0 ; bottom : 0 ; right : 0 ; left : 0 ; } </style> </head> <body> <script> var COLS = 26 , ROWS = 26 , EMPTY = 0 , SNAKE = 1 , FRUIT = 2 , LEFT = 0 , UP = 1 , RIGHT = 2 , DOWN = 3 , KEY_LEFT = 37 , KEY_UP = 38 , KEY_RIGHT = 39 , KEY_DOWN = 40 , canvas , ctx , keystate , frames , score ; grid = { width : null , height : null , _grid : null , init : function ( d , c , r ) { this . width = c ; this . height = r ; this . _grid = [ ] ; for ( var x = 0 ; x < c ; x ++ ) { this . _grid . push ( [ ] ) ; for ( var y = 0 ; y < r ; y ++ ) { this . _grid [ x ] . push ( d ) ; } } } , set : function ( val , x , y ) { this . _grid [ x ] [ y ] = val ; } , get : function ( x , y ) { return this . _grid [ x ] [ y ] ; } } snake = { direction : null , last : null , _queue : null , init : function ( d , x , y ) { this . direction = d ; this . _queue = [ ] ; this . insert ( x , y ) ; } , insert : function ( x , y ) { this . _queue . unshift ( { x : x , y : y } ) ; this . last = this . _queue [ 0 ] ; } , remove : function ( ) { return this . _queue . pop ( ) ; } } ; function setFood ( ) { var empty = [ ] ; for ( var x = 0 ; x < grid . width ; x ++ ) { for ( var y = 0 ; y < grid . height ; y ++ ) { if ( grid . get ( x , y ) === EMPTY ) { empty . push ( { x : x , y : y } ) ; } } } var randpos = empty [ Math . round ( Math . random ( ) * ( empty . length - 1 ) ) ] ; grid . set ( FRUIT , randpos . x , randpos . y ) ; } function main ( ) { canvas = document . createElement ( "canvas" ) ; canvas . width = COLS * 20 ; canvas . height = ROWS * 20 ; ctx = canvas . getContext ( "2d" ) ; document . body . appendChild ( canvas ) ; ctx . font = "12px Helvetica" ; frames = 0 ; keystate = { } ; document . addEventListener ( "keydown" , function ( evt ) { keystate [ evt . keyCode ] = true ; } ) ; document . addEventListener ( "keyup" , function ( evt ) { delete keystate [ evt . keyCode ] ; } ) ; init ( ) ; loop ( ) ; } function init ( ) { score = 0 ; grid . init ( EMPTY , COLS , ROWS ) ; var sp = { x : Math . floor ( COLS /2), y:ROWS-1}; snake.init(UP, sp.x, sp.y); grid.set(SNAKE, sp.x, sp.y); setFood(); } function loop() { update(); draw(); window.requestAnimationFrame(loop, canvas); } function update() { frames++; if (keystate[KEY_LEFT] && snake.direction !== RIGHT) { snake.direction = LEFT; } if (keystate[KEY_UP] && snake.direction !== DOWN) { snake.direction = UP; } if (keystate[KEY_RIGHT] && snake.direction !== LEFT) { snake.direction = RIGHT; } if (keystate[KEY_DOWN] && snake.direction !== UP) { snake.direction = DOWN; } if (frames%7 === 0) { var nx = snake.last.x; var ny = snake.last.y; switch (snake.direction) { case LEFT: nx--; break; case UP: ny--; break; case RIGHT: nx++; break; case DOWN: ny++; break; } if (0 > nx || nx > grid.width-1 || 0 > ny || ny > grid.height-1 || grid.get(nx, ny) === SNAKE ) { return init(); } if (grid.get(nx, ny) === FRUIT) { score++; setFood(); } else { var tail = snake.remove(); grid.set(EMPTY, tail.x, tail.y); } grid.set(SNAKE, nx, ny); snake.insert(nx, ny); } } function draw() { var tw = canvas.width/g rid . width ; var th = canvas . height /grid.height; for (var x=0; x < grid.width; x++) { for (var y=0; y < grid.height; y++) { switch (grid.get(x, y)) { case EMPTY: ctx.fillStyle = "#fff"; break; case SNAKE: ctx.fillStyle = "#333"; break; case FRUIT: ctx.fillStyle = "#009BFF"; break; } ctx.fillRect(x*tw, y*th, tw, th); } } ctx.fillStyle = "#000"; ctx.fillText("SCORE: " + score, 10, canvas.height-10); } main(); </s cript > </body> </html>

That’s it. You have successfully created a snake game. If you have any doubt comment down below.

Thanks For Visiting, Keep Visiting.