-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
153 lines (144 loc) · 4.58 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
import { data, shuffle } from "./utils.js";
const gameBoard = document.getElementById("gameBoard");
const container = document.querySelector(".container");
const matchPoint = document.getElementById("matchPoint");
const parent = document.querySelector(".life-box");
const restartBtn = document.querySelector(".restartBtn");
console.log("hello");
let countDown = 3;
let timer;
let selectedCards = [];
const matchCount = 2;
const lifeCount = 3;
const matchPoints = 0;
function handleCardFlip(e, i) {
if (
selectedCards.length < matchCount &&
selectedCards[0] !== i &&
selectedCards.length !== 2
) {
e.target.children[0].src = data[i].imgSrc;
selectedCards.push(i);
}
if (selectedCards.length === 2) {
if (data[selectedCards[0]].name === data[selectedCards[1]].name) {
const event1 = gameBoard.children[selectedCards[0]];
const event2 = gameBoard.children[selectedCards[1]];
event1.classList.remove("confettis-on");
event2.classList.remove("confettis-on");
// triggering reflow, so that the CSS animation restarts
void event1.offsetWidth;
void event2.offsetWidth;
event1.classList.add("confettis-on");
event2.classList.add("confettis-on");
matchPoint.innerText = Number(matchPoint.innerText) + 1;
selectedCards.length = 0;
} else {
setTimeout(() => {
if (parent.children.length !== 0) {
parent.removeChild(parent.lastChild);
if (parent.children.length === 0) {
createOverlay("Game Over !", true);
return;
}
}
gameBoard.children[selectedCards[0]].children[0].src =
"./images/default.png";
gameBoard.children[selectedCards[1]].children[0].src =
"./images/default.png";
selectedCards.length = 0;
}, 500);
// console.log( data[selectedCards[0]].name,gameBoard.children[selectedCards[0]].children[0])
// console.log( gameBoard.children[selectedCards[1]].children[0])
// gameBoard.children[selectedCards[0]].src = ;
}
}
}
function resetLifes() {
const lifeBox = document.querySelector(".life-box");
const fragment = document.createDocumentFragment();
for (let i = 0; i < lifeCount; i++) {
const img = document.createElement("img");
img.setAttribute("src", "./images/heart.svg");
img.classList.add("life");
fragment.appendChild(img);
}
lifeBox.appendChild(fragment);
}
function createBoard() {
restartGame();
shuffle(data);
resetLifes();
if(document.querySelector(".overlay")){
const overlay = document.querySelector(".overlay");
overlay.remove();
}
if(gameBoard.children.length > 0){
gameBoard.innerHTML = "";
}
matchPoint.innerText = matchPoints;
const fragment = document.createDocumentFragment();
for (let i = 0; i < data.length; i++) {
const card = document.createElement("div");
// card.style.width = "200px"
// card.style.heoght = "200px"
const img = document.createElement("img");
img.setAttribute("src", data[i].imgSrc);
setTimeout(() => {
img.setAttribute("src", "./images/default.png");
}, 3500);
img.style.pointerEvents = "none";
card.classList.add("card");
card.addEventListener("click", (e) => handleCardFlip(e, i), false);
img.classList.add("card-img");
card.appendChild(img);
fragment.append(card);
}
gameBoard.append(fragment);
}
createBoard();
const start = Date.now();
function updateTimer() {
if (countDown >= 0) {
console.log(countDown, "down");
if (countDown === 0) {
console.log("ffffff");
createOverlay("GO!");
setTimeout(() => {
const overlay = document.querySelector(".overlay");
overlay.remove();
}, 500);
} else {
createOverlay(countDown);
}
countDown = countDown - 1;
} else {
clearInterval(timer);
}
}
function restartGame() {
timer = setInterval(updateTimer, 1000);
updateTimer();
}
function createOverlay(overlayText, showButton = false) {
if (document.querySelector(".overlay")) {
document.querySelector(".overlay").children[0].textContent = overlayText;
} else {
const overlayElement = document.createElement("div");
overlayElement.classList.add("overlay");
const inner = `
<p>${overlayText}</p>
`;
overlayElement.innerHTML = inner;
if (showButton) {
const btn = document.createElement("button");
btn.innerText = "Restart";
btn.classList.add("restartBtn");
if (btn) {
btn.addEventListener("click", createBoard, false);
}
overlayElement.appendChild(btn);
}
container.appendChild(overlayElement);
}
}