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:
- 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. - 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', ];
- We aligned ...
Get HTML5 Game Development by Example : Beginner's Guide - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.