DEV Community

Chad
Chad

Posted on

Tetris Development #4 - Stop, Drop (and Roll?)

Continuing my development from the basic Tetris game, I added some new features that will make the game more player-friendly and fun: "Next Piece" indicators, Ability to drop the block instantly, and a Drop Indicator that tells you where the block will fall to.

Next Piece Indicators (NPIs)

I built the basic version of the Tetris game with 1 Next Piece Indicator (NPI), meaning that the player can see what will be the next ONE Tetris block coming up.

However, one block seems pretty limited in allowing the player to plan ahead -- c'mon, with only one NPI, when I see that the next block is an I-block (the straight linear Tetris), most of the time it's already too late to set up for a satisfying clear with that block. But if I can see THREE steps in advance, and know that the I-block is coming in THREE turns, as a player I can (or hope I can) definitely cook up some strategies!

Moreover, considering that I am planning to build an AI for this game (remember this is a Game & AI development!), looking 3 steps into the future makes the AI development much more interesting.

Therefore, I added more NPIs to the game, allowing players to see up to 3 upcoming pieces. Here's how it looks like in the game:
Image description

Instant Drop

I'm pretty impatient when it comes to Tetris, so if I'm sure that I want the block to drop directly down, I want a way to make it drop instantly instead of waiting for it to slowly make its way down.

With that thought in mind, I implemented a function where if the player taps the DOWN arrow key twice in quick succession, the block falls immediately. I did this by tracking the time delta since the last time the DOWN key was pressed. Using the process function, I can keep track of the time between two DOWN key taps, and if they are within 0.5 seconds, the block will be immediately dropped.

Drop Indicator

After implementing the instant drop, I tested the game, and found that sometimes when I drop the block instantly, it falls to the left or right of where I intended it to fall. This is due to my bad visual perception. Not the game.

In any case, it turns out that identifying which slot at the bottom of the board the block is going to fall into, when the block is at the top of the screen, is a not an easy task. Therefore, to help the players, I implemented a Drop Indicator to show where the block will fall to.
Image description

To implement this, I added a new variable: the indicator piece, which is different from the active piece which is the actual piece the player is controlling. Whenever the active piece moves, the indicator piece will instantiate at the same location, fall to the bottom, and stay there.

With the new Drop Indicator, it became a lot easier to see where my Tetris pieces were falling, and made the game less frustrating as I wouldn't accidentally drop a piece somewhere I didn't intend to.

Software Engineering Principles

During this part of the development, I realised again that I overlooked some Software Engineering principles in my development due to the lack of planning.

  • Don't Repeat Yourself (DRY)
    As I implemented the indicator piece to show the Tetris drop location, I realized that it also uses the drop block function used by the active piece controlled by the player. The only different is that the indicator piece uses it to see where it will drop to, while the active piece uses it when the player actively double taps DOWN to get an instant drop. There are also a few other functions that are used by both pieces, indicating a possibility for a Game Piece class as an abstraction. However, as I did not think of the indicator piece idea until I implemented the instant drop, I had to add some code refactoring and repeat some code to achieve the functionality.

  • God Class
    With more functionalities coming along, the Game script is getting longer and longer, making it difficult to locate specific functions within the script. During this phase of the development, I found myself using Ctrl+F to locate functions at times, simply because of the sheer size of the script file. I did not think a game like Tetris would have so many functions, and would have to take note to separate the game into components in the future.

Next Steps

Up next, I will continue to implement the expected features of the Tetris game!

Top comments (0)