March 28, 2010

JavaScript Tetris part III

Here’s just a short update on the Tetris project (if you can call it a project). I had a pretty busy week but I did find some time to play around some more with JavaScript, try out some GOF creational patterns, and build a nice domain model to make working with squares and blocks a bit easier.

This time I wanted to make the falling blocks look like real Tetris blocks. Right now the whole game state is pretty much an x and y position of the falling block, but in order to draw more complex blocks my domain model needs to get a bit bigger too.

The first thing I did was encapsulate x,y value pairs in a Vector object. I built a Square object with an x, y and a color and I built a Block object to hold several square objects to form one of the well known Tetris blocks.

The Vector, Square and Block classes only expose their properties through methods, that way they can’t be changed after creation. Actions on these objects will just return a new object, for example moving a square will leave the old square unchanged. This is something I’m used to in C#, and it looks pretty clean in JavaScript too. Not sure if this is common practice.

   1: function Vector(x, y) {
   2:     this.x = function() { return x; }
   3:     this.y = function() { return y; }
   4:     
   5:     this.moveLeft = function() {
   6:         return new Vector(
   7:             x - 1, y);
   8:     }
   9: }

This is a small snippet of the Vector class to illustrate my point. The x and y are implemented as functions instead of properties. The Game object stores the current position in a Vector and just calls moveLeft when the user presses the left button to get a new vector one step to the left. This means I go around creating and throwing away more objects. But I have less side effects, which seems more elegant to me especially in a functional-ish language like JavaScript.


In order to make it easy to build the seven types of blocks from code I tried implementing some of the creational patterns. Eventually I things weren’t so complex that I needed a factory object or a builder so I went with a simple factory method ‘buildBlock’.



   1: Block.buildBlock = function(color, coordinates) {
   2:     var squares = new Array();
   3:  
   4:     for( var i in coordinates) {
   5:         squares.push( new Square( coordinates[i][0], coordinates[i][1], color ) );
   6:     }
   7:     
   8:     return new Block( squares );
   9: }

Instead of creating this method in the constructor method of Block like I normally do I tacked this method on to the Block constructor. This way you can call it without having an instance of Block, a bit like a static method in C#.


Now I can define the different types of blocks like this.



   1: Block.blocks = 
   2:     [
   3:         Block.buildBlock('maroon', [[-1,0], [0,0], [1,0], [2,0]]),
   4:         Block.buildBlock('lightgrey', [[-1,0], [0,0], [1,0], [1,-1]]),
   5:         Block.buildBlock('magenta', [[-1,-1], [-1,0], [0,0], [1,0]]),
   6:         Block.buildBlock('darkblue', [[0,0], [1,0], [1,1], [0,1]]),
   7:         Block.buildBlock('green', [[-1,0], [0,0], [0,1],[1,1]]),
   8:         Block.buildBlock('brown', [[0,0], [-1,0], [1,0], [0,1]]),
   9:         Block.buildBlock( 'cyan', [[-1,1], [0,1], [0,0], [1,0]])
  10:     ];

Pretty clean if I say so myself.


As usual you can download the code and play around with it yourself.


tetris0.03.rar (13.79 kb)


Next up, rotating blocks.

No comments:

Post a Comment