-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
92 lines (88 loc) · 2.68 KB
/
app.js
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
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 <b>${level}</b> <br> Your Highest Score is <b> ${maxScore}</b> <br> 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);
}