diff --git "a/Screenshot 2024-06-05 at 12.24.40\342\200\257AM.png" "b/Screenshot 2024-06-05 at 12.24.40\342\200\257AM.png" new file mode 100644 index 0000000..1aa6c5d Binary files /dev/null and "b/Screenshot 2024-06-05 at 12.24.40\342\200\257AM.png" differ diff --git a/app.js b/app.js new file mode 100644 index 0000000..a3232fd --- /dev/null +++ b/app.js @@ -0,0 +1,92 @@ +let button = ["button-1", "button-2", "button-3", "button-4"]; +let gameseq = []; +let userseq = []; +let level = 0; +let count = 0; +let starter = false; +let h2 = document.querySelector("h2"); +document.addEventListener("keypress", function (event) { + if (!starter) { + console.log("Game Started..."); + starter = true; + } + if (event.code == "Enter") count++; + // console.log(event.code); + if (count<2) { + levelup(); + } +}); +let maxScore = 0; + +// creating a flash for a button +function gameflash(btn) { + btn.classList.add("flash"); //add the class name from css + setTimeout(function () { + btn.classList.remove("flash"); + }, 200); +} +// creating a user flash for a button +function userflash(btn) { + btn.classList.add("userflash"); //add the class name from css + setTimeout(function () { + btn.classList.remove("userflash"); + }, 200); +} +// Change the level and flash the button for level change +function levelup() { + userseq = []; + level++; + h2.innerText = `Level ${level}`; + // Random Color Generator + let randomNumber = Math.floor(Math.random() * button.length); //choosing a random number + let randomColor = button[randomNumber]; //getting the index value (class name) from color array by using randomNumber + let randombtn = document.querySelector(`.${randomColor}`); //selecting button according to class-name got from randomColor + // console.log(randomColor); + // console.log(randomNumber); + // console.log(randombtn); + gameseq.push(randomColor); + console.log(gameseq); + // calling btnflash function for flashing + gameflash(randombtn); +} +function checkans(idx) { + if (userseq[idx] === gameseq[idx]) { + if (userseq.length == gameseq.length) + { + setTimeout(levelup, 1000); + } + + } else { + if (maxScore < level) { + maxScore = level; + } + h2.innerHTML = `Game Over! Your score is ${level}
Your Highest Score is ${maxScore}
Press any key to start again`; + document.querySelector("body").style.backgroundColor = "red"; + setTimeout(function () { + document.querySelector("body").style.backgroundColor = "aquamarine"; + }, 100); + reset(); + } +} + +function reset() { + starter = false; + gameseq = []; + userseq = []; + level = 0; + count = 0; +} + +// calling userflash function +function btnpress() { + // console.log(this); + userflash(this); + userseq.push(this.getAttribute("id")); + // console.log(userseq); + checkans(userseq.length-1); +} +// User pressed the button +let allbtns = document.querySelectorAll(".btn"); +for (btns of allbtns) { + btns.addEventListener("click", btnpress); +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..7cd6dca --- /dev/null +++ b/index.html @@ -0,0 +1,25 @@ + + + + + + Simon Syes Game:Memory Booster + + + + +

Simon Game

+

Press any key to start the game

+
+
+

0

+

2

+
+
+

1

+

3

+
+
+ + + \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..aa728d0 --- /dev/null +++ b/style.css @@ -0,0 +1,42 @@ +body{ + text-align: center; + background-color: aquamarine; +} + +.box-container{ + display: flex; + justify-content: center; + +} + +.btn{ + height: 200px; + width: 200px; + border-radius: 20%; + border: 10px solid black; + margin: 1rem; + display: flex; + justify-content: center; + align-items: center; +} +p{ + font-size: 30px; +} +.button-1{ + background-color: #d95980; +} +.button-2{ + background-color: #63aac0; +} +.button-3{ + background-color: #f99b45; +} +.button-4{ + background-color: #819ff9; +} +.flash{ + background-color: #ffffff; +} +.userflash{ + background-color: black; +} \ No newline at end of file