Thomas Fuchs
Hi, I'm Thomas Fuchs. I'm the author of Zepto.js, of script.aculo.us, and I'm a Ruby on Rails core alumnus. With Amy Hoy I'm building cheerful software, like Noko Time Tracking and Every Time Zone and write books like Retinafy.me.
   Want me to speak at your conference? Contact me!

Build a Flash-like game with scripty2

April 30th, 2010

Flash games are facing stiff competition from JavaScript. You’ll see why when you follow along with this tutorial to create your first JavaScript-based game. How about a game of memory?

Naturally, we’re talking about animations here. The bar has never been higher in terms of interactivity in games. Games that don’t move are just no fun. Happily, in 2010, games built in JavaScript can be just as shiny, interactive, and fluid as games built in Flash.

All you need is a little help from a few open source tools.

For the visuals, we’ll use my Scripty2 animation framework, the successor to script.aculo.us. You’ll learn about easings, how to use and combine its built-in effects, and how to write your own. It’s easier than you think.

We’ll also use the Prototype JavaScript framework to write shorter, cleaner code, especially when managing the data structures and game logic. Like Scripty2, Prototype can change the way you think about JavaScript (and yes, we’re giving the latest and great version of Prototype a spin, which is available as Prototype 1.7 Release Candidate 1).

The best thing about learning to develop games is that the skills are useful even if you don’t want to be a game developer. Animations, easings, and the programming logic that drives them can help you make your “regular” web apps more useful and compelling. JavaScript as a technology has come so far, now it’s time for the interfaces built with it to catch up.

Check out the demo and play a game of memory!

Before proceeding, please download the tutorial files, and fire up a text editor of your choice to follow along!

1. Set up HTML skeleton

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>scripty2 Memory!</title>
    <link href="memory.css" media="screen" rel="Stylesheet" type="text/css" />
  </head>
  <body>
    <div id="field">
      <div class="card"><img src="back.png"/></div>
      <!-- 16 times -->
    </div>
  </body>
</html>

Our little memory game requires a playing field with 16 playing cards, a score field (1 point for each revealed pair) and a number of tries field (increments for each revealed card, regardless if a pair is found or not). It’s good style to add a DOCTYPE (we’ll use XHTML 1.0 Transitional) and define the character set as UTF-8.

2. Include JavaScript libraries

<script type="text/javascript" src="prototype.s2.min.js"></script>
<script type="text/javascript" src="memory.js"></script>

For fast loading, the best practise is to include JavaScript files at the bottom of your HTML, just before the closing tag. The tutorial demo code comes with all the files you need. Include the minified prototype.s2.min.js file, and the demo code in memory.js (which is not minified and heavily commented for easier comprehension).

3. Prepare card pair graphics

Fire up your graphics editor of choice and design 1 square card back graphic and 8 designs for the front (our memory game will use 16 cards, so it’s 8 pairs), resolution 140×140 pixels. The tutorial code comes with prefabricated graphics for this, with the scripty2 logo for the back and the numbers 1 to 8.

4. Shuffling cards

var cards = $$('div.card');
cards = cards.sortBy(Math.random);

To shuffle, first grab an array of all 16 card elements with var cards = $$('div.card') (memory.js, line 8), then use cards.sortBy(Math.random) (line 35) to randomly reorder. Array#sortBy is a Prototype JavaScript library function, to easily sort arrays. Feeding it JavaScript’s Math.random function is a handy shortcut. The expression returns a new array with the elements in random order.

5. Exporting internal functions for development

Object.extend(window, { shuffle: shuffle, win: win });

In Line 154 of memory.js, the shuffle and win functions are exported to the global namespace, for easy testing (no need to play through a complete game when you just want to tweak the winning animation timings). In the actual code, 'object' refers to the argument given to the outer wrapper function, which is 'window' (line 157), and'object' is the global namespace object when JavaScript is executed in a web browser.

6. Handling the click event

function dispatchClick(event, element){
  // the "element" argument contains the card that was clicked on!
  // now do stuff!
}

$('field').on('click', 'div.card', dispatchClick);

There’s two ways the clicking can be handled, either be registering event handlers on each card invidually, or using a dispatcher pattern. Opting for the latter is a good choice for games, as it allows tighter control. The dispatchClick function (lines 107 to 153) is registered on the “field” DIV, and detects which card was clicked on, by using Prototype 1.7’s handy new “on” function (line 151).

7. Add game logic

if(!element.retrieve('picture_revealed')){
  var id = reveal(element);
  
  $('tries').innerHTML = (++tries);
      
  // second card revealed, check if we have a matching card pair
  if(currentId){
    blocked = true;
    // yes, up score and run nice animation to remove the cards
    if(currentId == id){
      score++;
      updatescore.delay(1);
      
      (function(){
        cards.findAll(function(card){
          return card.retrieve('picture_id') == id;
        }).each(function(card){
          card.morph('opacity:0',{
            transition: 'pulse', duration: 1
          });
        });
        blocked = false;
      }).delay(0.5);
      
      if(score==8) win.delay(1.5);
    // no, hide all cards (after 1 second)
    } else {
      (function(){ 
        cards.each(function(card){ hide(card); });
        blocked = false;
      }).delay(1);
    }
    currentId = null;
  // first card revealed
  } else {
    currentId = id;
  }
}

There are two distinct possible states in the game when the player clicks a card:

  1. No card is currently revealed: reveal the card that is clicked on
  2. One card is already revealed: reveal the card that is clicked on and compare both shown cards, if a pair is found, remove those cards and update the score. Otherwise turn both cards on their back.

If the score reaches 8, the player wins the game.

8. Score keeping

// in dispatchClick, when a pair was revealed
score++;
updatescore.delay(1);

For each revaled pair, add a score point (line 121, 122). delay is a Prototype function that delays a function call for the given time, so the updating of the HTML is done a second later.

function updatescore(){
  $('score').update(score).setStyle('color:#77a638').morph('color:#aaa', 2);
}

The updatescore function sets the HTML, gives the new score a green color and then animates the color to fade to gray (line 85).

function shuffle(duration){
  $('score').innerHTML = score = 0;
  $('tries').innerHTML = tries = 0;
  // ...
}

Shuffling resets the score and number of tries counts (line 23, 24).

9. Add shuffling animation

// animate cards to go to new positions
cards.inGroupsOf(4).each(function(group, x) {
  group.each( function(card, y){
    flip(card, 'back.png');
    card.morph('opacity:1;left:'+(x*SIZE)+'px;top:'+(y*SIZE)+'px', {
      duration: duration || 0.5, transition: 'easeInOutQuint'
    });
  });
});

In the shuffle function, the movement of cards to a new position is achieved with just a few line of JavaScript, mostly to define animation options (lines 43 to 45). One key to good use of animation is to keep the duration short, as the animation should visually indicate that shuffling takes place, but shouldn’t feel sluggish or too drawn-out.

10. Basic easings

Easings are a mathematical function describing a change in timing. By default, Scripty2 runs effects with easing, so movements accelerate at the start of the effect, and decelerate smoothly at the end of the effect. There’s several predefined easings available, some of them taken from Flash game programming. Learn more and play around over at the scripty2 documentation.

11. Flip card animation

function flip(element, image){
  var img = element.down('img');
  img.morph('width:0px;left:70px;', {
    duration: .2, transition: 'easeInCubic',
    after: function(){
      if(image) img.src = image;
      img.morph('width:140px;left:0px', {
        duration: .2, transition: 'easeOutCubic'
      });
    }
  });
}

When revealing cards and hiding them again (when no matching pair was found), a “card flip” animation . To do this, two animations are chained: 1. the width of the image is gradually reduced to 0 (while keeping the height), accelerating troughout, 2. the card images is switched, and the width is expanded back to 140, decelerating.

12. Found a pair animation with advanced transitions

The transition system can be used for advanced scripting of the effects, for example for making an animation swing from or to a position, bouncing elements, pulsating or throbbing or even for random flickering. An effect often used in games is pulsating or blinking to grab the player’s attention.

card.morph('opacity:0',{
  transition: 'pulse', duration: 1
});

When a matching pair of cards is found by the player, fade those cards out, but use a pulsate transition when doing so.

This makes the cards “blink” smoothly and communicates the idea of accomplishment to the player (play a round of any jump and run video game and you’ll find similiar animations).

13. Winning animation

function win(){
  cards.each(function(card){
    card.morph('opacity:1');
    flip(card, urlForId(card.retrieve('picture_id')));
    card.store('picture_revealed', false);
    card.morph('top:0px;margin-left:'+((Math.random()*50) - 25)+'px', {
      propertyTransitions: {
        opacity: 'easeInOutQuart',
        marginLeft: 'pulse',
        top: 'bounce'
      },
      delay: 0.4+Math.random()*5,
      duration: 3+Math.random()*7
    }).morph('opacity:0;top:-100px;margin-left:0',2);
  });
  
  shuffle.delay(17);
}

Remember the animation when you win a round of Solitaire on Windows? You can do better! The example code shows a combination of several animations. First, all cards are shown with the numbers revealed, next there’s a smoke-like upwards movement and finally the cards disappear. Use win() on the console (or win the game!) to see this. After a slight delay the game is reset.

14. Preloading card graphics

// preload images $R(1,8) is like [1,2,3,4,5,6,7,8] (for this purpose)
$R(1,8).each(function(id){
  var img = new Image();
  img.src = urlForId(id);
});

function urlForId(id){ 
  return 'picture_'+id+'.png'; 
}

Because only the card backs are initially shown, preloading the card images makes sure there are no messed up card flipping animations.

15. Tweaking animation timings

The single most important thing with games is that everything should feel just right. Spending time on choosing the right transition animations, durations and delays is very important. This “motion design” is similiar to trying variations in graphic design. Don’t be shy and try out various settings and have fun fooling around. I’ll leave this step up to you!

16. Fix opacity on IE8

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

Internet Explorer 8 is a bit peculiar when it comes to support CSS opacity. It still doesn’t support the ‘opacity’ property like any other browser, but also breaks support for ‘filter: alpha(opacity=XX)’, which worked fine in IE6 and IE7. To work around this, insert above meta element in the HEAD section of the HTML.

17. Avoiding “stuck” cards

Randomly reordering doesn’t guarantee that a card is in different positions on each shuffle (e.g. card #13 might be in position #6 on both random shuffles). To avoid this, set up a loop that keeps reshuffling until all card positions are swapped. Use Prototype’s zip and any functions to keep the code short and concise.

// randomize cards and reposition them randomly, in a 4x4 grid
// 1. make sure all positions change place
// this can take several iterations
// [].concat() is used to create a copy of an array
var currentOrder = [].concat(cards), matches = true;
while(matches){
  // zip combines two arrays into one
  // [1,2].zip(['a','b']) --> [[1, "a"], [2, "b"]]
  // this can be used to compare two arrays (in this case, find "any" matching pairs)
  cards = cards.sortBy(Math.random);
  matches = cards.zip(currentOrder).any(function(pair) { return pair[0] === pair[1]; });
}

18. Final touches

It’s a good idea to harden your code, both against user intervention (wildly clicking on things!) and programmatically (wrapping it in its own namespace or JavaScript closure). In the demo code the “blocked” boolean is flipped when click events should be suppressed (lines 9, 109, 118, 132, 140), and everything is wrapped in a JavaScript closure pattern (lines 4, 157).

That’s it. Awesome browser games. No Flash required.


About the author

Thomas Fuchs has been writing hard-core JavaScript since waaaaay back in the late 1990s.

His famous script.aculo.us framework was created during the development of one of the most highly interactive applications the Web had ever seen. He’s continued to push the boundaries of what is possible with JavaScript, with Scriptaculous and its successor, Scripty2.

Script.aculo.us has gone on to be used in such web sites & applications as CNN.com, NASA.gov, Me.com and more.

In addition to being a Prototype core member and Rails core alumnus, Thomas is one of the world’s top JavaScript and rich web app performance experts. He is also the author of Textorize, an automated tool that brings beautifully anti-aliased image text to the web (and beats the pants off Photoshop).

He is a self-described “artsy wanker.”