-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
399 lines (378 loc) · 12.3 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
//html-element
const kiwiBird = document.querySelector(".kiwiBird");
const gameContainer = document.getElementById("gameContainer");
const switches = document.querySelector(".switches");
const scoreDisplay = document.querySelector("#score");
const livesDisplay = document.querySelector("#lives");
const kiwiSound = document.getElementById("kiwi");
const poopSound = document.getElementById("poop");
const deadSound = document.getElementById("dead");
const winSound = document.getElementById("win");
const gameOverSound = document.getElementById("gameOver");
const gameOver = `<h2>Game Over</h2>`;
const victory = `<h2>You Win!</h2><p>Go <a href="https://rebecka-oscarsson.github.io/tv-room/">watch tv<a/> or</p>`;
const start = `<h2>Kiwi</h2> <p>Use arrow keys and space bar <br>Collect the kiwis</p>`;
//variabler
let moveInterval = 100; //tid i millisekunder mellan att mat/fiende hoppar ett steg
let enemyInterval = 15000; //tid i millisekunder mellan att det kommer en fiende
let paused = false;
let croc = false; //avgör om det blir krokodil eller dinosaurie
let lives = 3;
let score = 0;
let jumping = false;
let muteSound = false;
//intervall
let dinoJump; //får dinosaurien att hoppa
let collisionDetection; // kollar kollisioner
let clearItems; //tar bort allt som kommer till vänsterkanten
let createFood; //gör bajs och kiwifrukt
let createEnemies;
let moveFood; //flyttar bajs och kiwifrukt
showDialog(start);
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" || (e.key === "Enter" && paused)) {
paused = !paused;
applyPausedStyles(paused);
}
});
function startGame() {
paused = false;
lives = 3;
score = 0;
moveInterval = 100;
enemyInterval = 15000;
scoreDisplay.textContent = score;
livesDisplay.textContent = lives;
kiwiBird.classList.remove("dead");
removeElements();
document.addEventListener("keydown", moveBird);
createFood = setInterval(createFoodItem, 5000);
createEnemies = setInterval(createEnemy, enemyInterval);
clearItems = setInterval(clearItem, 300);
collisionDetection = setInterval(checkCollision, 50);
}
function stopGame() {
removeAnimations();
document.removeEventListener("keydown", moveBird);
clearInterval(createFood);
clearInterval(createEnemies);
clearInterval(clearItems);
clearInterval(collisionDetection);
// clearInterval(dinoJump);
// clearInterval(moveFood);
paused = true;
}
//tar bort css-animationer
function removeAnimations() {
const foodElements = document.querySelectorAll(".food");
for (let index = 0; index < foodElements.length; index++) {
foodElements[index].classList.remove("zigzag");
foodElements[index].classList.remove("dinoJump");
}
}
//tar bort alla fiender och mat
function removeElements() {
const foodElements = document.querySelectorAll(".food");
for (let index = 0; index < foodElements.length; index++) {
gameContainer.removeChild(foodElements[index]);
}
}
//hoppa
function jump(element, jumptime) {
if (element.classList.contains("dino")) {
setTimeout(function () {
element.classList.add("dinoJump"); //det här ger dinosaurien annorlunda utseende när den hoppar
}, moveInterval * 3);
}
let counter = 0;
let bottom = 0;
let timer = setInterval(() => {
counter++;
bottom += 5;
if (counter == 15) {
clearInterval(timer);
let timerDown = setInterval(() => {
if (bottom <= 10) {
clearInterval(timerDown);
element.classList.remove("dinoJump");
} else {
bottom -= 5;
element.style.bottom = bottom + "%";
}
}, jumptime);
}
element.style.bottom = bottom + "%";
}, jumptime);
}
//flytta fågeln
function moveBird(evt) {
let moveDistance = 1;
let birdPosition = parseInt(kiwiBird.style.left);
switch (evt.key) {
case "ArrowLeft":
if (birdPosition > 0) {
kiwiBird.classList.add("mirror");
kiwiBird.style.left = birdPosition - moveDistance + "%";
break;
}
break;
case "ArrowRight":
if (birdPosition < 96) {
kiwiBird.classList.remove("mirror");
kiwiBird.style.left = birdPosition + moveDistance + "%";
break;
}
break;
case "ArrowUp":
case " ":
let birdBottom = parseInt(kiwiBird.style.bottom); //för att man inte ska kunna hoppa när man redan är i luften
if (birdBottom === 10) {
jump(kiwiBird, 70);
}
break;
}
}
//slumpa en siffra
function randomIntFromInterval(min, max) {
// min and max included
return Math.floor(Math.random() * (max - min + 1) + min);
}
//skapa fiende
function createEnemy() {
if (!paused) {
let foodPosition = 100;
let food = document.createElement("span");
food.classList.add("food");
food.style.left = foodPosition + "%";
food.style.bottom = "10vh";
if (croc) {
food.classList.add("croc");
croc = false;
} else {
food.classList.add("dino");
dinoJump = setTimeout(function () {
console.log("dinohopp");
jump(food, 90);
}, moveInterval * 40);
croc = true;
}
// behöver jag clearinterval?
gameContainer.appendChild(food);
//flyttar maten, bryt ut, hämta array
//är det här dumt för att varje mat har sitt eget intervall?
// försvinner det när elementet försvinner?
setInterval(() => {
if (!paused) {
let moveDistance = 1;
foodPosition = parseInt(food.style.left);
food.style.left = foodPosition - moveDistance + "%";
}
}, moveInterval);
}
}
function createFoodItem() {
if (!paused) {
// let moveDistance = 1;
let randomNumber = Math.random();
let foodPosition = 100;
//de skapas 100% till höger på skärmen
let food = document.createElement("span");
food.classList.add("food");
food.style.left = foodPosition + "%";
food.style.bottom = randomIntFromInterval(30, 80) + "%";
if (randomNumber > 0.5) {
food.textContent = "🥝";
} else {
food.textContent = "💩";
food.classList.add("zigzag");
}
gameContainer.appendChild(food);
//flyttar maten, bryt ut, hämta array
//är det här dumt för att varje mat har sitt eget intervall?
// försvinner det när elementet försvinner?
moveFood = setInterval(() => {
if (!paused) {
let moveDistance = 0.1;
foodPosition = parseInt(food.style.left);
food.style.left = foodPosition - moveDistance + "%";
}
}, moveInterval * 3);
}
}
function clearItem() {
let oldFood = document.querySelectorAll(".food");
for (let index = 0; index < oldFood.length; index++) {
const food = oldFood[index];
let position = window.getComputedStyle(food).getPropertyValue("left");
if (parseInt(position) <= 0) {
if (food.classList.contains("dino") || food.classList.contains("croc")) {
let foodWidth = parseInt(
window.getComputedStyle(food).getPropertyValue("width")
);
if (parseInt(position) <= 0 - foodWidth) {
// clearInterval(dinoJump);
gameContainer.removeChild(food);
}
} else {
gameContainer.removeChild(food);
}
}
}
}
function checkCollision() {
if (!paused) {
// let birdWidth = parseInt(window.getComputedStyle(kiwiBird).getPropertyValue("width"));
// let birdHeight = parseInt(window.getComputedStyle(kiwiBird).getPropertyValue("height"));
let oldFood = document.querySelectorAll("span.food");
if (oldFood.length > 0) {
//det blir fel om funktionen körs innan det finns någon mat
for (let index = 0; index < oldFood.length; index++) {
let foodPositionLeft = parseInt(
window.getComputedStyle(oldFood[index]).getPropertyValue("left")
);
let foodPositionBottom = parseInt(
window.getComputedStyle(oldFood[index]).getPropertyValue("bottom")
);
let birdPositionLeft = parseInt(
window.getComputedStyle(kiwiBird).getPropertyValue("left")
);
let birdPositionBottom = parseInt(
window.getComputedStyle(kiwiBird).getPropertyValue("bottom")
);
// console.log("mat: ", foodPositionLeft, foodPositionBottom, "fågel: ", birdPositionLeft, birdPositionBottom)
if (
foodPositionLeft > birdPositionLeft - 50 &&
foodPositionLeft < birdPositionLeft + 50 &&
foodPositionBottom > birdPositionBottom - 50 &&
foodPositionBottom < birdPositionBottom + 50
) {
handleCollision(oldFood[index]);
}
// && foodPositionBottom == birdPositionBottom
}
}
}
}
function handleCollision(collidingObject) {
if (collidingObject.classList.contains("zigzag")) {
if (!muteSound) {
poopSound.play();
}
kiwiBird.style.color = "sienna";
score -= 1;
gameContainer.removeChild(collidingObject);
scoreDisplay.textContent = score;
} else if (
collidingObject.classList.contains("dino") ||
collidingObject.classList.contains("croc")
) {
lives -= 1;
livesDisplay.textContent = lives;
kiwiBird.classList.add("dead");
if (lives === 0) {
if (!muteSound) {
gameOverSound.play();
}
stopGame();
showDialog(gameOver);
} else {
if (!muteSound) {
deadSound.play();
}
clearInterval(collisionDetection);
document.removeEventListener("keydown", moveBird);
setTimeout(function () {
kiwiBird.classList.remove("dead");
collisionDetection = setInterval(checkCollision, 50);
document.addEventListener("keydown", moveBird);
}, 2500);
}
} else {
kiwiBird.style.color = "teal";
if (!muteSound) {
kiwiSound.play();
}
score += 1;
gameContainer.removeChild(collidingObject);
scoreDisplay.textContent = score;
if (score === 5) {
moveInterval = 50;
}
else if (score === 10) {
moveInterval = 75;
clearInterval(createEnemies);
createEnemies = setInterval(createEnemy, 4000);
}
else if (score === 15) {
if (!muteSound) {
winSound.play();
}
showDialog(victory);
stopGame();
}
}
}
switches.addEventListener("change", (e) => {
const soundLabel = document.querySelector(".sound");
const muteLabel = document.querySelector(".mute");
const musicButton = document.getElementById("musicButton");
switch (e.target.id) {
case "pauseButton":
pauseButton.blur(); //för annars pausar man när man hoppar (med mellanslaget)
e.target.checked ? (paused = false) : (paused = true);
applyPausedStyles(paused);
case "musicButton":
musicButton.blur();
if (e.target.checked) {
muteSound = false;
soundLabel.style.backgroundColor = "rgba(107, 142, 35, 0.7)";
muteLabel.style.backgroundColor = "transparent";
} else {
muteSound = true;
muteLabel.style.backgroundColor = "peru";
soundLabel.style.backgroundColor = "transparent";
}
}
});
function applyPausedStyles(paused) {
const playText = document.querySelector(".play");
const pauseText = document.querySelector(".pause");
const pauseButton = document.getElementById("pauseButton");
if (paused) {
pauseText.style.backgroundColor = "peru";
playText.style.backgroundColor = "transparent";
document.removeEventListener("keydown", moveBird);
} else {
playText.style.backgroundColor = "rgba(107, 142, 35, 0.7)";
pauseText.style.backgroundColor = "transparent";
document.addEventListener("keydown", moveBird);
}
pauseButton.checked = !paused;
}
function showDialog(gameEvent) {
const dialog = document.createElement("dialog");
dialog.setAttribute("open", "");
dialog.setAttribute("role", "dialog");
dialog.setAttribute("tabindex", "0");
document.addEventListener("keydown", enableStartKey);
dialog.insertAdjacentHTML("beforeend", gameEvent);
document.body.appendChild(dialog);
let closeButton = document.createElement("button");
gameEvent === start
? (closeButton.innerText = "Start Game")
: (closeButton.innerText = "Play Again!");
closeButton.addEventListener("click", startGame);
closeButton.addEventListener("click", closeDialog);
dialog.appendChild(closeButton);
}
function closeDialog() {
let dialog = document.querySelector("dialog");
document.body.removeChild(dialog);
document.removeEventListener("keydown", enableStartKey);
}
function enableStartKey(e) {
if (e.key === "Enter") {
startGame();
closeDialog();
}
}