Skip to main content

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)

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.