-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.html
More file actions
269 lines (188 loc) Β· 5.45 KB
/
game.html
File metadata and controls
269 lines (188 loc) Β· 5.45 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#super-title {
font-weight: 100;
font-family: Georgia, 'Times New Roman', Times, serif;
padding: 27px;
text-align: center;
/* This is the syntax for creating borders in css, it must the pixels then shown along with the density and then the colour */
}
@keyframes frenzy {
0% {
background-color: rgb(133, 89, 198);
}
25% {
background-color: rgb(36, 182, 198);
}
50% {
background-color: rgb(133, 89, 198);
}
75% {
background-color: rgb(36, 182, 198);
}
100% {
background-color: rgb(133, 89, 198);
}
}
body {
font-family: arial, sans-serif;
animation: frenzy 7s infinite;
}
.game-container {
margin: 10px auto;
max-width: 70px;
}
h1 {
color: #040404;
}
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1px;
margin-top: 10px;
justify-content: center;
}
.card {
width: 100px;
height: 100px;
background-color: #2f1313;
display: flex;
justify-content: center;
align-items: center;
color: #c5adad;
font-size: 2em;
cursor: pointer;
border-radius: 20px;
transition: transform 0.3s;
margin: 50px auto;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
.card.flip {
background-color: #f7cfcf;
color: #444;
transform: rotateY(180deg);
cursor: default;
}
.card.matched {
background-color: #8bc34a;
color: #fff;
pointer-events: none;
}
.center-container {
display: flex;
justify-content: center; /* If you wanted it to work you needed a parent div, imagine it like this the button element only concerns itself with information inside the button but has authority to control itself across whole section of its div so you a parent div to encapsulate the rest of the spaced secction */
align-items: center;
height: 30vh;
}
.restart-btn {
display: block;
margin-top: 20px auto;
padding: 20px 40px;
font-size: 20px;
color: #fff;
background-color: #007bff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.8s;
text-align: center;
}
.restart-btn:hover {
background-color: #054386;
}
</style>
</head>
<!--When looking at style sheet we can see that we change text orientation
using the text-align option -->
</head>
<body>
<div id = "super-title"> <!--The id wont work unless you have a dash inbetween each space, you should know that from Python-->
<h1><u>Welcome to my memeory card game</u></h1>
</div>
<div class="grid">
<!-- Cards will be dynamically generated here -->
</div>
<div class="center-container">
<button class="restart-btn">Restart</button>
</div>
<p> </p>
<h2></h2>
<script>
const grid = document.querySelector('.grid');
const restartBtn = document.querySelector('.restart-btn');
// Card data (pairs of items)
const cards = ['π', 'π', 'π', 'π', 'π', 'π', 'π', 'π₯'];
// Duplicate and shuffle cards
let shuffledCards = [...cards, ...cards].sort(() => Math.random() - 0.5);
// State tracking
let firstCard = null;
let secondCard = null;
let lockBoard = false;
// Generate the cards
function createBoard() {
grid.innerHTML = ''; // Clear the grid
shuffledCards.forEach((symbol, index) => {
const card = document.createElement('div');
card.classList.add('card');
card.dataset.symbol = symbol;
card.dataset.index = index; // Unique index for each card
card.innerHTML = '<div class="content"></div>'; // Placeholder
card.addEventListener('click', flipCard);
grid.appendChild(card);
});
}
// Flip a card
function flipCard() {
if (lockBoard) return; // Prevent clicking during animation
if (this === firstCard) return; // Ignore double-clicking same card
this.classList.add('flip');
this.textContent = this.dataset.symbol;
if (!firstCard) {
// First card selected
firstCard = this;
} else {
// Second card selected
secondCard = this;
checkMatch();
}
}
// Check if two selected cards match
function checkMatch() {
lockBoard = true;
const isMatch = firstCard.dataset.symbol === secondCard.dataset.symbol;
if (isMatch) {
// Cards match
firstCard.classList.add('matched');
secondCard.classList.add('matched');
resetBoard();
} else {
// No match, flip cards back
setTimeout(() => {
firstCard.classList.remove('flip');
secondCard.classList.remove('flip');
firstCard.textContent = '';
secondCard.textContent = '';
resetBoard();
}, 1000);
}
}
// Reset board state
function resetBoard() {
[firstCard, secondCard] = [null, null];
lockBoard = false;
}
// Restart game
restartBtn.addEventListener('click', () => {
shuffledCards = [...cards, ...cards].sort(() => Math.random() - 0.5); // the sort method to our left is an iteration of our randomised array algorithm.
createBoard();
});
// Initialize game
createBoard();
</script>
</body>
</html>