CHAPTER 3
Tetris Game
Part 2
Functionality to be added:
• Transfer of color to background
• Color validation
• Move right and left
• Side boundary validation
• Rotate
• The spacebar must be programmed to
pause and restart the animation as a
PART 2 CRITERIA
1. Color Transferred to board
when brick stops falling.
2. Pause key on typing space bar
3. Key listener installed in
TetrisDisplay class
4. Functionality of left and right
move of shape
5. Validity checking on left and
right move.
6. Functionality of rotation and
unrotation (also an abstract
method)
7. Validity checking on rotation
move.
8. New Game when ‘N’ is pressed
9. Pause game on ‘space bar’
SECTION 1
Evaluation Criteria
In part 2, you will be evaluated on new aspects of the game.
The points you earned points in Parts 1 will not earn points
this time. However, any errors not corrected from previous
parts will earn you penalty points. The UML is shown below,
with all new methods shown in red.
2
For this phase of the development of the Tetris game we will
be accomplishing the following:
•Transfer the brick color to background
•Move Tetris brick right and left
•Rotate Tetris brick
•Validation of move extended to side boundaries and color
•New game capability
Here are the new methods. They have arguments and return
values only where indicated.
1. TetrisWindow – no new methods.
2. TetrisDisplay –
• translateKey () – called within the keyTyped method of
the anonymous KeyListener. This method will call the
make move method from the game with the appropriate
move code and takes the key event as argument.
• drawBackground() – Will draw the content of the
Background attribute of the game object taking Graphics
object as argument.
3. TetrisGame –
• initboard – will set the background attribute to default
content.
• newGame – will start a new game.
• fetchBoardPosition – will return the integer content
of the game attribute, background, based on the row and
column passed to the method.
• transferColor – when a brick has no place to fall, it’s
color is transferred to the board array in game.
4. TetrisBrick (abstract class) – New methods:
• rotate- abstract method in Tetris brick, and must be
overwritten in each individual brick subclass.
• unrotate- abstract method, used if validation of rota-
tion move fails. It is an abstract class in Tetris brick, and
must be overwritten in each individual brick subclass to
undo the rotate move
• moveLeft- translates each segment of the brick one cell
to the left.
• moveRight – translates each segment of the brick one
cell to the right.
Overall Functionality to complete in Part 2:
It is recommended that you proceed to build up you game one
functionality at a time, debugging as you go. Here is the rec-
ommended order.
1. The colors from the current falling brick must be trans-
ferred to the background when the brick has stopped falling
and no brick may fall over a colored brick in the back
ground (color validation).
2. You must install the key listeners in the Display class.
3. Make the space bar the Pause key that toggles pausing the
animation of the game and restarting it.
4. The functionality of the left and right arrow keys must in-
corporate move validation, meaning it cannot over run the
3
side boundaries of the game board or move over a colored
segment.
5. The rotation functionality in the game class must be com-
plete (calling the rotation method of the falling brick) along
with validation of the move , meaning it cannot over run
any boundaries of the game board or move over a colored
segment.
6. A new game must be started when ‘N’ is typed.
Use the Criteria table posted on the Moodle submission site
as a check off list for the completion of your project BEFORE
submission. You should know what your score will be before
you submit your work. Remember, if one part of the cri-
teria is not correct, all points for that criteria are
lost. There is no partial credit. This is to establish the impor-
tance of completing a task and attention to detail.
Also, use the penalty table provide at the submission point, to
check for possible loss of points for poor programming form
or improper submission practice. Again, full points deducted!
If you have any questions on a criteria, ASK!
Enjoy learning and don’t wait too long to come for assistance!!
4
INCORPORATING COLOR AND VALIDATION COLOR
Color of the current brick is entered into the bricks
last valid position on the board. (transferColor)
A move cannot allow the current brick to cover any
part of the background that has a color number
greater than Zero. (validateMove)
SECTION 2
Transfer of color into back
ground
The first functionality to be added is incorporating the color of
the current brick into the background. This is fairly straight
forward. Remember the following
The well is divided into cells that are initialized to zero or -1 or
any number you wish to use to indicate no color. You should
coordinate the color number of a specific Tetris brick with the
index of the rray of colors in the display class.
The diagrams that follow were created by drawing an outline
around each cell representing the available positions within
the new background attribute that will represent the content
of the well, especially as the bricks accumulate.
I have also printed the value of each cell to the center of that
cell. This is for teaching purposes. You can see that as the
5
brick falls (fig. A) the color is not incorporated into the the
background. The falling brick is drawn over the background.
But when the brick reaches the point where it cannot fall with-
out violating either a boundary or a color condition ( fig. B ),
then the color of that brick is incorporated into the background
and a new brick spawned. In this example the first color num-
ber is 5 for the light blue.
Each time the falling brick stops, the color is incorporated into
the background ( fig. C through fig. E ). This process continues
until there is no room at the top of the background, at which
point the game is over.
The process of incorporating the color into the background is
simple. It consists of assigning the color number of the falling
brick to the coordinates of the background that are stored in
the positions array of the the falling brick, after the final move
is validated.
The pseudo code for transfer of color process is given below
1. REPEAT for each segment of the brick’s position array
2. the background element designated by coordinate stored
in that segment are assigned the falling brick’s color
number
Color validation (validateMove method) uses the process of go-
ing through each segment of the falling brick, taking its coordi-
nates (row and col of background) and checking that position of
the background, to make sure that the value stored there is not
a color number. In the case of this example, check that the
number is not zero. If the value is not zero, the move in NOT
VALID.
6
VALIDITY OF SIDEWAYS MOVE
1. Moving left – subtract 1 to each segment’s
column
A. Check that the column coordinate is not less
then 0
B. Check that there is no color in each segment
of the new position
2. Moving right – add 1 to each column
A. Check that column is not greater or equal to
maximum Column index
B. Check that there is no color in each segment
of the new new positions array.
SECTION 3
Moving left and right The process of moving a brick to the left or right, is the same
for all bricks and as such this method must be implemented in
the TetrisBrick class. These methods had not argument and
n0 return value.
When the left arrow key (VK_LEFT) is pressed: To move
left, the column coordinate of each segment of the brick is
decremented.
When the right arrow key (VK_RIGHT) is pressed: To
move right, the column coordinate of each segment is incre-
mented.
In general, the makeMove, validateMove and the associate
move methods of the brick classes work together. The only
method of these that has an argument is makeMove be-
cause in must translate the move code.
To validate the left move (validateMove method), the brick
must be in bounds (each segment must have a column num-
ber greater than -1) and it must be color validated (see section
2 for this technique). If the move is invalid, it must be moved
back (move right).
Like wise, to validate the right move, it must be in bounds
(each segment must have a column number less than the
number of column in the array) and it must be color validated
(see section 2 for this technique). If the move is invalid, it
must be reversed (move left).
7
VALIDITY OF ROTATION MOVE
1. rotation – each sub brick has a unique
rotation method
A. Check that columns is not less then zero or
greater then he number of columns
B. Check that rows are not equal or greater
than the number of rows
C. Check that there is no color in new positions
SECTION 4
Rotation
Since the coordinates for each type of brick are different, the
method to rotate the brick is different for each brick and as
such must be implement in in each individual subclass of the
TetrisBrick. What you should notice is that there is different
symmetry among the different bricks. For example, the square
brick has only one appearance, no matter how you rotate it
while the long brick has 2 appearances and the stack brick has
4 appearances. As such the rotation methods will be different.
So, when the up arrow key (VK_UP) is pressed in the dis-
play class, the move code will indicate rotation. To rotate the
sub brick, the rotate method in each sub brick (no argu-
ments) will need to be unique to that sub brick. For example,
the long brick has only two orientations; horizontal and verti-
cal and as such only needs two configurations, which always
pivot on segment 2. As such segment 2 does not change in
the rotation. The same can be applied to the EssBrick and
JayBrick.
8
The StackBrick, ElBrick and JayBrick have four orientations
that must be rotated through.
For example, the StackBrick will rotate around segment 2,
which is its center segment
as shown to the right. The
center segment does not
change. The initial positions
of each segment based on
this center segment is
shown to the left.
The un-rotate method is needed when the rotate method pro-
duced an invalid move. An invalid move must be undone in
the makeMove method.
For a brick with only two orientations, the un-rotate can be
accomplished by simply rotating one more time. For a brick
with all four orientation, simply rotate three more times to
undo the rotation. Remember, you can call a method from the
body of another method. Reuse your code, don’t reinvent!
The method shown here is only one way to do the rotation.
You can also reference each segment from the center segment.
It is your choice of how to do this implementation.
9
- Tetris Game Part 2
- Evaluation Criteria
- Transfer of color into back ground
- Moving left and right
- Rotation