In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to achieve the game of Tetris on C++". In the daily operation, I believe many people have doubts about how to realize the game of Tetris on C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to realize the game of Tetris by C++". Next, please follow the editor to study!
Tetris C++
1. Create a project
two。 A total of two files need to be created, one main.cpp and one elsfk2.h. The compiler I use is vs2019.
3. Create a folder image under the source folder of the project
4. Rename the following two pictures to put them in the folder you just created
Elsfk.jpg
Elsfk_block.jpg
5. Copy the following code to the corresponding file
-the following is the contents of the main.cpp file # include "elsfk2.h" int main () {srand ((int) time (0)) / / create the game window RenderWindow window (VideoMode (COL*18/*+100*/, ROW*18-36), "ELSFK"); / / add the game background Texture T1 image/elsfk_block.jpg T2; t1.loadFromFile ("image/elsfk.jpg"); t2.loadFromFile ("image/elsfk_block.jpg"); Sprite sprite_Bg (T1); Sprite sprite_block (T2); NewBlock () Clock begin; float time1 = 0, time2 = 0; while (window.isOpen ()) {time2 = begin.getElapsedTime () .asSeconds (); begin.restart (); time1 + = time2; / / waiting for the user to press keyEvent (& window) If (time1 > delay) {time1 = 0; blockDrop ();} delay = SPEED_NOM; window.draw (sprite_Bg); drawBlock (& sprite_block, & window) Window.display () }}-the following is the elsfk2.cpp#pragma once#include / / image Processing header file / / # include # includeusing namespace sf # define ROW 22 / / Row # define COL 10 / column # define SPEED_NOM 0.5 / / interval between unaccelerated movements # define SPEED_QIK 0.05 / / the interval between accelerating motion int map [ROW] [COL] = {0} / / Game area size int blocktype; / / Square type int Delete=0; / / number of rows deleted float delay = SPEED_NOM; / / interval void Move_x (int) Void Rotate (); void ClearBlock (); void NewBlock (); void blockDrop (); void keyEvent (RenderWindow*); void drawBlock (Sprite*, RenderWindow*); bool check (); / / A two-dimensional array of 7 squares int a [7] [4] = {1, 3, 5, 7, 2, 4, 4, 5, 3, 4, 5, 3, 5, 4, 4, 5, 5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 2, 3, 5, 5, 5, 7, 2, 3, 4, 4, 5, 2, 5, 5, 4, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 2, 5, 5, 5, 7,} / / the structure of the point struct point {int x, y;}; / / the current square point block [4]; / / the backup point bakblock of the square [4]; / / the processing key void keyEvent (RenderWindow * w) {Event e; bool rotate = 0; int x = 0 While (w-> pollEvent (e)) {if (e.type = = Event::Closed) {w-> close () } if (e.type = = Event::KeyPressed) {switch (e.key.code) {case Keyboard::Up: rotate = 1; break Case Keyboard::Left: X =-1; break; case Keyboard::Right: X = 1; break Default: break;}} if (Keyboard::isKeyPressed (Keyboard::Down)) {delay = SPEED_QIK } if (x) {Move_x (x);} if (rotate) {Rotate ();} / / eliminate the completed line void ClearBlock () {int k = ROW-1 For (int I = ROW-1; I > 0; I Murray -) {int count = 0; for (int j = 0; j)
< COL; j++) { if (map[i][j]) { count++; } map[k][j] = map[i][j]; } if (count < COL) { k--; } }}//检查移动合理性bool check() { for (int i = 0; i < 4; i++){ if( block[i].x=COL||block[i].y>= ROW | | Map [block.y] [block.x]) {return 0;}} return 1;} / / Square landing void blockDrop () {for (block I = 0; I)
< 4; i++) { bakblock[i] = block[i]; block[i].y++; } if (!check()) { for (int j = 0; j < 4; j++){ map[bakblock[j].y][bakblock[j].x] = blocktype; } NewBlock(); ClearBlock(); }}//左右移动void Move_x(int x) { for (int i = 0; i < 4; i++){ bakblock[i] = block[i]; block[i].x += x; } if (!check()) { for (int i = 0; i < 4; i++) { block[i] = bakblock[i]; } }}//旋转****************************** importantvoid Rotate() { if (blocktype == 7) { return; } point p = block[1]; for (int i = 0; i < 4; i++) { bakblock[i] = block[i]; block[i].x = p.x - bakblock[i].y + p.y; block[i].y = bakblock[i].x - p.x + p.y; } if (!check()) { for (int i = 0; i < 4; i++) { block[i] = bakblock[i]; } }}//生成方块void NewBlock() { blocktype = 1 + rand() % 7; for (int i = 0; i < 4; i++) { block[i].x = a[blocktype-1][i] % 2+4; block[i].y = a[blocktype-1][i] / 2; }}//绘制方块void drawBlock(Sprite *b, RenderWindow *w) { //完成的方块 for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (map[i][j] != 0) { b->SetTextureRect (IntRect (Mapi] [j] * 18j0pc18)); b-> setPosition (j * 18, I * 18); b-> move (0,-36); w-> draw (* b) } for (int j = 0; j)
< 4; j++) { b->SetTextureRect (IntRect (blocktype * 18,0,18,18); b-> setPosition (block.x * 18, block.y * 18); b-> move (0,-36); w-> draw (* b);} so far, the study of "how C++ implements the game of Tetris" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.