From bd1bfb08572fc9295dcdb1b2b12ea397fb142fb1 Mon Sep 17 00:00:00 2001 From: Pranav Dudhane Date: Sat, 3 Oct 2020 11:11:21 +0530 Subject: [PATCH 1/2] Made easier to add levels --- app.js | 827 +++------------------------------------------------------ 1 file changed, 40 insertions(+), 787 deletions(-) diff --git a/app.js b/app.js index ea998da..bec08a7 100644 --- a/app.js +++ b/app.js @@ -3,792 +3,38 @@ document.addEventListener("DOMContentLoaded", () => { const width = 28; let score = 0; const grid = document.querySelector(".grid"); - const layout = [ - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 3, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 3, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 4, - 1, - 1, - 1, - 2, - 2, - 1, - 1, - 1, - 4, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 4, - 1, - 2, - 2, - 2, - 2, - 2, - 2, - 1, - 4, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 4, - 4, - 4, - 4, - 4, - 4, - 0, - 0, - 0, - 4, - 1, - 2, - 2, - 2, - 2, - 2, - 2, - 1, - 4, - 0, - 0, - 0, - 4, - 4, - 4, - 4, - 4, - 4, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 4, - 1, - 2, - 2, - 2, - 2, - 2, - 2, - 1, - 4, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 4, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 4, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 4, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 3, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 3, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 0, - 1, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - 1, - ]; + let winScore = null; + let layout = null; + const levels = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, + 1, 3, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 3, 1, + 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1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 1, 1, 0, 1, + 1, 3, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 3, 1, + 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, + 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, + 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, + 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, + 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,], + ] + const scores = [274,] // 0 - pac-dots // 1 - wall // 2 - ghost-lair @@ -799,6 +45,7 @@ document.addEventListener("DOMContentLoaded", () => { //create your board function createBoard() { + loadLevel("01"); for (let i = 0; i < layout.length; i++) { const square = document.createElement("div"); grid.appendChild(square); @@ -1063,7 +310,7 @@ document.addEventListener("DOMContentLoaded", () => { //check for a win - more is when this score is reached function checkForWin() { - if (score === 274) { + if (score === winScore) { ghosts.forEach((ghost) => clearInterval(ghost.timerId)); document.removeEventListener("keyup", movePacman); pacmanVelocity.x = 0; @@ -1090,5 +337,11 @@ document.addEventListener("DOMContentLoaded", () => { } } + function loadLevel(level){ + level = parseInt(level)-1 + winScore = scores[level] + layout = levels[level] + } + document.addEventListener("keydown", startGame); }); From 5ed0540cce18137d3e16adba825e29e5cf546141 Mon Sep 17 00:00:00 2001 From: Pranav Dudhane Date: Sat, 3 Oct 2020 11:57:30 +0530 Subject: [PATCH 2/2] Update app.js --- app.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/app.js b/app.js index bec08a7..46d290d 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,8 @@ document.addEventListener("DOMContentLoaded", () => { const grid = document.querySelector(".grid"); let winScore = null; let layout = null; + let pacmanCurrentIndex = null; + let ghostPos = []; const levels = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, @@ -17,12 +19,12 @@ document.addEventListener("DOMContentLoaded", () => { 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 4, 1, 1, 1, 2, 2, 1, 1, 1, 4, 1, 1, 0, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 0, 1, 1, 4, 1, 2, 2, 2, 2, 2, 2, 1, 4, 1, 1, 0, 1, 1, 1, 1, 1, 1, - 4, 4, 4, 4, 4, 4, 0, 0, 0, 4, 1, 2, 2, 2, 2, 2, 2, 1, 4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+ // 5 - ghosts + // 6 - pacman const squares = []; //create your board @@ -60,6 +63,10 @@ document.addEventListener("DOMContentLoaded", () => { squares[i].classList.add("ghost-lair"); } else if (layout[i] === 3) { squares[i].classList.add("power-pellet"); + } else if (layout[i] === 5){ + ghostPos.push(i) + } else if (layout[i] === 6) { + pacmanCurrentIndex = i; } } } @@ -67,7 +74,6 @@ document.addEventListener("DOMContentLoaded", () => { //create Characters //draw pacman onto the board - let pacmanCurrentIndex = 490; let pacmanVelocity = { x: 0, y: 0, @@ -236,10 +242,10 @@ document.addEventListener("DOMContentLoaded", () => { //all my ghosts ghosts = [ - new Ghost("blinky", 348, 100), - new Ghost("stinky", 376, 400), - new Ghost("inky", 351, 300), - new Ghost("clyde", 379, 200), + new Ghost("blinky", ghostPos[0], 100), + new Ghost("stinky", ghostPos[1], 400), + new Ghost("inky", ghostPos[2], 300), + new Ghost("clyde", ghostPos[3], 200), ]; //draw my ghosts onto the grid