-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
99 lines (92 loc) · 2.46 KB
/
Copy pathapp.js
File metadata and controls
99 lines (92 loc) · 2.46 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
88
89
90
91
92
93
94
95
96
97
98
99
let body = document.querySelector("body");
let boxes = document.querySelectorAll(".box");
let mainBox = document.querySelector(".boxes");
let isRunning = false;
let level = 1;
let seq = [];
let count = 0;
let infoLine = document.querySelector("h2");
let isAndroid = false;
let isRestart = false;
function boxBlink(box) {
defaultColor = box.style.backgroundColor;
box.style.backgroundColor = "black";
setTimeout(() => {
box.style.backgroundColor = defaultColor;
}, 100)
}
function boxClick(box) {
defaultOpacity = box.style.opacity;
box.style.opacity = 0.8;
setTimeout(() => {
box.style.opacity = defaultColor;
}, 100)
}
function bodyBlink(body) {
defaultColor = body.style.backgroundColor;
body.style.backgroundColor = "red";
setTimeout(() => {
body.style.backgroundColor = defaultColor;
}, 100)
}
function randBoxBlink(boxColl) {
let randno = Math.floor(Math.random() * 4);
boxBlink(boxColl[randno]);
return randno;
}
function clickHandler(event) {
boxClick(event.target);
if (event.target.id[3] == seq[count++]) {
console.log("right box")
} else {
function gameOver() {
bodyBlink(body);
};
let id = setInterval(gameOver, 200);
setTimeout(() => { clearInterval(id) }, 400);
infoLine.innerText = `Game Over! Your Score was ${level - 1}\nPress any Key to Restart the Game`;
console.log("HERES THE BUG");
count--;
resetGame();
}
if (count == seq.length && isRunning) {
infoLine.innerText = `Level ${++level}`;
seq.push(randBoxBlink(boxes));
count = 0;
}
}
function resetGame() {
isRunning = false;
isAndroid = false;
isRestart = true;
seq = [];
level = 1;
count = 0;
if (!isAndroid) {
mainBox.removeEventListener("click", clickHandler);
} else {
mainBox.removeEventListener("touchstart", clickHandler);
}
}
function startGame() {
seq.push(randBoxBlink(boxes));
infoLine.innerText = `Level ${level}`;
if (!isAndroid) {
mainBox.addEventListener("click", clickHandler);
} else {
mainBox.addEventListener("touchstart", clickHandler);
}
}
body.addEventListener("keypress", () => {
if (!isRunning) {
isRunning = true;
startGame();
}
})
body.addEventListener("touchstart", () => {
isAndroid = true;
if (!isRunning && !isRestart) {
isRunning = true;
startGame();
}
})