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?

Running a Git repository on Ubuntu using Gitosis

20I’ve been using Git for a couple of small projects that I’ve been hosting on github.com but version control for my bigger ‘secret’ projects still runs on a windows machine with visual svn server.Now that I’m starting to use Mono for a couple of projects so I’m playing with linux more. Last week I decided to try to try out gitosis on an ubuntu server. I found out it’s pretty easy to use when you know your way around git but for a noob like me some things weren’t immediately clear. Eventually I solved most problems I ran into, so I decided to write up the steps I took to install gitosis on ubuntu 10.04

Android development resource links

I've been playing with the Android SDK and I have a growing list of bookmarks to Android dev resources for my own use. I thought the best place to keep them would be here on my blog. That way other people can benefit too. I'll keep updating this list so feel free to add suggestions in the comments.