These notes assume that you are familiar with the game of
Tetris. If not, we suggest that you first read the
Wikipedia entry on Tetris
and then play the game a bit.
These notes also assume that you are familiar with the
JComponentWithEvents class in our 15-100 course.
Here is Tetris as we will write it:
applet: tetris.html
jar file: tetris.jar
zip file:
tetris.zip (which is just a zip file containing tetris.jar)
As should be clear when you run it, this is a simplified version of Tetris,
and many interesting features have been left for you to add as bonus (see
step 8 for details).
And here is a picture of the game in action (note that there have been some
minor changes since these pictures were made, so you may notice some small
cosmetic differences between them and the code above, such as the color
scheme and where the score is displayed):
Our design includes two main elements: a board
and a falling piece. In the picture, the board contains all
blue (empty) cells except in the bottom three rows, where it contains
various other colors. The falling piece is the red piece in the
picture. In our design, this is not part of the board, but is
drawn over the board.
More specifically, the board is a 2-dimensional array of Java Color
objects. Initially, the board is full of one color, the emptyColor
(Color.blue in our sample code). As falling pieces are transferred
onto the board, other colors are introduced.
The goal of the game is to fill rows entirely with non-empty
colors.
The falling piece is a 2-dimensional array of
booleans, indicating whether the given cell is or is not painted in this
piece. For example, here is the definition of an S piece:
private static final boolean[][] S_PIECE = {
{ false, true,
true},
{ true,
true, false }
};
As noted, the falling piece is not part of the board, but painted over
the board. It becomes part of the board when it can no longer fall and
we place it on the board, which will not happen until several steps from now
in our design process.
The game is graphical and event-based. This is
achieved by extending the JComponentWithEvents class.
Many more details are available on the following pages.
David Kosbie |