-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (162 loc) · 5.44 KB
/
Copy pathscript.js
File metadata and controls
192 lines (162 loc) · 5.44 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
const cards = document.querySelectorAll(".card");
const homeScreen = document.querySelector(".home-screen");
const gameScreen = document.querySelector(".game-screen");
const timeoutScreen = document.querySelector(".timeout-screen");
const winScreen = document.querySelector(".win-screen");
const startGameBtn = document.getElementById("start-game-btn");
const restartBtn = document.querySelectorAll("#restart-btn, #restart-btn-win");
const homeBtn = document.querySelectorAll("#home-btn, #home-btn-win");
const timerDisplay = document.getElementById("timer");
let matched = 0;
let cardOne, cardTwo;
let disableDeck = true;
let countdown;
let timeLeft = 60;
startGameBtn.addEventListener("click", startGame);
restartBtn.forEach(btn => btn.addEventListener("click", restartGame));
homeBtn.forEach(btn => btn.addEventListener("click", goHome));
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}
function startGame() {
homeScreen.classList.add("hidden");
homeScreen.classList.remove("flex");
gameScreen.classList.remove("hidden");
gameScreen.classList.add("flex");
matched = 0;
timeLeft = 60;
timerDisplay.textContent = formatTime(timeLeft);
disableDeck = true;
shuffleCard();
setTimeout(flipAllCards, 0);
setTimeout(startCountdown, 5000);
}
function restartGame() {
timeoutScreen.classList.add("hidden");
timeoutScreen.classList.remove("flex");
winScreen.classList.add("hidden");
winScreen.classList.remove("flex");
startGame();
}
function goHome() {
timeoutScreen.classList.add("hidden");
timeoutScreen.classList.remove("flex");
winScreen.classList.add("hidden");
winScreen.classList.remove("flex");
gameScreen.classList.add("hidden");
gameScreen.classList.remove("flex");
homeScreen.classList.remove("hidden");
homeScreen.classList.add("flex");
clearInterval(countdown);
}
function flipAllCards() {
cards.forEach(card => card.classList.add("flip"));
setTimeout(() => {
cards.forEach(card => card.classList.remove("flip"));
disableDeck = false;
}, 5000);
}
function startCountdown() {
countdown = setInterval(() => {
timeLeft--;
timerDisplay.textContent = formatTime(timeLeft);
if (timeLeft <= 0) {
clearInterval(countdown);
endGame(false);
}
}, 1000);
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
return `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
}
function endGame(isWin) {
disableDeck = true;
gameScreen.classList.add("hidden");
gameScreen.classList.remove("flex");
if (isWin) {
winScreen.classList.remove("hidden");
winScreen.classList.add("flex");
explodeConfetti();
} else {
timeoutScreen.classList.remove("hidden");
timeoutScreen.classList.add("flex");
}
}
function flipCard({target: clickedCard}) {
if(cardOne !== clickedCard && !disableDeck) {
clickedCard.classList.add("flip");
if(!cardOne) {
return cardOne = clickedCard;
}
cardTwo = clickedCard;
disableDeck = true;
let cardOneImg = cardOne.querySelector(".back-view img").src,
cardTwoImg = cardTwo.querySelector(".back-view img").src;
matchCards(cardOneImg, cardTwoImg);
}
}
function matchCards(img1, img2) {
if(img1 === img2) {
matched++;
if(matched === 4) {
clearInterval(countdown);
setTimeout(() => {
endGame(true);
}, 500);
}
cardOne.removeEventListener("click", flipCard);
cardTwo.removeEventListener("click", flipCard);
cardOne = cardTwo = "";
return disableDeck = false;
}
setTimeout(() => {
cardOne.classList.add("shake");
cardTwo.classList.add("shake");
}, 400);
setTimeout(() => {
cardOne.classList.remove("shake", "flip");
cardTwo.classList.remove("shake", "flip");
cardOne = cardTwo = "";
disableDeck = false;
}, 1200);
}
function shuffleCard() {
matched = 0;
cardOne = cardTwo = "";
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
arr.sort(() => Math.random() > 0.5 ? 1 : -1);
cards.forEach((card, i) => {
card.classList.remove("flip");
let imgTag = card.querySelector(".back-view img");
imgTag.src = `images/img-${arr[i]}.png`;
card.addEventListener("click", flipCard);
});
}
function explodeConfetti() {
const duration = 15 * 1000,
animationEnd = Date.now() + duration,
defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 0 };
const interval = setInterval(function() {
const timeLeft = animationEnd - Date.now();
if (timeLeft <= 0) {
return clearInterval(interval);
}
const particleCount = 50 * (timeLeft / duration);
// since particles fall down, start a bit higher than random
confetti(
Object.assign({}, defaults, {
particleCount,
origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 },
})
);
confetti(
Object.assign({}, defaults, {
particleCount,
origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 },
})
);
}, 250);
}
shuffleCard();