-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (110 loc) · 3.53 KB
/
script.js
File metadata and controls
136 lines (110 loc) · 3.53 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
const cards = document.querySelectorAll(".card");
let hasFlippedCard = false;
let firstCard, secondCard;
let lockBoard = false;
let matchesFound = 0;
const totalPairs = 8;
const confettiContainer = document.getElementById("confetti-container");
const playAgainBtn = document.getElementById("play-again");
//pop-over logic
const instructionBtn = document.getElementById("instruction-btn");
const popover = document.getElementById("popover");
const closeBtn = document.getElementById("close-btn");
instructionBtn.addEventListener("click", () => {
popover.classList.toggle("hidden");
});
closeBtn.addEventListener("click", () => {
popover.classList.add("hidden");
});
// Flip card logic
function flipCard() {
if (lockBoard || this === firstCard) return;
this.classList.add("flipped");
if (!hasFlippedCard) {
hasFlippedCard = true;
firstCard = this;
} else {
secondCard = this;
checkForMatch();
}
}
// Check if two cards match
function checkForMatch() {
let isMatch = firstCard.dataset.image === secondCard.dataset.image;
isMatch ? disableCards() : unflipCards();
}
function startConfetti() {
function createConfettiPiece() {
const confetti = document.createElement("div");
confetti.classList.add("confetti");
// Random size
const sizeClass = ["small", "medium", "large"][
Math.floor(Math.random() * 3)
];
confetti.classList.add(sizeClass);
// Random shape - add 'circle' only if needed
if (Math.random() > 0.5) {
confetti.classList.add("circle");
}
// Random color
const confettiColors = [
"#FFC107",
"#2196F3",
"#FF5722",
"#4CAF50",
"#E91E63",
];
confetti.style.backgroundColor =
confettiColors[Math.floor(Math.random() * confettiColors.length)];
// Random horizontal position
confetti.style.left = `${Math.random() * 100}vw`;
// Random animation delay
confetti.style.animationDelay = `${Math.random() * 2}s`;
confettiContainer.appendChild(confetti);
// Remove confetti after animation ends
confetti.addEventListener("animationend", () => confetti.remove());
}
// Generate multiple confetti pieces
for (let i = 0; i < 1000; i++) {
createConfettiPiece();
}
}
// Show confetti on win
function showConfetti() {
startConfetti(); // Call the confetti animation
playAgainBtn.style.display = "block"; // Show the play again button
document.getElementById("congratulations").style.display = "block"; // Show the congratulations message
}
// Disable matched cards
function disableCards() {
firstCard.classList.add("matched");
secondCard.classList.add("matched");
matchesFound++;
if (matchesFound === totalPairs) showConfetti();
resetBoard();
}
// Unflip cards if not matched
function unflipCards() {
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove("flipped");
secondCard.classList.remove("flipped");
resetBoard();
}, 1000);
}
// Reset the board state
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
// Shuffle the cards
(function shuffle() {
cards.forEach((card) => {
let randomPos = Math.floor(Math.random() * 16);
card.style.order = randomPos;
});
})();
// Add event listeners to cards
cards.forEach((card) => card.addEventListener("click", flipCard));
// Reload game on play again
playAgainBtn.addEventListener("click", () => window.location.reload());