Tag Archives: coffeescript

Tetris in CoffeeScript

From July 26th through Aug 9th, the team conributed to a Tetris clone written in CoffeeScript: coffee-canvas-tetris

CoffeeScript is unique in that it is compiled into Javascript. Although the syntax is a bit unusual, I found it to be much more concise and easier to read than Javascript. Here is an example of something I contributed to the project:

$(canvas).click -> pause_or_unpause()

The compiled JS looks like this:

$(canvas).click(function() {
  return pause_or_unpause();

So although you’re probably confused about the arrow and dollar sign, I’m sure you can see how much nicer it is to read in CoffeeScript. If you take a look at the project source code, you’ll see just how much of a difference it makes. CoffeeScript was inspired by Ruby and Python, which is why the syntax is so concise.

Beyond just making the code look pretty, CoffeeScript offers a lot of built-in functionality that Javascript lacks. For one, it allows you to define classes, rather than forcing you to use prototypical inheritance. It also offers comprehensions, which are loops but treated as expressions– meaning they can be assigned like a variable.

The most difficult part of this project was setting up the development environment. Just about everyone on the team had difficulties with the dependencies required to run this project. The easiest way (in linux) turned out to be to just download the source and run “sudo bin/cake install” from within the downloaded folder.