Intermediate
+50 XP

👋 Start learning JavaScript right now — for free!

📖

Game Architecture and HTML Structure

Understand how the game works and write the HTML skeleton

🎓

This is your final project!

You've completed the JavaScript course and the HTML/CSS course. Now it's time to bring all your knowledge together and write a real game. Memory Game is a classic game with 16 cards (8 pairs), all face-down. You flip two — if they match, they stay open. If not — they flip back. Goal: reveal all pairs as fast as possible.

What we'll use

JavaScript:

  • Arrays — store cards
  • Functions — shuffle, flipCard, checkMatch
  • if/else — pair checking
  • Loops — render cards
  • DOM — document.createElement, classList
  • setInterval — timer

HTML:

  • Semantic structure (header, main)
  • divs for cards and grid
  • id and class attributes

CSS:

  • CSS Grid for the card grid
  • transition + transform for the flip animation
  • backface-visibility for the 3D effect

HTML структура гри

html
<!DOCTYPE html>
<html lang="uk">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Memory Game</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>

  <div class="game-wrapper">

    <!-- Шапка з рахунком та таймером -->
    <header class="game-header">
      <h1>🃏 Memory Game</h1>
      <div class="stats">
        <div class="stat">
          <span class="stat-label">Ходів:</span>
          <span class="stat-value" id="moves">0</span>
        </div>
        <div class="stat">
          <span class="stat-label">Час:</span>
          <span class="stat-value" id="timer">0s</span>
        </div>
      </div>
      <button id="restart-btn">🔄 Почати знову</button>
    </header>

    <!-- Ігрова сітка (4×4 = 16 карток) -->
    <main class="game-board" id="game-board">
      <!-- Картки генеруються через JavaScript -->
    </main>

    <!-- Повідомлення про перемогу (приховане) -->
    <div class="win-screen" id="win-screen" hidden>
      <h2>🎉 Молодець!</h2>
      <p id="win-message"></p>
      <button id="play-again-btn">Зіграти ще раз</button>
    </div>

  </div>

  <script src="game.js"></script>
</body>
</html>

CSS для гри

Скопіюй цей CSS в файл style.css — він вже готовий, весь фокус буде на JavaScript:

css
* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  font-family: 'Inter', system-ui, sans-serif;
}

.game-wrapper {
  background: white;
  border-radius: 20px;
  padding: 32px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
  max-width: 600px;
  width: 100%;
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.game-header h1 { font-size: 1.5rem; color: #1e293b; }

.stats { display: flex; gap: 16px; }
.stat { text-align: center; }
.stat-label { font-size: 0.75rem; color: #64748b; display: block; }
.stat-value { font-size: 1.5rem; font-weight: 700; color: #6070f7; }

#restart-btn {
  background: #f1f5f9;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 0.875rem;
  transition: background 0.2s;
}
#restart-btn:hover { background: #e2e8f0; }

/* Ігрова сітка */
.game-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}

/* Картка */
.card {
  aspect-ratio: 3/4;
  cursor: pointer;
  perspective: 600px;
}

.card-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.4s ease;
  border-radius: 10px;
}

.card.flipped .card-inner,
.card.matched .card-inner {
  transform: rotateY(180deg);
}

.card-front,
.card-back {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  font-size: 2rem;
}

.card-front {
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  font-size: 1.5rem;
  font-weight: 700;
}

.card-back {
  background: white;
  border: 2px solid #e2e8f0;
  transform: rotateY(180deg);
}

.card.matched .card-back { border-color: #22c55e; background: #f0fdf4; }
.card.matched { pointer-events: none; }

/* Екран перемоги */
.win-screen {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: white;
  font-size: 1.25rem;
}
.win-screen h2 { font-size: 3rem; }
#play-again-btn {
  background: #6070f7;
  color: white;
  border: none;
  padding: 12px 32px;
  border-radius: 12px;
  font-size: 1rem;
  cursor: pointer;
  margin-top: 8px;
}

You don't need to understand every CSS line right away! The key is you already know grid, flexbox, transition, backface-visibility from the course. Copy the CSS and focus on the JavaScript logic — that's what we'll write in the tasks.

Comments

Log In or Start to leave a comment.