This page demonstrates an implementation of the classic game 'Tetris', in JavaScript. The major purpose is to demonstrate how addition to and manipulation of the DOM can be performed easily: the Tetris display consists of two hundred DIVs in a 10x20 grid, which are given different class names to represent the blocks in the well.

JavaScript also makes it easy to retrieve events from the keyboard; the 'window' object provides an 'event' subsystem, which in combination with the setting of event handlers for the keyboard allows the script to capture and process keys. This implementation of Tetris uses four keys:

  • Left: move the current piece left one block
  • Right: move the current piece right one block
  • Up: Rotate the piece by 90 degrees
  • Down: Push the piece down quickly

You're free to take a look at the source, and explore the game for any bugs which may exist (I already know of a couple). Enjoy.

Article dated: 4th Oct 2007

Get the RSS feed