forked from wdi-sg/sei-19
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptLevelTwo.js
More file actions
317 lines (215 loc) · 9.05 KB
/
Copy pathscriptLevelTwo.js
File metadata and controls
317 lines (215 loc) · 9.05 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Gotta Catch Em All! //
// What are the objectives of the game?
// To catch as many fruits as you can before time runs out!
// How do you play the game?
// Avoid catching the bomb as you will lose points.
// Game ends when the time is up or you click on Team Rocket.
// How can you make the game better?
// Different pokemons dropping
// How can you improve the code? KISS and DRY
//-------- Global Variables --------//
let fruitNum = 0;
let currentScore = 0;
let timeleft = 60;
let livesLeft = 3;
let pikachu = "https://img.pokemondb.net/sprites/black-white/anim/normal/pikachu.gif";
let pikachuBoom = "https://www.pokewiki.de/images/f/f3/Pok%C3%A9monsprite_025_Smaragd.gif"
let squirtle = "https://img.pokemondb.net/sprites/black-white/anim/normal/squirtle.gif"
let squirtleBoom = "https://www.pokewiki.de/images/e/e0/Pok%C3%A9monsprite_007_Smaragd.gif"
let bulbasaur = "https://img.pokemondb.net/sprites/black-white/anim/normal/bulbasaur.gif"
let bulbasaurBoom = "https://www.pokewiki.de/images/0/05/Pok%C3%A9monsprite_001_Smaragd.gif"
let charmander = "https://img.pokemondb.net/sprites/black-white/anim/normal/charmander.gif"
let charmanderBoom = "https://www.pokewiki.de/images/8/84/Pok%C3%A9monsprite_004_Smaragd.gif"
let pokemonChosen;
//-------- Functions --------//
// To make the fruit drop by adding to their position from the top
const fruitDrop = () => {
let fruitsInPlay = document.querySelector("#playArea").children;
for (i = 0; i < fruitsInPlay.length; i ++) {
let fruitChosen = fruitsInPlay[i].id;
let curPosition = document.querySelector("#" + fruitChosen).style.top;
let curPositionNum = parseFloat(curPosition);
let newPosition = curPositionNum + 0.4 + "%";
document.querySelector("#" + fruitChosen).style.top = newPosition;
}
}
// To create the fruit and randomize their starting position from the left at the top
const makeFruit = () => {
let randomNum0to5 = Math.floor(Math.random() * 6); // for the start position array
let randNum0to5 = Math.floor(Math.random() * 6); // for the fruit array
let startPosition = [20,30,40,50,60,70];
let fruitArray = ["🍍","🍒","🍌","🍓","🍎","💣"];
let positionFromLeft = startPosition[randomNum0to5];
let fruit = fruitArray[randNum0to5];
let newFruit = document.createElement('p');
newFruit.setAttribute('id', `fruit${fruitNum + 1}`);
newFruit.textContent = fruit;
newFruit.style.position = "absolute";
newFruit.style.left = `${positionFromLeft}%`;
newFruit.style.top = `0%`;
document.querySelector("#playArea").appendChild(newFruit);
fruitNum ++ ;
}
// To check when to remove the missed fruits from playing area
const removeFruit = () => {
let fruitsInPlay2 = document.querySelector("#playArea").children;
// console.log('Scanning the board')
for (i = 0; i < fruitsInPlay2.length; i ++) {
let fruitChosen2 = fruitsInPlay2[i].id;
let curPosition2 = document.querySelector("#" + fruitChosen2).style.top
let curPositionNum2 = parseFloat(curPosition2);
if (curPositionNum2 > 80) {
let elem = document.querySelector("#" + fruitChosen2);
elem.remove();
}
}
}
// To check when to add to player's current score
const checkScore = () => {
let fruitsInPlay3 = document.querySelector("#playArea").children;
// console.log('Scoring the board')
for (i = 0; i < fruitsInPlay3.length; i ++) {
let fruitChosen3 = fruitsInPlay3[i].id;
let curPosTopFruit = document.querySelector("#" + fruitChosen3).style.top;
let curPosTopFruitNum = parseFloat(curPosTopFruit);
let curPosLeftFruit = document.querySelector("#" + fruitChosen3).style.left;
let curPosLeftCatcher = document.querySelector("#catcher").style.left;
let fruitChosen3Text = document.querySelector("#" + fruitChosen3).textContent;
// What to do when Pokemon is able to catch the food. Plus 1 to current score.
if (curPosLeftFruit === curPosLeftCatcher && (curPosTopFruitNum > 72 && curPosTopFruitNum < 80) && fruitChosen3Text !== "💣" ) {
yumYum();
document.querySelector("#catcher").src = pikachu;
let elem = document.querySelector("#" + fruitChosen3);
elem.remove();
currentScore ++;
document.querySelector("#score").textContent = currentScore;
}
// What to do when Pokemon catches the bomb. Minus 1 from current score.
else if (curPosLeftFruit === curPosLeftCatcher && (curPosTopFruitNum > 72 && curPosTopFruitNum < 80) && fruitChosen3Text === "💣" ) {
boomBoom();
document.querySelector("#catcher").src = pikachuBoom;
let elem = document.querySelector("#" + fruitChosen3);
elem.remove();
currentScore --;
document.querySelector("#score").textContent = currentScore;
// What to do when Pokemon misses catching the fruit. Minus 1 from lives left.
} else if (curPosLeftFruit !== curPosLeftCatcher && curPosTopFruitNum > 80 && fruitChosen3Text !== "💣") {
livesLeft --;
let elem = document.querySelector("#" + fruitChosen3);
elem.remove();
let parentIcon = document.querySelector(".icon-list").children;
if (livesLeft === 2) {
parentIcon[2].classList.add("is-empty");
} else if (livesLeft === 1) {
parentIcon[1].classList.add("is-empty");
} else if (livesLeft === 0) {
parentIcon[0].classList.add("is-empty");
}
}
}
}
// To make the catcher at the bottom
const makePokemon = () => {
let newPokemon = document.createElement('img');
newPokemon.setAttribute('id', "catcher");
newPokemon.style.position = "absolute";
newPokemon.style.left = "80%";
newPokemon.style.bottom = "5%";
newPokemon.style.width = "100px";
newPokemon.src = pikachu;
document.querySelector("#catchArea").append(newPokemon);
}
// To make the countdown timer and update DOM accordingly
const gameTimer = () => {
timeleft-- ;
}
// When to stop the game
const whenToStopGame = () => {
document.querySelector("#time-left").textContent = timeleft + "s";
if (timeleft === 0) {
stopTheGame();
showTimesUpModal();
} else if (livesLeft === 0) {
stopTheGame();
showGameOverModal();
}
}
// When the game stops, clear all SetIntervals
const stopTheGame = () => {
clearInterval(dropThemFruits);
clearInterval(checkThemFruits);
clearInterval(makeThemFruits);
clearInterval(checkTheScore);
clearInterval(timer);
clearInterval(whenToStop);
}
const showGameOverModal = () => {
document.getElementById('dialog-dark-rounded').showModal();
}
const showTimesUpModal = () => {
document.getElementById('dialog-rounded').showModal();
document.getElementById('scoreModal').textContent = currentScore;
}
// Function for the soundbite when pokemon eats fruit
const yumYum = () => {
let sound = document.getElementById("yumyum");
sound.play();
}
// Function for the soundbite when pokemon eats a bomb
const boomBoom = () => {
let sound = document.getElementById("boom");
sound.play();
}
// When you press the left and right keys to move the catcher
document.onkeydown = function(event) {
let pokeball = document.querySelector("#catcher");
let curPosLeft;
let curPosLeftNum;
let newPostLeft;
switch (event.keyCode) {
case 37:
// When you press on the left arrow button
curPosLeft = pokeball.style.left;
curPosLeftNum = parseFloat(curPosLeft);
newPosLeft = curPosLeftNum - 10 + "%";
document.querySelector("#catcher").style.left = newPosLeft;
break;
case 39:
// When you press on the right arrow button
curPosLeft = pokeball.style.left;
curPosLeftNum = parseFloat(curPosLeft);
newPosLeft = curPosLeftNum + 10 + "%";
document.querySelector("#catcher").style.left = newPosLeft;
break;
}
};
//-------- SetIntervals --------//
makePokemon();
setTimeout(() => {
dropThemFruits = setInterval(fruitDrop,10);
checkThemFruits = setInterval(removeFruit,500)
checkTheScore = setInterval(checkScore,8)
timer = setInterval(gameTimer, 1000);
whenToStop = setInterval(whenToStopGame, 500)
},3000)
setTimeout(() => {
makeThemFruits = setInterval(makeFruit,2000);
},1000)
//-------- AddEventListeners --------//
document.querySelector("#restart-btn1").onclick = function () {
location.href = "levelTwo.html";
};
document.querySelector("#restart-btn2").onclick = function () {
location.href = "levelTwo.html";
};
document.querySelector("#mainMenu-btn1").onclick = function () {
location.href = "index.html";
};
document.querySelector("#mainMenu-btn2").onclick = function () {
location.href = "index.html";
};
document.querySelector("#nextLevel-btn").onclick = function () {
location.href = "levelThree.html";
};
//
//