Simple Maze Game Using Javascript With Source Code
The Simple Maze Game Using Javascript was developed using JavaScript, CSS and HTML, In this Simple Javascript Project With Source Code is a basic game created or built using primarily functional javascript works or parts.
A Maze Game Javascript can be quite engaging and fun for the user. Although HTML and CSS are important building blocks in web development, JavaScript is the show language that transforms websites from functional to entertaining. As a result, it’s no wonder that video games make the list.
This Javascript Project With Source Code also includes a downloadable Project With Source Code for free, just find the downloadable source code below and click to start downloading.
I have here a suggested list of Best JavaScript Projects with Source Code Free to download and I’m sure this can help you to improve your skills in JavaScript programming and web development as a whole.
To start executing a Simple Maze Game Using Javascript With Source Code, makes sure that you have any platform in creating a JavaScript, CSS, bootstrap, and HTML installed in your computer, in my case I will use Sublime Text.
Simple Maze Game Using Javascript With Source Code : Steps on how to create the project
Time needed: 5 minutes.
These are the steps on how to create Simple Maze Game Using Javascript With Source Code
- Step 1: Create a folder.
First, create a folder and named it.
- Step 2: Open the folder in “Sublime Text”.
Second ,after creating a folder, open it in “sublime text“.
- Step 3: Create a html file.
Third, create a “html” file and save it as “index.html“
The code given below is for the css module
1 2 3 4 5 6 7 8 9 |
<style> body { background-color: grey; } canvas { position: absolute; } </style> |
In this module which is the css module of the simple project.
The code given below is for the javascript module
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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
<script> //Loads up the game var w = window, d = document, e = d.documentElement, g = d.getElementsByTagName('body')[0]; var x = w.innerWidth || e.clientWidth || g.clientWidth, y = w.innerHeight || e.clientHeight || g.clientHeight; var width = x - 16, height = y - 16; var currentPosition; var N = 1 << 0, S = 1 << 1, W = 1 << 2, E = 1 << 3; var body = document.querySelectorAll('body'); var layout = [], fronteirTest = []; // Determines the size of the blocks var cellSize = 16, cellSpacing = 8, cellWidth = Math.floor((width - cellSpacing) / (cellSize + cellSpacing)), cellHeight = Math.floor((height - cellSpacing) / (cellSize + cellSpacing)), cells = new Array(cellWidth * cellHeight), // each cell’s edge bits frontier = []; var maxY = Math.floor((height - cellSpacing) / (cellSize + cellSpacing)) - 1, maxX = Math.floor((width - cellSpacing) / (cellSize + cellSpacing)) - 1; var canvas = document.createElement('canvas'); canvas.setAttribute("id", "canvas"); canvas.setAttribute("width", width); canvas.setAttribute("height", height); body[0].appendChild(canvas); var context = canvas.getContext("2d"); context.translate( Math.round((width - cellWidth * cellSize - (cellWidth + 1) * cellSpacing) / 2), Math.round((height - cellHeight * cellSize - (cellHeight + 1) * cellSpacing) / 2) ); var canvas2 = document.createElement('canvas'); canvas2.setAttribute("id", "canvas2"); canvas2.setAttribute("width", width); canvas2.setAttribute("height", height); body[0].appendChild(canvas2); var game = canvas2.getContext("2d"); game.translate( Math.round((width - cellWidth * cellSize - (cellWidth + 1) * cellSpacing) / 2), Math.round((height - cellHeight * cellSize - (cellHeight + 1) * cellSpacing) / 2) ); //color of the maze context.fillStyle = "#00ffff"; // Add a random cell and two initial edges. var start = (cellHeight - 1) * cellWidth; cells[start] = 0; fillCell(start); frontier.push({ index: start, direction: N }); frontier.push({ index: start, direction: E }); // Explore the frontier until the tree spans the graph. function run() { var done, k = 0; while (++k < 50 && !(done = exploreFrontier())); return done; }; function exploreFrontier() { if ((edge = popRandom(frontier)) == null) { layout.push({x: 0, y: maxY, d1: 0, d0: 0}) for (var i = layout.length - 1; i >= 0; i--) { if(layout[i].x === 0 && layout[i].y === maxY) { drawPlayer(layout[i]); } }; return true; } var edge, i0 = edge.index, d0 = edge.direction, i1 = i0 + (d0 === N ? -cellWidth : d0 === S ? cellWidth : d0 === W ? -1 : +1), x0 = i0 % cellWidth, y0 = i0 / cellWidth | 0, x1, y1, d1, open = cells[i1] == null, // opposite not yet part of the maze north, east, south, west; // makes the maze color context.fillStyle = open ? "#00ffff": "transparent"; if (d0 === N) fillSouth(i1), x1 = x0, y1 = y0 - 1, d1 = S, south = true; else if (d0 === S) fillSouth(i0), x1 = x0, y1 = y0 + 1, d1 = N, south = true; else if (d0 === W) fillEast(i1), x1 = x0 - 1, y1 = y0, d1 = E, east = true; else fillEast(i0), x1 = x0 + 1, y1 = y0, d1 = W, east = true; if (open) { fillCell(i1); cells[i0] |= d0, cells[i1] |= d1; context.fillStyle = "transparent"; if (y1 > 0 && cells[i1 - cellWidth] == null) fillSouth(i1 - cellWidth), frontier.push({ index: i1, direction: N }), south = true; if (y1 < cellHeight - 1 && cells[i1 + cellWidth] == null) fillSouth(i1), frontier.push({ index: i1, direction: S }), south = true; if (x1 > 0 && cells[i1 - 1] == null) fillEast(i1 - 1), frontier.push({ index: i1, direction: W }), east = true; if (x1 < cellWidth - 1 && cells[i1 + 1] == null) fillEast(i1), frontier.push({ index: i1, direction: E }), east = true; } layout.push({ open: open, x: x1, y: y1, d0: d0, d1: d1 }); } function fillCell(index) { var i = index % cellWidth, j = index / cellWidth | 0; context.fillRect(i * cellSize + (i + 1) * cellSpacing, j * cellSize + (j + 1) * cellSpacing, cellSize, cellSize); } function fillEast(index) { var i = index % cellWidth, j = index / cellWidth | 0; context.fillRect((i + 1) * (cellSize + cellSpacing), j * cellSize + (j + 1) * cellSpacing, cellSpacing, cellSize); } function fillSouth(index) { var i = index % cellWidth, j = index / cellWidth | 0; context.fillRect(i * cellSize + (i + 1) * cellSpacing, (j + 1) * (cellSize + cellSpacing), cellSize, cellSpacing); } function popRandom(array) { if (!array.length) return; var n = array.length, i = Math.random() * n | 0, t; t = array[i], array[i] = array[n - 1], array[n - 1] = t; return array.pop(); } //Changing the color and size of the balls function drawPlayer(position) { game.clearRect(0, 0, width, height); currentPosition = position; var playerX = position.x * cellSize + (position.x + 1) * cellSpacing; var playerY = position.y * cellSize + (position.y + 1) * cellSpacing var finishX = maxX * cellSize + (maxX + 1) * cellSpacing; var finishY = 0 * cellSize + (0 + 1) * cellSpacing game.beginPath(); game.arc(finishX + (cellSize / 2), finishY + (cellSize / 2), cellSize / 2, 0, 2 * Math.PI, false); game.fillStyle = "green"; game.fill(); game.beginPath(); game.arc(playerX + (cellSize / 2), playerY + (cellSize / 2), cellSize / 2, 0, 2 * Math.PI, false); game.fillStyle = "black"; game.fill(); } window.addEventListener("keydown", function(e) { var value = e.which; if(value === 37) moveWest(), e.preventDefault(); if(value === 38) moveNorth(), e.preventDefault(); if(value === 39) moveEast(), e.preventDefault(); if(value === 40) moveSouth(), e.preventDefault(); return false; }); function moveWest() { var newY = currentPosition.y; var newX = currentPosition.x - 1; var newPosition; if (newX < 0) return false; for (var i = layout.length - 1; i >= 0; i--) { if(layout[i].x === newX && layout[i].y === newY) { newPosition = layout[i]; } }; if(newPosition.x === maxX && newPosition.y === 0) { gameComplete(); } if (( currentPosition.d1 === W) || (newPosition.d1 === E)) { drawPlayer(newPosition); }; } function moveEast() { var newY = currentPosition.y; var newX = currentPosition.x + 1; var newPosition; if (newX > maxX) return false; for (var i = layout.length - 1; i >= 0; i--) { if(layout[i].x === newX && layout[i].y === newY) { newPosition = layout[i]; } }; if(newPosition.x === maxX && newPosition.y === 0) { gameComplete(); } if (( currentPosition.d1 === E) || (newPosition.d1 === W)) { drawPlayer(newPosition); }; } function moveNorth() { var newY = currentPosition.y - 1; var newX = currentPosition.x; var newPosition; if (newY < 0) return false; for (var i = layout.length - 1; i >= 0; i--) { if(layout[i].x === newX && layout[i].y === newY) { newPosition = layout[i]; } }; if(newPosition.x === maxX && newPosition.y === 0) { gameComplete(); } if (( currentPosition.d1 === N) || (newPosition.d1 === S)) { drawPlayer(newPosition); }; } function moveSouth() { var newY = currentPosition.y + 1; var newX = currentPosition.x; var newPosition; if (newY > maxY) return false; for (var i = layout.length - 1; i >= 0; i--) { if(layout[i].x === newX && layout[i].y === newY) { newPosition = layout[i]; } }; if(newPosition.x === maxX && newPosition.y === 0) { gameComplete(); } if (( currentPosition.d1 === S) || (newPosition.d1 === N)) { drawPlayer(newPosition); }; } //Changes the alert when you win the game function gameComplete() { alert('Wow! You won! Thats pretty neat!'); } (function() { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); function animate() { requestAnimationFrame(function() { if(!run()) { animate(); } }); } animate(); </script> |
In this module which is the javascript module of the simple project.
Project Output

ABOUT PROJECT | PROJECT DETAILS |
---|---|
Project Name : | Simple Maze Game Using Javascript |
Project Platform : | JavaScript |
Programming Language Used: | php,javascript,html,css |
Developer Name : | itsourcecode.com |
IDE Tool (Recommended): | Sublime |
Project Type : | Web Application |
Database: | None |
Upload Date: | July 13, 2021 |
Run Quick Virus Scan for secure Download
Run Quick Scan for secure DownloadDownloadable Source Code
Summary
This JavaScript Project With Source Code is simply in HTML, CSS, and JavaScript. To start creating this Simple Javascript Project With Source Code, make sure that you have basic knowledge in HTML, CSS and Javascript.
This Javascript Game Project also includes a downloadable Javascript Game Project With Source Code for free.
Related Articles
- Todo List In JavaScript With Source Code
- Currency Converter Project in JavaScript with Source Code
- Calculator In JavaScript Source Code
- [SOLVED] How To Make A Live Chat In JavaScript With Source Code
Inquiries
If you have any questions or suggestions about Simple Maze Game Using Javascript With Source Code , please feel free to leave a comment below.