Javascript Tetris Pt 1: Rationale

UPDATED: Fixed a bug in collision detection causing tiles on far left to fall through.

I’ve wanted to learn Javascript for a while now - and I’d like to learn it properly this time.

Of course I’ve used a smattering of Javascript code here and there. Small stuff, like input validation in web pages. Unfortunately, I’ve mostly relied on good old “google-copy-paste-tweak” coding. I even used serverside Javascript in a project at one point, but I never really made the effort to properly learn the language.

And I don’t think I’m the only guy guilty of an aversion towards it. If you come from strongly typed, compiled languages and suddenly have to accomplish something using browser scripting, you might have the same reaction I did: “Eeeew!”.

“No fancy IDE or compiler to hold my hand? Annoying crossbrowser issues? The syntax sort of looks like java… but without classes or type safety? Screw this!”

In the years after that, Javascript usage took off as a part of the AJAX/Web 2.0 phenomenon… and I mostly ignored it for a while, having been burned by my earlier experience. Fortunately I saw the light eventually. :)

I learned to like interpreted, dynamic languages through Ruby, so Javascript syntax started to make much more sense to me. Fiddling with functional programming also helped. Javascript has powerful stuff like higher order functions, closures, eval and metaprogramming. The language actually started as a Scheme dialect, before the author was told to “make it look like Java”.

Javascript also turned into a more reliable tool through a bunch of new libraries. These extended the language and hid some of the icky crossbrowser problems.

Finally, and most importantly, Javascript has become very pervasive. Disregarding the standard Web 2.0 applications, the language can be applied in a number of different contexts - embedded wherever you have a JVM, in Flash/Flex, the Google App Engine… I love tools I can take with me everywhere.

Steve Yegge’s talk on Rhino was the final straw; I needed to learn the core language. Also, optional bonus points for updating myself on DOM scripting and CSS.

What’s the best way of learning a tool? Use it to create something! We want to build something not overly trivial, with a variety of requirements on input, output, data structures, state and program flow… so let’s build a game. We’ll have to deal with graphics, sound, user input and game state. Perfect.