April 7, 2010

JavaScript Tetris update

Here’s the last post in my JavaScript Tetris series. Time to show the final version of the Tetris implementation I built. It’s not completely finished but I did come pretty far. I only put in a few hours in total. I’m going to race through the final implementation in this post. After this I’ll go back to writing about C#. I promise :-)

There’s a couple of stories I want to implement in this post;

  • Rotating blocks
  • Bounds checking. We want to make sure blocks don’t move outside the playing field.
  • When a block reaches the bottom of the playing field it has to stack up in a wall.

Rotation

The first big change I made was to implement rotation. It’s actually pretty easy to rotate a block 90 degrees. Just swap the x and y coordinates of all the squares that make up the block and make one negative. Negative x means rotate left and negative y means rotate right. To make it possible to rotate around other points than 0,0 I just move the block before and after the rotation. Blocks now have a center of rotation property too.

Bounds checking

In order to implement this easily I had to shift around some object responsibilities. Blocks didn’t know about their position, so in order to know if a block was outside of the playing fields I had combine information from the game object (block position) with information from the block (block shape). It would be more logical to have blocks maintain their own position. After I made this change it was pretty easy to check if a block moved outside of it’s bounds.

tetris0.04.rar (15.32 kb)

Wall

This seems like a pretty big feature but most of the functionality for it was already there. All the wall has to do was maintain an array of blocks and do some collision detection. I had this implemented in a couple of minutes. Hooking it into the presenter and the game object was easy too. I actually spent some time moving the bounds checking logic into the wall object too.

Thoughts

There’s still some stuff missing. You can’t start/stop the game. The game doesn’t detect when you lose. And most importantly it doesn’t keep score. These are all pretty easy to implement but my goal isn’t to build a complete implementation of Tetris. It’s to learn more about JavaScript.

So how do I feel about JavaScript now? To be honest I’ve got mixed feelings. It’s a pretty interesting language. Lots of functional elements. I like that. And I’d like to get a bit more into the prototypical nature of JavaScript objects. But there’s a lot not to like about JavaScript too. I had some issues where different browsers worked differently. For example I wrote some tests checking the type of returned objects that broke in Internet Explorer. On other occasions Chrome and Firefox were too lenient with functions that were passed incorrect parameters and failed in a completely unrelated place.

Oh, and there’s no parameter overloading for functions.

I might be spoiled with my programming languages but JavaScript seems a bit like version 0.9b of a language that’s going to blow everyone away in version 2.0. (and yes, I know I’m going to be flamed for this)

No comments:

Post a Comment