-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_game.html
More file actions
346 lines (295 loc) · 10.7 KB
/
memory_game.html
File metadata and controls
346 lines (295 loc) · 10.7 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Card Game</title>
<style>
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
color: white;
}
.game-container {
text-align: center;
padding: 20px;
position: relative;
}
h1 {
margin-bottom: 10px;
font-size: 2.5em;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.stats {
display: flex;
justify-content: center;
gap: 30px;
margin-bottom: 20px;
font-size: 1.2em;
}
.stat {
background: rgba(255,255,255,0.2);
padding: 10px 20px;
border-radius: 10px;
backdrop-filter: blur(10px);
}
.game-board {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 15px;
max-width: 500px;
margin: 0 auto;
}
.card {
width: 100px;
height: 100px;
position: relative;
cursor: pointer;
transform-style: preserve-3d;
transition: transform 0.6s;
}
.card.flipped {
transform: rotateY(180deg);
}
.card.matched {
animation: matchPulse 0.6s ease;
}
/* Different colors for each matched pair */
.card.matched[data-card-value="🎮"] .card-back {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.card.matched[data-card-value="🎯"] .card-back {
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
}
.card.matched[data-card-value="🎨"] .card-back {
background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
}
.card.matched[data-card-value="🎭"] .card-back {
background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
}
.card.matched[data-card-value="🎪"] .card-back {
background: linear-gradient(135deg, #30cfd0 0%, #330867 100%);
}
.card.matched[data-card-value="🎸"] .card-back {
background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
}
.card.matched[data-card-value="🎲"] .card-back {
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
}
.card.matched[data-card-value="🎰"] .card-back {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
}
@keyframes matchPulse {
0% { transform: scale(1) rotateY(180deg); }
50% { transform: scale(1.1) rotateY(180deg); }
100% { transform: scale(1) rotateY(180deg); }
}
.card-face {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 2.5em;
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
}
.card-front {
background: linear-gradient(135deg, #e0e0e0 0%, #f5f5f5 100%);
color: #333;
font-weight: bold;
}
.card-back {
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
transform: rotateY(180deg);
}
.win-screen {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.95);
border-radius: 20px;
z-index: 1000;
display: none;
flex-direction: column;
justify-content: center;
align-items: center;
}
.win-screen.show {
display: flex;
}
.win-screen h2 {
color: #4ecdc4;
font-size: 3em;
margin-bottom: 20px;
}
.play-again {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 15px 30px;
font-size: 1.2em;
border-radius: 10px;
cursor: pointer;
transition: transform 0.2s;
}
.play-again:hover {
transform: scale(1.05);
}
</style>
</head>
<body>
<div class="game-container">
<h1>Memory Card Game</h1>
<div class="stats">
<div class="stat">Moves: <span id="moves">0</span></div>
<div class="stat">Time: <span id="time">0:00</span></div>
<div class="stat">Matches: <span id="matches">0</span>/8</div>
</div>
<div class="game-board" id="gameBoard"></div>
<div class="win-screen" id="winScreen">
<h2>🎉 You Win! 🎉</h2>
<p>Completed in <span id="finalMoves">0</span> moves</p>
<p>Time: <span id="finalTime">0:00</span></p>
<button class="play-again" onclick="resetGame()">Play Again</button>
</div>
</div>
<script>
// Game variables
let cards = [];
let flippedCards = [];
let matchedPairs = 0;
let moves = 0;
let gameStarted = false;
let startTime = 0;
let timerInterval = null;
// Card emojis - using pairs of emojis
const cardEmojis = ['🎮', '🎯', '🎨', '🎭', '🎪', '🎸', '🎲', '🎰'];
// Initialize the game
function initGame() {
// Create pairs of cards
const gameDeck = [...cardEmojis, ...cardEmojis];
// Shuffle the deck
for (let i = gameDeck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[gameDeck[i], gameDeck[j]] = [gameDeck[j], gameDeck[i]];
}
// Create card elements
const gameBoard = document.getElementById('gameBoard');
gameBoard.innerHTML = '';
gameDeck.forEach((emoji, index) => {
const card = document.createElement('div');
card.className = 'card';
card.dataset.cardValue = emoji;
card.dataset.cardIndex = index;
card.innerHTML = `
<div class="card-face card-front">?</div>
<div class="card-face card-back">${emoji}</div>
`;
card.addEventListener('click', flipCard);
gameBoard.appendChild(card);
cards.push(card);
});
}
// Flip a card
function flipCard(e) {
const card = e.currentTarget;
// Start timer on first flip
if (!gameStarted) {
gameStarted = true;
startTime = Date.now();
timerInterval = setInterval(updateTimer, 1000);
}
// Don't flip if already flipped or if two cards are already flipped
if (card.classList.contains('flipped') ||
card.classList.contains('matched') ||
flippedCards.length === 2) {
return;
}
// Flip the card
card.classList.add('flipped');
flippedCards.push(card);
// Check for match when two cards are flipped
if (flippedCards.length === 2) {
moves++;
document.getElementById('moves').textContent = moves;
checkMatch();
}
}
// Check if two flipped cards match
function checkMatch() {
const [card1, card2] = flippedCards;
const match = card1.dataset.cardValue === card2.dataset.cardValue;
if (match) {
// Cards match!
card1.classList.add('matched');
card2.classList.add('matched');
matchedPairs++;
document.getElementById('matches').textContent = matchedPairs;
// Check for win
if (matchedPairs === cardEmojis.length) {
endGame();
}
flippedCards = [];
} else {
// No match - flip back after delay
setTimeout(() => {
card1.classList.remove('flipped');
card2.classList.remove('flipped');
flippedCards = [];
}, 1000);
}
}
// Update timer display
function updateTimer() {
const elapsed = Math.floor((Date.now() - startTime) / 1000);
const minutes = Math.floor(elapsed / 60);
const seconds = elapsed % 60;
document.getElementById('time').textContent =
`${minutes}:${seconds.toString().padStart(2, '0')}`;
}
// End the game
function endGame() {
clearInterval(timerInterval);
// Show win screen
document.getElementById('finalMoves').textContent = moves;
document.getElementById('finalTime').textContent =
document.getElementById('time').textContent;
document.getElementById('winScreen').classList.add('show');
}
// Reset the game
function resetGame() {
// Reset variables
cards = [];
flippedCards = [];
matchedPairs = 0;
moves = 0;
gameStarted = false;
startTime = 0;
// Reset displays
document.getElementById('moves').textContent = '0';
document.getElementById('time').textContent = '0:00';
document.getElementById('matches').textContent = '0';
document.getElementById('winScreen').classList.remove('show');
// Clear timer
if (timerInterval) {
clearInterval(timerInterval);
}
// Reinitialize the game
initGame();
}
// Start the game when page loads
window.onload = initGame;
</script>
</body>
</html>