Skip to main content

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.

Comments

Popular posts from this blog

Using xUnit.Net with .Net 4.0

I’ve been using xUnit.Net for a while now. It’s just a tiny bit cleaner and slightly less abrasive than other .Net unit testing frameworks. Leaving out unnecessary stuff like [TestFixture] and shortening Assert.AreEqual to the equally clear but shorter Assert.Equal don’t seem like big improvements but when you type them several times a day tiny improvements start to add up. I also like the use of the [Fact] attribute instead of [Test]. It shifts the focus from testing to defining behavior. So how do we get all this goodness working with the Visual Studio 2010 beta?

Square One available on the Android market

This is just a short post to let you know that a first version of the Android app I’ve been working on for the last couple of weeks is available on the Android market. The app is called Square One and it’s a simple bassline synthesizer. It’s free so try it out and let me know what you think of it, but be prepared it’s still an early version. I hope to add more features in the next few months and maybe build something that can be used to create real music.The lower part of the screen contains the sequencer controls that can be used to program your own bass lines. On the left is a four by four grid of buttons where you can select a step in the sequence. On the right you can select the note to be played on that step. When you’re done you can press Start and the sequence starts playing. The knobs on the top can be used to control a couple of parameters from the synthesizer engine that creates the sound. You can control the cutoff frequency and resonance of the low-pass filter, attack and …

Building Android projects with Jenkins, Ant and Mercurial

I have recently set up a Jenkins build server for my Android projects hosted on Bitbucket. It’s not difficult but there are a couple pitfalls and the information on how to do this isn’t available from one single place so I decided to document the process and put up the information over here. Maybe other people will benefit from having a step-by-step guide too.