Mobile Tetris
Simon M. Lucas

 

Introduction

This page links to the source code for my mobile phone version of Tetris, and outlines some of the software design.  Brief instructions are provided for how to install it on your mobile phone.  The current version of the game is mostly functioning, and the games plays reasonably well.  However, there are two main limitation:

  • the high score does not get saved
  • the start button does not work - you have to exit the game and select it again to play another game

Fixing these little problems is left as an exercise for the reader!

The following images show the game running on the emulator and on my phone.

Installation

There are two ways to install it to most mobile phones.  One is to copy the .jar to a suitable directory of your phone.  Note: I have only tested this for my phone (the lovely Sony Ericsson W880i).  The instructions work fine on that; other phones may differ...

Once copied on to your phone (using either Bluetooth or USB), simply click the tetris.jar file and it should prompt you to install it.

The other is to use OTA (Over The Air) installation.  This is still a bit experimental:

Software Design

The code is split into four classes, and approximately follows a Model-View-Controller architecture, except that the controller and view classes have been combined.  The reason for this is simplicity. The Canvas class from the Java Mobile Framework (for graphical display of the game objects) is set up to handle all the key events (the events generated when a button gets pressed on the phone).  These events could be caught and passed to a separate controller, but all the action takes place in the model, so there's not really much point in doing that. The UML diagram below shows the classes involved, and the relationship between them.  Each class box shows only the method signatures. 

Explore the code for more details.

Some code snippets - these show key (button) event handling, and the game main loop (which could be simplified a bit).

Source Code

Download from here:

Note that the TetrisCanvas class is not actually used - this was just a simple test to create a window on the phone screen.

Wireless Toolkit

Feel free to modify the code as you wish.

To take this further you'll need the Wireless Toolkit from Sun.

If you explore the toolkit's examples directory, you might be able to work out how to add sound effects to the game (see the AudioDemo there).

end of page