Home | About | Apps | Github | Rss

Tetris in Javascript and Canvas

I love tetris. I have never written tetris before and I have always wanted to. Attempted it once but couldn’t figure out heads and tails of it and then left at it. I wanted to get over it. So i wrote it, in less than 4 hours (bragging rights) in JavaScript using canvas. Feel free to browse the source or Play it here

http://kalyanchakravarthy.net/projects/fun/jstris/

During the process I learnt how to use the javascript “with()”. Despite all the cliches around it, I feel its a pretty awesome and useful language feature. It logically made sense to use it to group statements together, especially the drawing functions.

with( stage.ctx ) {
	fillStyle = "#fff";
	fillRect( theX+1, theY+1, stage.cellSize-2, stage.cellSize-2 );
	fill();
}

Isn’t that neater than this?

stage.ctx.fillStyle = "#fff";
stage.ctx.fillRect( theX+1, theY+1, stage.cellSize-2, stage.cellSize-2 );
stage.ctx.fill();

Imagine doing it for 20-30 statements.


More posts