-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
170 lines (150 loc) · 4.11 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Matching Game</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
background-color: #f8f9fa;
}
header {
padding: 20px;
text-align: center;
background: #343a40;
color: #ffffff;
width: 100%;
}
button {
margin-top: 10px;
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.game-container {
display: grid;
grid-template-columns: repeat(4, 100px);
grid-gap: 10px;
margin: 20px auto;
}
.card {
width: 100px;
height: 100px;
background-color: #6c757d;
border-radius: 10px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
cursor: pointer;
position: relative;
}
.card img {
width: 100%;
height: 100%;
object-fit: cover;
display: none;
}
.card.flipped img {
display: block;
}
.card.matched {
cursor: default;
background-color: #28a745; /* Green background for matched cards */
}
</style>
</head>
<body>
<header>
<h1>Memory Matching Game</h1>
<button onclick="restartGame()">Restart Game</button>
</header>
<main>
<div class="game-container" id="gameContainer">
<!-- Cards will be dynamically added here -->
</div>
</main>
<footer>
<p>Moves: <span id="moves">0</span></p>
<p>Time: <span id="time">0:00</span></p>
</footer>
<script>
const gameContainer = document.getElementById('gameContainer');
const movesElement = document.getElementById('moves');
const imageSources = [
'images/picture1.jpg', 'images/picture2.jpg',
'images/picture3.jpg', 'images/picture4.jpg',
'images/picture5.jpg', 'images/picture6.jpg',
'images/picture7.jpg', 'images/picture8.jpg',
];
let firstCard = null;
let secondCard = null;
let lockBoard = false;
let moves = 0;
function createCard(imageSrc) {
const card = document.createElement('div');
card.classList.add('card');
card.innerHTML = `<img src="${imageSrc}" alt="Memory Card">`;
card.addEventListener('click', () => handleCardClick(card));
return card;
}
function initializeGame() {
const cards = [...imageSources, ...imageSources];
cards.sort(() => Math.random() - 0.5);
cards.forEach(imageSrc => {
const card = createCard(imageSrc);
gameContainer.appendChild(card);
});
}
function handleCardClick(card) {
if (lockBoard || card === firstCard || card.classList.contains('matched')) return;
card.classList.add('flipped');
if (!firstCard) {
firstCard = card;
} else {
secondCard = card;
checkForMatch();
}
}
function checkForMatch() {
lockBoard = true;
moves++;
movesElement.textContent = moves;
const isMatch =
firstCard.querySelector('img').src === secondCard.querySelector('img').src;
if (isMatch) {
firstCard.classList.add('matched');
secondCard.classList.add('matched');
resetBoard();
} else {
setTimeout(() => {
firstCard.classList.remove('flipped');
secondCard.classList.remove('flipped');
resetBoard();
}, 1000);
}
}
function resetBoard() {
[firstCard, secondCard, lockBoard] = [null, null, false];
}
function restartGame() {
gameContainer.innerHTML = '';
moves = 0;
movesElement.textContent = moves;
firstCard = null;
secondCard = null;
lockBoard = false;
initializeGame();
}
initializeGame();
</script>
</body>
</html>