Step by Step with Score and Game Over and Video tutorial
Tetris (Russian: Тетрис)[a] is a multimedia franchise originating from a puzzle video game created by Soviet software engineer Alexey Pajitnov in 1984.
Video games with Tetris’ core mechanics have been published by several companies for multiple platforms, most prominently during a dispute over the appropriation of the rights in the late 1980s. After a significant period of publication by Nintendo, the rights reverted to Pajitnov in 1996, who co-founded the Tetris Company with Henk Rogers to manage licensing. The franchise has since expanded into film, television, books, and music singles.
In this article we will create Tetris with C++ and SFML adding score and game over .
At the end of the article there is a video and also the link to the source code on GitHub .
01. DRAW THE SQUARES ON THE SCREEN
Basic start files:
tetris.hpptetris.cppmain.cppMakefile
Compile and run:
02. DRAW SHAPES ON THE SCREEN
Add to tetris.hpp(private and first of all)
Add to tetris.hpp the member function: void moveToDown();
Shapes:
Add tetris.cpp to the Constructor:
Add to tetris.hpp the member function void Tetris::moveToDown() to tetris.cpp:
Add to tetris.cpp the void Tetris::draw() member function:
Add to tetris.cpp the void Tetris::run() member function:
03. ROTATE AND MOVE TO THE SIDE
Add to tetris.hpp:
Add tetris.cpp to the constructor:
Add tetris.cpp to the events() loop
Add tetris.cpp contents of the 3 created member functions:
Change the run() member function into tetris.cpp and add the new created member functions in that order:
TO MOVE YOU NEED TO CHANGE moveToDown() and add the condition if( z[0].x == 0 ){} and put the LOOP inside
COMPILE AND DISPLAY ROTATION AND SIDES MOVEMENT!
04. MOVE DOWN
Add to tetris.hpp:
Adicionar ao construtor:
Adicionar à void Tetris::events() antes de auto e = ...:
Adicionar à void Tetris::moveToDown() antes do std::uint32_t number = {3}:
Add to builder:
Add to void Tetris::events() before auto and = ...:
Add to void Tetris::moveToDown() before std::uint32_t number = {3}:
05. LIMIT THE PARTS TO THE FLOOR AND FIT ONE ON TOP OF THE OTHER
Add to tetris.hpp:
Add to bool Tetris::maxLimit():
Add the seeder to main.cpp:
Change changePosition() to store the value of the moving shape(z[i]) to fix it(k[i]) and check with maxLimit():
Copy the condition from if( maxLimit() )... and paste inside the if( rotate )..., but outside the for loop in void Tetris::setRotate()
Add to int colortetris.hpp and initialize it to 1: color = {1};
Same thing also in moveToDown(), but creating random pieces and changing the position of the number line into the if( maxLimit() ) condition, REMOVE CONDITION: if( z[0 ].x == 0 ){ ... :
IMPORTANT: create the loop to set the color to a fixed position and make it random;
Add loop in void Tetris::draw():
Add to events() outside the loop:
TO MAKE THE FIRST PIECE ALSO RANDOM, add to the constructor:
Copy from moveToDown()
06. ADD RANDOM COLORS
Add sprite->setTextureRect(sf::IntRect(color * 36, 0, 36, 36)); to the squares loop in draw()
Note that the fixed part will also change color, so to solve this, just add sprite->setTextureRect(sf::IntRect(area[i][j] * 36, 0, 36, 36));(COPY the already existing and modify) to the lines cols loop:
07. ADD SCORE
Create a new member function in tetris.hpp also create score and add as zero to the constructor:
Add sf::Texture tiles, bg; and std::shared_ptr<sf::Sprite> sprite, background;
Constructor:
draw() → window->draw(*background);
09. ADD SCORE AND GAMEOVER
Add to tetris.hpp
Add to tetris.cpp
Tetris::draw():
Tetris::run()
Tetris::setScore:
WATCH THE VIDEO
The video is in Portuguese, but you can follow the steps even if you don’t understand the language. You can also enable subtitles and use Youtube’s automatic translation.