-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
301 lines (251 loc) · 9.07 KB
/
script.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Declare global variables
window.onload = begginningAnimation();
let computerSelection;
let playerSelection;
let computerScore = 0;
let playerScore = 0;
let buttons = document.querySelectorAll(".button");
const body = document.querySelector("body");
const main = document.querySelector("main");
const container = document.querySelector("#results-container")
const endAlert = document.querySelector("#end-alert")
const endDesc = document.querySelector("#end-desc")
const returnMainBtn = document.querySelector("#retry-btn")
const desc = document.querySelector("#desc3")
//skip the animation when clicking on the window
body.addEventListener("click", skipAnime)
body.addEventListener("keydown", skipAnime)
function skipAnime() {
const span = document.querySelectorAll("span");
span.forEach((span) => span.classList.add("skip"));
}
//animation
function begginningAnimation() {
fadeIn();
//turn span list into array
const desc1 = document.querySelector("#desc1");
let desc1Span = desc1.querySelectorAll("span");
desc1Span = Array.from(desc1Span);
const desc2 = document.querySelector("#desc2");
const desc3 = document.querySelector("#desc3");
desc1Span[desc1Span.length - 1].ontransitionend = () => {
desc1.classList.add("fade-out");
desc1.addEventListener("animationend", () => {
desc1.classList.add("disappear");
desc1.classList.remove("animate");
desc2.classList.remove("disappear");
desc2.classList.add("animate");
fadeIn();
//collect nodelist of span
let desc2Span = desc2.querySelectorAll("span");
desc2Span = Array.from(desc2Span);
desc2Span[desc2Span.length - 1].ontransitionend = () => {
desc2.classList.add("fade-out");
desc2.addEventListener("animationend", () => {
desc2.classList.add("disappear");
desc2.classList.remove("animate");
desc3.classList.remove("disappear");
desc3.classList.add("animate");
fadeIn();
let desc3Span = desc3.querySelectorAll("span");
desc3Span = Array.from(desc3Span);
desc3Span[desc3Span.length - 1].ontransitionend = () => {
const cta = document.querySelector("#cta");
cta.classList.add("drop-down");
cta.addEventListener("animationend", () => {
const gameCtn = document.querySelector( "#game-container");
setTimeout(function () {
gameCtn.classList.add("fade-in");
}, 300);
});
};
});
};
});
};
}
//fading in the letters
function fadeIn() {
let text = document.querySelector(".animate");
let strText = text.textContent;
let splitText = strText.split("");
text.textContent = "";
//append span tags to each charactere in the string
for (i = 0; i < splitText.length; i++) {
text.innerHTML += `<span>${splitText[i]}</span>`;
}
let char = 0;
let timer = setInterval(onTick, 30);
function onTick() {
const span = text.querySelectorAll('span')[char];
span.classList.add("fade");
char++;
//stop the function from running once the end of the string has been reached
if (char === splitText.length) {
complete();
return;
}
}
function complete() {
clearInterval(timer);
timer = null;
}
}
//buttons
buttons.forEach((button) => {
button.addEventListener("click", () => {
const img = button.querySelector("img")
playerSelection = img.alt.toLowerCase();
playRound(playerSelection, computerSelection);
if (playerScore == 5 || computerScore == 5) {
declareWinner();
}
})
})
//function to capitalize a string
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// generate a random value from myArray
function computerPlay () {
const myArray = ["Rock", "Paper", "Scissors"]
return myArray[Math.floor(Math.random() * myArray.length)];
}
// play Rock Scissor Paper
function playRound(playerSelection, computerSelection) {
computerSelection = computerPlay().toLowerCase();
playerSelection = playerSelection.toLowerCase();
// test the options
if (computerSelection == playerSelection) {
displayResults ("Tie game!");
} else if (
(computerSelection == "rock" && playerSelection == "scissors") ||
(computerSelection == "scissors" && playerSelection == "paper") ||
(computerSelection == "paper" && playerSelection == "rock")
) {
computerScore = ++computerScore
keepCpuScore();
if (computerScore == 1) {
displayResults (`Oooops... you lost!
${capitalize(computerSelection)} beats ${playerSelection}`
);
} else if (computerScore == 2) {
displayResults (`I am getting worried!
${capitalize(computerSelection)} beats ${playerSelection}`
);
} else if (computerScore == 3) {
displayResults (`Thats really not ok!! Wake up for life! Come on...
${capitalize(computerSelection)} beats ${playerSelection}`
);
} else if (computerScore == 4) {
displayResults (`I am almost giving up on you! You lost again baby!
${capitalize(computerSelection)} beats ${playerSelection}`
);
} else {
displayResults (`You definitely lost!!
${capitalize(computerSelection)} beats ${playerSelection}`
);
}
} else {
playerScore = ++playerScore;
keepPlayerScore();
if (playerScore == 1) {
displayResults (`You won. Very good!
${capitalize(playerSelection)} beats ${computerSelection}`
);
} else if (playerScore == 2) {
displayResults (`Now we are talking. You are on the right path.
${capitalize(playerSelection)} beats ${computerSelection}`
);
} else if (playerScore == 3) {
displayResults (`Waw! Almost in love with you.
${capitalize(playerSelection)} beats ${computerSelection}`
);
} else if (playerScore == 4) {
displayResults (`Yeahhhhhhhhhhhh... You are my hero!
${capitalize(playerSelection)} beats ${computerSelection}`
);
} else {
displayResults (`You won!! Congratulations.
${capitalize(playerSelection)} beats ${computerSelection}`
);
}
}
}
function displayResults(str) {
container.animate([{opacity: 0}, {opacity: 1}], {
duration: 300,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-out",
});
container.textContent = str;
}
function declareWinner() {
rplContent();
if (playerScore > computerScore) {
endDesc.textContent = "You win! Yuuuupiiiii...";
returnMainBtn.innerText = "Play again";
} else {
endDesc.textContent = "You lost! Shame on you motherfucker...";
returnMainBtn.innerText = "Try again";
}
fadeIn();
let endDescSpan = endDesc.querySelectorAll("span");
endDescSpan = Array.from(endDescSpan);
endDescSpan[endDescSpan.length - 1].ontransitionend = () => {
returnMainBtn.classList.add("fade-in");
/*returnMainBtn.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 00,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-in",
});*/
};
}
function rplContent() {
main.classList.add("disappear");
endAlert.classList.remove("disappear");
desc.classList.remove("animate");
endDesc.classList.add("animate");
returnMainBtn.addEventListener("click", () => {
main.classList.remove("disappear");
endAlert.classList.add("disappear");
desc.classList.add("animate");
returnMainBtn.classList.remove("fade-in");
resetGame()
})
}
function resetGame() {
fadeIn();
container.textContent = "";
playerScore = 0;
computerScore = 0;
keepPlayerScore();
keepCpuScore();
}
function keepPlayerScore() {
let playerScoreBox = document.querySelector("#player-score");
playerScoreBox.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: 300,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-out",
});
playerScoreBox.textContent = playerScore;
}
function keepCpuScore() {
let computerScoreBox = document.querySelector("#computer-score");
computerScoreBox.animate([{ opacity: 0}, { opacity: 1}], {
duration: 300,
fill: "forwards",
iterations: 1,
delay: 0,
easing: "ease-out",
});
computerScoreBox.textContent = computerScore;
}
//begginningAnimation();