DEV Community

Chad
Chad

Posted on

Tetris Development #5 - The HOLD Piece

In this post, I will talk about the implementation of the HOLD piece in our Tetris game.

First, what is a HOLD piece? In Tetris, there is a HOLD slot, which is empty at the start of the game, and the player can press a button to put the currently controlled piece into the HOLD slot to be used later. The game then continues as if the hold piece vanished. When the player thinks it is time to use the HOLD piece, they can press the same button again to swap the HOLD piece with the currently controlled piece and continue the game.

This allows for strategic planning and setup, where players can HOLD a useful piece to be used later in the game.

"Ok but how do we implement it?"

Logic

I started by adding a new variable for HOLD piece, and it is empty at the start of the game.

When the HOLD button (spacebar) is pressed, the game logic will check if there exists a piece being held. If there isn't, the HOLD piece will clone the current active piece, and the active piece will be replaced by the next piece in line.

If there is already a piece being held, then pressing the HOLD button will swap the held piece with the active piece.

Details

There are two important details in the HOLD implementation.
Firstly, when the active piece is replaced by the HOLD piece, the drop indicator will need to change as well. Without it, the drop indicator will still show where the original block (now in HOLD) will drop, instead of the newly swapped piece. Therefore, extra logic on the drop indicator piece was implemented to correct the behavior.

Next, to ensure consistency, I made it so that when a piece is put into HOLD, its orientation (i.e. rotation) is reverted back to upright. This means that every time the player swap a piece out of HOLD, the piece will be in an upright position, instead of whatever position it was when the player pressed HOLD. Since there are 4 possible orientations, if the player has to remember which orientation the HOLD piece was in, it is a huge burden especially if the player is swapping between different HOLD pieces. Thus, by reverting to always being upright, it adds more consistency to the outcome when using the HOLD functionality.

UI updates

But the HOLD piece won't work if the player can't see it!
Thus, the HUD must now have space for the HOLD slot, and the player should be able to see what piece is being held right now. I updated the UI to free up some space on the top-right in order to show the HOLD slot.

Image description

That is the end of the HOLD piece implementation. Personally, I feel that similar to the Next Piece Indicators, the HOLD piece adds another level of depth to the game and more rewards to carefully planned gameplay by strategic players. This also makes the training of an AI to play the game later more interesting.

Top comments (0)