-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
98 lines (68 loc) · 1.94 KB
/
Copy pathscript.js
File metadata and controls
98 lines (68 loc) · 1.94 KB
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
console.log("script is intalizing...");
let gameseq = [];
let userseq = [];
let button = ["yellow", "red", "blue", "purple"];
let started = false;
let level = 0;
let highestscore = 0;
let h3 = document.querySelector("h3");
let allbtn = document.querySelectorAll(".box");
let high = document.querySelector(".highest");
document.addEventListener("keypress", () => {
if (!started) {
console.log("game started")
started = true;
levelup();
}
})
let randombtn = Math.floor(Math.random() * 4);
function levelup() {
level++;
h3.innerText = `Level ${level}`;
let randomind = Math.floor(Math.random() * 4);
let randomcolor = button[randomind];
let randombtn = document.querySelector(`.${randomcolor}`);
btnflash(randombtn, "flash");
gameseq.push(randomcolor);
userseq = [];
}
function checkans(index) {
if (userseq[index] == gameseq[index]) {
if (userseq.length == gameseq.length) {
setTimeout(levelup, 500);
}
} else {
highestscore = Math.max(highestscore, level * 2);
h3.innerHTML = `Game Over ! Your Score was ${level * 2} <br><br>press any to restart`;
high.innerHTML = `Highest Score : ${highestscore}`;
document.querySelector("body").style.backgroundColor = "red";
setInterval(() => {
document.querySelector("body").style.backgroundColor = "white";
}, 150)
reset();
}
}
let usercolor;
function btnflash(btn, cls) {
btn.classList.add(cls);
setTimeout(() => {
btn.classList.remove(cls);
}, 250)
usercolor = btn.getAttribute("id");
}
function btnpressed() {
let btn = this;
btnflash(btn, "userflash");
userseq.push(usercolor);
console.log(gameseq)
checkans(userseq.length - 1);
}
for (let btn of allbtn) {
btn.addEventListener("click", btnpressed);
}
function reset() {
started = false;
gameseq = [];
userseq = [];
level = 0;
}