Book Image

HTML5 Game Development by Example: Beginner's Guide

By : Seng Hin Mak
Book Image

HTML5 Game Development by Example: Beginner's Guide

By: Seng Hin Mak

Overview of this book

Table of Contents (18 chapters)
HTML5 Game Development by Example Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
9
Building a Physics Car Game with Box2D and Canvas
Index

Time for action – adding game logic to the matching game


We have prepared the game environment in the last example and decided the game logic should be the same as playing a real deck. It is time to code the JavaScript logic now:

  1. Let's begin from our last matching game example. We have styled the CSS, and now, it is time to add the game logic in the js/matchgame.js file.

  2. The game is to match pairs of playing cards. We have 12 cards now, so we need six pairs of playing cards. Put the following code in the js/matchgame.js file. The array declares six pairs of card patterns:

    var matchingGame = {};
    matchingGame.deck = [
      'cardAK', 'cardAK',
      'cardAQ', 'cardAQ',
      'cardAJ', 'cardAJ',
      'cardBK', 'cardBK',
      'cardBQ', 'cardBQ',
      'cardBJ', 'cardBJ',
    ];
  3. We aligned the cards in jQuery's ready function in the previous chapter. Now we need to prepare and initialize more code in the ready function. To do this, change the ready function into the following code. The changed code is highlighted here:

    $(function...