-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
166 lines (162 loc) · 4.22 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
document.addEventListener("DOMContentLoaded", function () {
// Show popup on page load
document.getElementById("popup-container").style.display = "flex";
});
function startGame() {
// Hide popup
document.getElementById("popup-container").style.display = "none";
var audio = document.getElementById("myAudio");
function backchange(pre, obj) {
setTimeout(function () {
obj.style.backgroundColor = "white";
}, 0);
setTimeout(function () {
obj.style.backgroundColor = pre;
}, 70);
}
var aud1 = new Audio("l.mp3");
var aud2 = new Audio("l1.mp3");
var aud3 = new Audio("point.wav");
var aud4 = new Audio("tap.wav");
function changebg() {
document.body.style.backgroundColor = "red";
}
function addbutton() {
let but = document.createElement("button");
but.innerText = "Click to play Again!!";
let h3 = document.querySelector("h3");
h3.append(but);
but.className = "butt";
but.addEventListener("click", function () {
location.reload();
});
}
let obj1 = document.all[29];
let obj2 = document.all[30];
let obj3 = document.all[32];
let obj4 = document.all[33];
let objs = {
1: obj1,
2: obj2,
3: obj3,
4: obj4,
};
let arr = [];
let index = 0;
let p = document.querySelector("h3");
let x = Math.floor(Math.random() * 4) + 1;
setTimeout(function () {
backchange(objs[x].style.backgroundColor, objs[x]);
}, 2000);
arr.push(x);
let c = 0;
obj1.addEventListener("click", function (e) {
aud4.play();
let pre = obj1.style.backgroundColor;
backchange(pre, obj1);
y = obj1.className;
if (arr[index] == y) {
index++;
} else {
p.innerHTML = `your final Score: ${c} `;
changebg();
addbutton();
aud1.play();
aud2.play();
arr[index] = -1;
}
if (index == arr.length) {
aud3.play();
c++;
p.innerHTML = `Score: ${c}`;
x = Math.floor(Math.random() * 4) + 1;
arr.push(x);
setTimeout(function () {
backchange(objs[x].style.backgroundColor, objs[x]);
}, 1000);
index = 0;
}
});
obj2.addEventListener("click", function (e) {
aud4.play();
let pre = obj2.style.backgroundColor;
backchange(pre, obj2);
y = obj2.className;
if (arr[index] == y) {
index++;
} else {
p.innerHTML = `your final Score: ${c} `;
changebg();
addbutton();
aud1.play();
aud2.play();
arr[index] = -1;
}
if (index == arr.length) {
aud3.play();
c++;
p.innerHTML = `Score: ${c}`;
x = Math.floor(Math.random() * 4) + 1;
arr.push(x);
setTimeout(function () {
backchange(objs[x].style.backgroundColor, objs[x]);
}, 1000);
index = 0;
}
});
obj3.addEventListener("click", function (e) {
aud4.play();
let pre = obj3.style.backgroundColor;
backchange(pre, obj3);
y = obj3.className;
if (arr[index] == y) {
index++;
} else {
p.innerHTML = `your final Score: ${c} `;
changebg();
addbutton();
aud1.play();
aud2.play();
arr[index] = -1;
}
if (index == arr.length) {
aud3.play();
c++;
p.innerHTML = `Score: ${c}`;
x = Math.floor(Math.random() * 4) + 1;
arr.push(x);
setTimeout(function () {
backchange(objs[x].style.backgroundColor, objs[x]);
}, 1000);
index = 0;
}
});
obj4.addEventListener("click", function (e) {
aud4.play();
let pre = obj4.style.backgroundColor;
backchange(pre, obj4);
y = obj4.className;
if (arr[index] == y) {
index++;
} else {
p.innerHTML = `your final Score: ${c} `;
changebg();
addbutton();
aud1.play();
aud2.play();
arr[index] = -1;
}
if (index == arr.length) {
aud3.play();
c++;
p.innerHTML = `Score: ${c}`;
x = Math.floor(Math.random() * 4) + 1;
arr.push(x);
setTimeout(function () {
backchange(objs[x].style.backgroundColor, objs[x]);
}, 1000);
index = 0;
}
});
console.log("Simon Says game started!");
}