“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
People born in the 1980s and 90s usually play video games like this, which is the memory of our generation. Tetris is probably one of the oldest games we’ve ever played. The post-00s generation has never touched these games. But this little game as a training project is still very good ah. The algorithms and design ideas used in it will also be of some help in the future work.
As usual, no more nonsense, straight to the renderings
Use data tetris structure
First we need to know how many tetris there are in the game. And you need to define these squares with data.
Const int sharp [15] [8] = / / graphics of each coordinates of each point, after the first longitudinal transverse {,0,1,0,2,0,3,0 {0}, {0,0,0,1,0,2,0,3}, / / bar (a tasikmalaya, A vertical),0,1,0,0,1,1,1 {0}, / / square,0,1,0,1,1,1,2 {0}, {0,1,1,1,2,0,2,1},,0,0,1,0,2,1,2 {0}, {0,0,0,1,1,0,2,0}, / / L ,0,0,1,1,1,2,1,0,1,1,1,2,0,2 {1}, {0}, {0,0,0,1,0,2,1,0}, {0,0,1,0,2,0,2,1}, / / T,0,0,1,1,1,1,2 {0}, {0,1,1,0,1,1,2,0}, / / z1 ,0,1,0,1,1,2,1,1,0,2,1,0,1,1 {0}, {0} / / z2};Copy the code
2 Operation tetris rotation
Rotation, that’s just up and down, and it transforms. According to the above rules, a bar can only change into the next bar, a square can’t change, and an L can only change into an L. All we need to do is replace the data structure defined above with an array.
3 game update, whereabouts update
Void Games::Updata() // update function {int I, flag; int nx, ny; for(i = 0; i < 4; i++) { nx = pt[0] + sharp[id][i * 2]; ny = pt[1] + sharp[id][i * 2 + 1]; SetPos((ny + 1) * 2, nx + 1); SetColor(0); Cout < < "s"; map[nx][ny] = 1; } if(pt[0] < top) top = pt[0]; For (I = pt[0]; i < pt[0] + high[id]; I++) // eliminate the line {flag = 1; for(int j = 0; j < 13; If (map[I][j] == 0) flag = 0; if(flag == 1) { for(int z = i; z >= top; z--) { for(int p = 0; p < 13; p++) { map[z][p] = map[z - 1][p]; SetPos((p + 1) * 2, z + 1); If (map[z][p] == 1) cout << "■"; else cout << " "; }}}}} // Erase graphics and draw graphics. If in the process of falling, there is a "■" need to become "". Void Games::Draw(int x, int y, int num) // Draw a graph {int nx, ny; for(int i = 0; i < 4; i++) { nx = x + sharp[num][2 * i]; ny = y + sharp[num][2 * i + 1]; SetPos((ny + 1) * 2, nx + 1); SetColor(i + 1); Cout < < "s"; }} void Games::ClearDraw(int x, int y, int num) {int nx, ny; for(int i = 0; i < 4; i++) { nx = x + sharp[num][2 * i]; ny = y + sharp[num][2 * i + 1]; SetPos((ny + 1) * 2, nx + 1); cout << " "; }}Copy the code
It’s actually a very simple principle, using array control graphics to draw, and erase. Then determine if the row is full to eliminate the score. And then you have to decide if the grid is full, and you have to make a game-over judgment.
If you want to get started on the front end of the game, you can start with a stand-alone game. There is a need to take the complete source code, please move to the public number: like the code poem
Now that I’m in, it’s not easy to try. Let’s go with a “like”.