Finishing Tetrominos
A few hints on how to finish the Tetrominos game.
At this point, you should have a well-organized working program that lets you draw colored blocks on the screen. As this is a challenge project, the rest is up to you. I recommend breaking the project down into manageable chunks, setting some intermediate goals, and reaching those goals one at a time. There is no hurry – take plenty of time to think through each step, and test it before going on to the next item.
Here are some things you’ll need to do:
-
Create the falling pieces, which are made up of multiple blocks. I wrote a class
Piece
to keep track of information for a small group of blocks. -
Each block needs a physical location within the piece – the layout of the piece. I created a class
Location
that simply stores an x, y pair. APiece
then contains a list of locations. -
Each Piece needs to be able to fall, so each piece needs to know where it is on the screen. I added an instance variable
position
of typeLocation
to the Piece class. I wrote a methodtranslate
in the Piece class. To actually make the piece fall, I had a line of codeactivePiece.translate(0, -1);
in thenextTurn
method of the Board class. Since thenextTurn
method is called every second or so by the Tetrominos user interface, this causes the active piece to fall downwards one step at a time. -
The blocks should translate left and write as the user presses the
s
andf
keys on the keyboard. When those keys are pressed, theslide
method of Board.java is called. Finish theslide
method. -
Add rotation in response to key presses. This step is trickiest, and you might leave it until last, since you can write a working game without it. You’ll need to figure out how locations within a piece change on rotation.
-
Check for collisions between the falling active piece and the bottom of the screen, and blocks that are already sitting on the bottom of the screen. When there is a collision, copy block locations from the Piece to the Board to represent the leftover blocks, and then create a new active piece.
-
Check for complete rows of blocks in in the board, and delete them.
Good luck, and happy coding!
Get hands-on with 1400+ tech skills courses.