
Today were talking about how I created my Dino Pairs Game using CSS3 animations. This tutorial only really works in Chrome because I’m using 3d transforms. The game is playable in Firefox but better in Chrome.
So yeah I’ve had a bit of time off and had a little dabble with games. I’ve been having a look at the canvas tag, looking at out of the box game development like Construct2 (which by the way is awesome for rapid development of HTML5 games). I’ve also had a look at impactjs but haven’t managed to get to grips with that yet. But my heart lies with CSS, I just get it. I find it easy can experiment with confidence and enjoy coding it. So over the next few weeks I’m going to be really pushing the boat out with CSS3 and trying new stuff little bits of tips and tricks to full blown posts like this one.
So where was I, oh yeah Dino pairs. So the idea behind this game is that my eldest son loves to play this game the traditional way so whilst playing one evening I thought I have a go at creating one on line to up my profile and get loads of people to link to my game.(Something like that anyway).
Hit the Deck
So first things first we need the image assets for the deck. As you can see, I aggregated all the images together into one image so later I could position the image as a background with CSS.

So now that is done we can get on with the rest of the game.
HTML5 Page
The HTML is pretty straight forward but I have used HTML5 layout elements within the page.
|
||
|
|
As you can see we’ve got game stats section that holds all the information about the game. Then below that we have the game section that holds all the game elements like the gameIntro screen and and the gamecomplete screen and also a div called cards that is eventually going to hold the deck. Then within that we create one card with a div to hold the front and back of the card. We will clone this to create the rest of the cards. I’ve also got jquery in there because I’m lazy and can’t be bothered to write pure javascript and a link to my js file.
The Javascript
Ok so there’s a lot going on in the javascript so I’m only going to highlight a couple of things. I’ve commented all the code in the download to make it easier to understand but just can always ask me any questions in the comments below.
|
||
|
|
So as I said we are concentrating on the CSS3 animations here. So the selectCard function runs when the card div is clicked. So first if there is more than 1 element with the class name .card-flipped then the click does nothing because we already have two cards turned over. Otherwise we add the class .card-flipped to the element that has been clicked. And when we have a look at the CSS this is where we will see the transition has been created. Also at the end of this function we start the check to see if the cards match but I’m not going to get into that stuff here.
|
||
|
|
So there’s quite a bit going on in this function so firstly we’re checking if the card is matched with the isMatchedpattern function. If it is then we removed the card-flipped class and add the card-removed class which as you will see just reduces the opacity of the div to zero so you can no longer see it. Then we bind the event listener webkitTransitionEnd to say when the div has zero opacity run the removeTookCards function which basically deletes the div.
So now the CSS
So here is where the good stuff happens. Basically the javascript just adds and removes classes when needed and the CSS does the rest.
For each card, we define a perspective property to give it a visual depth effect. We also set the transition property otherwise it’s not a card flip: the card simply toggles between the front and back faces.
|
||
|
|
There are two faces on each card. The face will be rotated later and we will define the transition properties to animate the style changes. We also want to make sure the back face is hidden:
|
||
|
|
Then we set the front and back face styles. They are almost the same as the flipping card example, except that we are now giving them background images
and box shadows:
|
||
|
|
When any card is removed, we want to fade it out. Therefore, we declare a card-removed class with 0 opacity:
|
||
|
|
In order to show different card graphics from the sprite sheet of the card deck, we clip the background of the card into different background positions:
|
||
|
|
And that’s it. If you want to download the code I’ve attached it. Hope you enjoy it and play the game.












21 Responses
[...] http://www.my-html-codes.com/game-using-css3-animation RedditBufferShareEmailPrintFacebookDiggStumbleUpon [...]
download link “Error! This link is broken.”
Sorry. Don’t know whats happened there.
I’ll sort that out ASAP.
Sorted now. Sorry for the mistake.
Thanks for your sharing, I’m from MozTW Community (http://moztw.org), could you tell me how about the license?
I want to replace the pictures to make my card game and modify it for Firefox 10 to demo CSS3 transform supporting, what kind of attribution should I put on my modified version?
Sorry for my poor English.
BTW, I don’t know why you set #cards {height: 580px} instead of 530px in #gameIntro and #gameComplete, but that would make #linkBack not able to be clicked after the game started.
Your English is very good. You can use the code if you can include a link back to my site and a thank you in your page. And thanks for spotting the height error. I have fixed that in the demo and the download.
Thank you!
I’ll put a link back to this article when we finish it.
Great stuff. Could you also send a link to your article?
The other members in our community is preparing hand-painting original art for this demo project, so we still not finish it.
Currently, I’ve added Firefox 10 and fullscreen API support, when we finally done it, I’ll come back here and leave a comment to notify, thank for your patience
That’s fine. It’s sounds like its going to be great. Can’t wait to see it.
Hello~
We almost done it, now it has original card art and background music, but is still waiting for other detail tuning.
Could you declare a specific license like CC:BY on your work? So that we could also distribute our work base on your license when we publish it.
wow! the power of css3! its very nice pra xuxu!
Now we put the files on github:
https://github.com/moztw/browserpairs
and you can see it here:
http://moztw.github.com/game/browserpairs/
[...] Dino Pairs Game – Using CSS3 Animation 13. 14. 12. [...]
Would it be hard to modify this matching game to match a word to another word? For example, to teach English vocabulary through opposites, etc.?
HTML5 is so sweet. i can’t wait to learn more about it. great article btw, my best time was 29 seconds lol
Thats better than any of my times!!
Very nice… Good job!
[...] View Game View Article [...]
Matt, thank you for this very informative tutorial! I have been tasked with HTML5ing our non-profit website, and one area I am really excited to explore is in HTML5 Game Development. My first thought was of a Memory card game, and yours is already fully-functional. I am in the process of making it my own, and I will certainly linkback to you, but I have just a few questions before the game can go live. I have the cards created, but I would like to use 20 cards. I see the comment in your JS which manually creates the 3×6 array, but how can I change that to be 4×5? Also, I would like to keep score by number of turns instead of time taken. Apologies in advance, I am not much of a Javascripter. Thanks again for this wonderful look at the power of HTML5/CSS3!