-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
185 lines (151 loc) · 4.47 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
const cards=document.querySelectorAll('.memory-card ');
const backcards= document.querySelectorAll('.backface');
const restertbtn=document.querySelector('.restert');
const moved=document.querySelector('.Moves');
const countdown=document.querySelector('#countdown');
const model= document.querySelector('.modal');
const overly=document.querySelector('.overlay');
const secondModel=document.querySelector('.second-model');
const MovesModel=document.querySelector('.move-model');
let audio=document.getElementById('audio');
const gameOverModel=document.querySelector('.GameOver');
let lockBoard=false;
let hasFlipedCard=false;
let firstCard ,secondCard;
let firstbackface,secondbackface;
let randomPos;
let movesCount=0;
let startingMinuts=1;
let time=startingMinuts * 60;
let interval;
let correct=0;
const startAudio= new Audio ('audio/audiostart.mp3');
const matchAudio= new Audio ('audio/match.mp3');
const loseAudio= new Audio ('audio/lose.mp3');
const winnAudio= new Audio ('audio/winn.mp3');
const musicAudio= new Audio ('audio/music.mp3');
/********Functions********/
function startGame(){
cards.forEach(cardd =>{
cardd.classList.add('flip');
});
cards.forEach(cardd =>{
setTimeout(()=>{
cardd.classList.remove('flip');
},1000);
});
}
const closeModel=function(){
model.classList.add('visibilty');
overly.classList.add('visibilty');
gameOverModel.classList.add('visibilty');
location.reload();
}
const openModel=function(){
model.classList.remove('visibilty');
overly.classList.remove('visibilty');
MovesModel.textContent=movesCount;
secondModel.textContent=countdown.textContent;
}
function startTimer(){
const minutes= Math.floor(time / 60);
let seconds = time % 60;
seconds= seconds<10 ?'0'+seconds : seconds;
let timeover=`${minutes}:${seconds}`;
countdown.innerHTML=timeover
time--;
if(timeover==='0:00'){
musicAudio.pause();
loseAudio.play();
gameOverModel.classList.remove('visibilty');
overly.classList.remove('visibilty');
clearInterval(interval);
correct=0;
}
}
function resetBoard(){
[hasFlipedCard,lockBoard]=[false,false];
[firstCard,secondCard]=[null,null];
[firstbackface,secondbackface]=[null,null];
}
function checkforMatch(){
//check cards
if(firstbackface.src===secondbackface.src){
setTimeout(()=>{
matchAudio.play();
lockBoard=false;
firstCard.classList.add('visibilty');
secondCard.classList.add('visibilty');
},1000);
lockBoard=true;
correct++;
}else{
lockBoard=true;
setTimeout(()=>{
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
resetBoard();
},1500);
}
}
/*******Shuffle Function*******/
(function shuffle(){
startAudio.play();
setTimeout(()=>{
startAudio.pause();
musicAudio.play();
},4500);
startGame();
cards.forEach(card =>{
randomPos=Math.trunc(Math.random()*12) ;
card.style.order=randomPos; // the order of item flex(container should be flex) });
});
interval=setInterval(startTimer,900);
})(); // (function)(); imediatly invoked expression
/********Moved Count********/
function countMoved(){
movesCount++;
moved.textContent=movesCount;
}
overly.addEventListener('click',closeModel);
/***********Restert Button**********/
restertbtn.addEventListener('click',function(){
location.reload();
});
document.addEventListener('keydown',function(e){
if((e.key==='Escape')&&(!model.classList.contains('hidden'))){
closeModel();
}
});
/**********Click Carts Event**********/
for(let i=0 ;i<=cards.length;i++){
cards[i].addEventListener('click',function(){
if(lockBoard) return;
this.classList.add('flip');
// double click in the same card
if(this===firstCard) return;
if(!hasFlipedCard){
//first click
hasFlipedCard=true;
firstCard=this;
firstbackface=backcards[i];
}
else{
//second click
hasFlipedCard=false;
secondCard=this;
secondbackface=backcards[i];
checkforMatch();
}
countMoved();
if(correct==6){
setTimeout(()=>{
clearInterval(interval);
musicAudio.pause();
winnAudio.play();
openModel();
correct=0;
},900);
}
});
}