Open In App

Build a Memory Card Game Using HTML CSS and JavaScript

Last Updated : 25 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A memory game is a type of game that can be used to test or check the memory of a human being. It is a very famous game. In this game, the player has some cards in front of him and all of them facing down initially. The player has to choose a pair of cards at one time and check whether the faces of both cards are matching or not. If the faces of both cards are the same, then the player can keep those cards face up and continue. Otherwise, the player needs to put the picked cards back face down and continue by selecting other cards. In this article, we are going to build a memory game with the help of HTML, CSS, and JavaScript.

Preview:

cardFlipPreview

Technologies Used:

  • HTML
  • CSS
  • JavaScript

Approach:

  • Create a basic structure for you games using the HTML tags like div, heading, paragraph, img etc. with the particular Classes and Ids associated with each one of them.
  • Now, select the elements with the help of class and id CSS selectors to style the elements and make the UI attractive and interactive.
  • After HTML and CSS, its time to get all the required elements inside the JavaScript code and apply logic on them.
  • In JavaScript, we will create an array of objects with the image link, respective alt value and ID for each item of the array.
  • In the next part, a callback function will be created to handle the click event of the cards. This function will handle the card flips whether the flipped card is same or different by removing and ading the classes to the elements.
  • Next, another callback function will be created to handle the Restart Game button click, it will shuffle all the cards and flip them backward again to restart the game.
  • At the end, we will check for the condition where game ends and show the result to the user by using the window.alert() method of JavaScript.

Example: The below example will explain how you can create an memory card game using HTML, CSS and JavaScript:

HTML
<!DOCTYPE html>
<html lang="en">

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

<body>
    <div class="container">
        <div id="dynamic-cards" class="game-container">
            <div class="heading-container">
                <h1 class="gfg-heading">
                      GeeksforGeeks
                  </h1>
                <h2 class="game-heading">Click any card to flip it!</h2>
            </div>
            <div class="cards-container">
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="1" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102835/html5Image.png"
                        alt="HTML Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="2" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102834/JSImage.jpg"
                        alt="JavaScript Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="3" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102834/CSS3Image.png"
                        alt="CSS Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="4" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102834/JSImage.jpg"
                        alt="JavaScript Image" class="card-image">
                </div>

                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="5" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102835/html5Image.png"
                        alt="HTML Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="6" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102833/vueImage.png"
                        alt="VUE Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="7" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102834/CSS3Image.png"
                        alt="CSS Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="8" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102833/reactImage.png"
                        alt="React Image" class="card-image">
                </div>

                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="9" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102833/vueImage.png"
                        alt="VUE Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="10"
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102833/AngularImage.png"
                        alt="Angular Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="11" 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102833/reactImage.png"
                        alt="React Image" class="card-image">
                </div>
                <div class="card">
                    <img 
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122105630/gfgImage.png"
                        class="outline-image">
                    <img id="12"
                        src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231122102833/AngularImage.png"
                        alt="Angular Image" class="card-image">
                </div>
            </div>

            <div class="progress-container">
                <div class="move-counter">Moves: 0</div>
                <button id="restart" class="restart-button">
                      Restart Game
                  </button>
            </div>
        </div>
    </div>

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

</html>
CSS JavaScript

Output: The below output will show you a demo of the above card game:


Next Article

Similar Reads