• Develop a memory card game using HTML5, CSS3 and JavaScript

    Published on April 10, 2012

    Creating the game engine using jQuery

    First, we declare a function which will create the initial conditions for the game flow:

    Firstly we declare the deck of paired cards in the array matchingGame.deck, for this example being 12 pairs. After this, the elements of the array are randomly arranged. The next step is to clone the first child of the element with the class “card” and append the new created elements to the element with the ID “card”. Doing this we’ll have the containers for all 24 cards, containers that will be positioned inside the box with the ID “cards”, based on their width and height and their position inside the matchingGame.deck array. At the same time a class is added to the elements with class “back”, class that holds the name of each paired elements. If a card is clicked, the selectCard() function is called:

    This function checks if one or two cards are flipped and based on this criteria performs some actions. Another check is made to avoid more than one click of a card, and the number of clicks made to be accurate.
    If two cards are flipped the checkPattern() functions is called:

    The checkPattern() function performs some actions if two cards flipped are identical. If two cards are identical they are removed (hide) from the user’s view, otherwise they are flipped back. In the case that all cards are removed a message is displayed to replay the game and the HTML elements manipulated during the game flow are reset to a new state (children of the element with the class “card” are removed). This new state should be checked next time the game starts and the elements with the classes “face front” and “face back” should be recreated. To do this the following lines of code are added to the startGame() function:

    Each time the checkPattern() function is called the matched pairs value should be updated accordingly:

    Having this set, the game can be played from now.

    A new game version released on April 17th 2012

    This new version uses 12 pairs of cards taken (somehow randomly) from a deck of 52 cards. With this approach the game flow is not as flat as in previous version.

    The modified code:

    19 comments to "Develop a memory card game using HTML5, CSS3 and JavaScript"

    Share your thoughts on this article.

    Your email address will not be published. Required fields are marked *

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">