-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
136 lines (112 loc) · 3.49 KB
/
Copy pathgame.js
File metadata and controls
136 lines (112 loc) · 3.49 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
//KEY PRESSES
document.addEventListener("keydown", function (event) {
keyPressed = event.key;
keyPressed = event.key;
if (event.key == "ArrowLeft") {
1;
history.back();
}
});
//----------------------------------------------------
//TIMER
// the player has 60 seconds to complete the game, but i wanted another decimal place
// for more precision
// https://www.w3schools.com/jsref/jsref_tofixed.asp
var i = 60;
function Timer() {
if (i > 0) {
document.getElementById("timer").innerHTML = i.toFixed(1);
i -= 0.1;
} else {
document.getElementById("time").style.display = "none";
document.getElementById("timer").innerHTML = "YOU HAVE LOST!";
clearInterval(timerInterval); //stops from further decrementing
playAudio();
//the location is redirected after 3,4 seconds
setTimeout(function () {
window.location.href = "index.html";
}, 3350);
}
}
var timerInterval = setInterval(Timer, 100); //it is executing the timer after 100 miliseconds
//--------------------------------------------------------
//AUDIO
//when u lose
function playAudio() {
document.getElementById("loser").play();
}
function pauseAudio() {
document.getElementById("loser").pause();
}
//when u win
function playAudio2() {
document.getElementById("winner").play();
}
function pauseAudio2() {
document.getElementById("winner").pause();
}
//----------------------------------------------------------
//THE GAME
let hasBeenFlipped = false;
let firstCard, secondCard;
let lockPlayground = false;
const cards = document.querySelectorAll(".item");
function flipCard() {
if (lockPlayground) return;
if (this == firstCard) return; //if u double click on one card and its the same one u return from the function
this.classList.add("flip");
if (!hasBeenFlipped) {
//first click
hasBeenFlipped = true;
firstCard = this; //reference to the first card being flipped
} else {
//second flip
hasBeenFlipped = false;
secondCard = this;
lockPlayground = true;
//are they matching?
matchingCheck();
}
}
//to check matching
var matchedPairs = 0;
var winner = 8;
function matchingCheck() {
if (firstCard.dataset.icons == secondCard.dataset.icons) {
//match
firstCard.removeEventListener("click", flipCard);
secondCard.removeEventListener("click", flipCard); //prevent further clicking on the cards
lockPlayground = false; //it isnt locked anymore
matchedPairs += 1;
if (matchedPairs == winner) {
document.getElementById("time").style.display = "none";
document.getElementById("timer").innerHTML =
"WINNER WINNER CHICKEN DINNER"; //THIS PHRASE IS FROM A VIDEO GAME I PLAYED (PUBG)
clearInterval(timerInterval);
playAudio2();
setTimeout(function () {
window.location.href = "index.html";
}, 3350);
}
} else {
//do not match
setTimeout(() => {
firstCard.classList.remove("flip");
secondCard.classList.remove("flip"); //flips cards back
resetPlayground();
}, 1500); //cards will be flipped after 1,5 sec
}
function resetPlayground() {
[hasBeenFlipped, lockPlayground] = [false, false];
[firstCard, secondCard] = [null, null]; //null indicating that no cards are currently selected
}
}
//to shuffle
//it is wrapped into () so it is immediately invoked
(function shuffle() {
cards.forEach((card) => {
let random = Math.floor(Math.random() * 16);
card.style.order = random;
});
})();
cards.forEach((card) => card.addEventListener("click", flipCard));