Feel free to download the project:
In the course Overthrowing Game Engine Tyranny – Games from Scratch with C++ I learned to use C++ to create a small 2D game engine with the help of SFML. With this knowledge I created a Tetris clone. You can download the code including the game here:
The engine is based on what I have created with the help of the teacher André Braun. I changed the physics system since it was based on a 2D action adventure and thus unusable for the steady pace of ticks found in a Tetris game.
To make the detection of the rows which need to be deleted easier, I used a matrix which represents the map of the game. When one row is full each block in it is deleted and the blocks above are set to that row.
Each brick (which is a part of a block) is its own entity, so the blocks do not need to be sliced. Spawning the blocks is done in a way in which a matrix describes the positions of the bricks. These are then added individually to the game.
Each brick has a physics component which allows the tick of the physics system to update the position of the bricks. When the block reaches its destination, the component gets destroyed.
Since only the moving bricks have a physics component, this is used to identify them for the rotation. The rotation is done by rotating the spawn matrix and saving the number of times the rotation has taken place.