
I’ve been busy for the last month, working with a couple of friends to put together an online version of Ninja Pirate Zombie Robot. This was initially going to be an iPhone app, written in Objective C, but after further consideration, we decided to write it using HTML 5 canvas.
The libraries we are using for our JavaScript card game are JSUnitTest, event.simulate.js and JS Prototype. The choices here are largely due to our experience of writing test driven, object oriented code as our day jobs. This was our first game, so this post may not reflect the very best way to write an HTML5 game, so if there are things we could improve on, please let me know.
Test first
This article will just cover our initial set up. I’ll write a subsequent article on tips and tricks we learnt along the way. First we set up a test case using JSUnitTest. This comprised of 3 files.
HTML web page which runs and displays the results of the tests
-
-
<html>
-
<head>
-
<title>JavaScript unit test file</title>
-
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
-
<script src="../lib/jsunittest.js" type="text/javascript"></script>
-
<script src="../lib/prototype.js" type="text/javascript"></script>
-
<script src="../lib/event.simulate.js" type="text/javascript"></script>
-
<script src="../src/game.js" type="text/javascript"></script>
-
<link rel="stylesheet" href="unittest.css" type="text/css" />
-
</head>
-
<body>
-
-
<div id="content">
-
-
<div id="header">
-
<h1>Ninja Pirate Zombie Robot unit test file</h1>
-
</div>
-
<!– Log output (one per Runner, via {testLog: "testlog"} option)–>
-
<p>
-
Running <strong>test_game.js</strong>.
-
</p>
-
<div id="game"></div>
-
<script src="test_game.js" type="text/javascript"></script>
-
</body>
-
</html>
-
The TestCase
-
-
new Test.Unit.Runner({
-
setup: function() {
-
this.game = new Game()
-
},
-
-
teardown: function() {
-
this.game = null
-
},
-
-
testGameStartsWith2Players: function() { with(this) {
-
assertEqual(2, game.players.length)
-
}},
-
}, { testLog: "game"} )
-
And the bare minimum initial game code
-
-
var Game = Class.create({
-
initialize: function(options) {
-
this.players = []
-
},
-
})
-
This was our starting point for our game, which yielded one failing test. We decided to build the rules of the game first, before dealing with the rendering and computer player components. Given that we wouldn’t have the chance to pair on most of the code, we wanted to get the central domain model for the game sorted, so we could then work on other parts in parallel.
In my next post, I’ll cover defining the game domain model, which encapsulates all the rules of the game.
