Brief Description
¡ì This is a Tetris game.
¡ì To start a new game, click "START" button.
¡ì Then click inside the playing field and start playing.
¡ì Using arrow keys to control the movement of the dropping
figure:
| LEFT | moving left | UP | clockwise-rotating figure |
| RIGHT | moving right | DOWN | speeding up the dropping |
¡ì Check the radio button to switch the shape of dropping figure between
box and ball.
(!!Don't forget to click
inside the playing field to reactivate the arrow keys after every switch!!)
¡ì There are 12 levels altogether, with gradually
faster dropping speeds respectively.
¡ì Gaining every 5000 points advances the player
to the next level. And,
Points for landing a figure: 10
Points for filling up one row: 500
Points for filling up four rows in a time: 3000
Complete
Instructions for compiling and running the program
¡ì Unzip all the files in Tetris.jar into one directory. If you
want to recompile it, you can delete all the class files, and run
javac
Tetris.java to
regenerate all those class files.
¡ì Make sure that
Tetris.jar and other image files are present in the same directory with the HTML file.
¡ì Embed the following code in your HTML file:
<applet
Archive="Tetris.jar" Code="Tetris.class" Width="150"
Height="344" Name="TetrisApplet">
Features
Summarization
¡ì Use java.awt.Color.darker( )
to get a better 3D visual effect of dropping figures.
¡ì Use java.awt.Graphics.dispose(
) to dispose the graphics object and release any system resources that is
using.
When a Java program runs, a large number of
Graphicsobjects can be created within a short time frame. Although the finalization process of the garbage collector also disposes of the same system resources, it is preferable to manually free the associated resources by calling this method rather than to rely on a finalization process which may not run to completion for a long period of time.
¡ì Use GUI controllers, like
canvas, panel, label, button, checkbox (radio button)
(I tried to use imagebutton/icon in SWING,
but it didn't work. :( )
¡ì Use hookmethods (placeholders) in class Shape, which provides basic functionality
for all shapes derived from it
(such as, TShape, LShape,
StickShape etc... ).
public abstract Color getShapeColor( );
// Will be defined by subclasses to return the color of the shape
public abstract Cells getLowestCell( );
// Will be defined by subclasses to return a point with coordinates
// of the lowest cell and number of cells to its right at the same
level
public abstract FilledCells getFilledCells( );
// Return an array of all filled cells for the current position
¡ì Use KeyListener
in class PlayingField, which
is an extension of Canvas component.
Its responsibilities are:
(1) Listen to
keyPressed event and carry out one of the following actions depending on a key:
a. Move figure to left or right
b. Rotate the figure
c. Drop the figure
(2) Track filled cells
and rows. If a whole row has been filled up, eliminate it.
(3) Check that the
height of the pile does not exceed the limit.
(4) Check when a figure
has hit the bottom.
class PlayingFieldKeyListener extends
KeyAdapter
// Listens to keyboard events generated by the
player and responds to them accordingly
¡ì To define more different shapes, just remember to:
(1) Write your own
class that extends Shape.
(2) Make sure that your
custom class defines the above three hookmethods in Shape
class.
(3) In the pickShape( ) method of the PlayingField
class, add the name of your custom class to the
shapes[ ] array. Just add a line to the 'if' block:
else if ( curPos == NEXTNUMBERINSEQUENCE )
return YOURCLASSNAME( this );
// where the NEXTNUMBERINSEQUENCE is the number from the previous
'else if' statement.
// and YOURCLASSNAME is the name of the class
file containing the shape.