Tetris Games (c + +)

Source: Internet
Author: User

Took a few days to finally put the Russian block finished, I would like to change the black box and then make a good interface, but feel bad to get it first.

#include <iostream> #include <cstdio> #include <windows.h> #include <time.h> #include < conio.h>using namespace std; const int lf_up_x =; bool vis[100][100]; Used to mark coordinates int co[100][100];            The color of the coordinate is void color (int x), void Get_coord (int x, int y), class coord{public:int x;            int y; int Col;}    G[10][10][10]; class t_box{static int score;    static int Rank; Private:int x, y; coordinate int type; type int FG;             The shape of the first FG variation of a graph public:t_box (int sx = x, int sy = 3, int st = 0, int f = 0);             static void staticnum (int num);             void set (int sx = +, int sy = 3, int st = 0, int f = 0);             void Move (int stepa, int stepb);             BOOL Stop ();             void Print_box ();             void Erase_box ();             void Mark ();             void Pause ();             void Next_box (T_box temp);             void Transform ();BOOL Judge ();} ; int t_box::score = 0; int t_box::rank = 0;  T_box::t_box (int sx, int sy, int st, int f) {x = SX;  y = sy;  type = st; FG = f;}    void t_box::staticnum (int num) {score + = num;    Rank = SCORE/10;    Color (5); Get_coord (62, 19);    Cout<<score; Get_coord (62, 21); Cout<<rank;}  void T_box::set (int sx, int sy, int st, int f) {st = rand ()%6;    f = rand ()%4; x = SX; y = sy; type = st; FG = f;}         void T_box::next_box (T_box temp) {for (int i = 0; i < 4; ++i) {int SX = g[temp.type][i][temp.fg].x + 59;        int sy = G[TEMP.TYPE][I][TEMP.FG].Y + 6;        Get_coord (SX, SY);    cout<< "";        } for (int i = 0; i < 4; ++i) {int SX = g[type][i][fg].x + 59;        int sy = G[TYPE][I][FG].Y + 6; Color (G[TYPE][I][FG].        Col);        Get_coord (SX, SY);    cout<< "";  }}void T_box::p rint_box ()//input graphic {for (int i = 0; i < 4; ++i) {int SX = g[type][i][fg].x + x; //The first several forms of the graph/the first several squares/the first several representations of the figure int sy = g[type][i][fg].y + y; Color (G[TYPE][I][FG].  Col);   Color Get_coord (SX, SY);   Coordinates cout<< "";       Graphics}}void T_box::erase_box ()//Erase graphics {for (int i = 0; i < 4; ++i) {int SX = g[type][i][fg].x + x;       int sy = g[type][i][fg].y + y;       Get_coord (SX, SY);    cout<< "";        }}void t_box::move (int stepa, int stepb) {BOOL flag = true;//check whether the boundary is exceeded, default does not out of bounds for (int i = 0; i < 4; ++i) {        int SX = x + g[type][i][fg].x + Stepa;        int sy = y + g[type][i][fg].y + stepb;  if (SX <= | | SX >= | | sy >= | | sy <= 0 | | vis[sx][sy]) {flag = false;        break; }} if (flag) {x + = Stepa; y + = STEPB;}}       BOOL T_box::stop () {for (int i = 0; i < 4; ++i) {int SX = g[type][i][fg].x + x;       int sy = g[type][i][fg].y + y;    if (Vis[sx][sy+1]) return true; } return false;} void T_box::mark () {for (int i = 0; i < 4;       ++i) {int SX = g[type][i][fg].x + x;       int sy = g[type][i][fg].y + y;       Vis[sx][sy] = true; Co[sx][sy] = G[TYPE][I][FG].    Col;        }}bool T_box::judge () {for (int i = 0; i < 4; ++i) {int SX = g[type][i][fg].x + x;        int sy = g[type][i][fg].y + y;    if (Vis[sx][sy]) return false; } return true;    void T_box::transform ()//Remove the previous graphic before outputting the changed graphic {erase_box ();//Erase the previous graphic int temp = FG;    FG = (FG + 1)%4;    if (Judge ()) Print_box ();        else {FG = temp;    Print_box ();    }}void Get_coord (int x, int y) {coord pos; Pos.    x = x; Pos.    y = y; SetConsoleCursorPosition (GetStdHandle (Std_output_handle), POS);} void color (int a) {Setconsoletextattribute (GetStdHandle (Std_output_handle), a);}   void Tt_fram ()//Set Frame {color (5);   Get_coord (30, 1);   cout<< "< Welcome to use >";   Color (10); int x = lf_up_x, y = 3;     Main frame for (int i = 0; i < ++i) {  Get_coord (x, 2);  VIS[X][2] = true;       cout<< "-"; Get_coord (x, 23); VIS[X][23] = true;       cout<< "-";   x + = 2;       } for (int i = 0; i <; ++i) {Get_coord (lf_up_x, y); vis[lf_up_x][y] = true; cout<< "-";      Get_coord (n, y); Vis[71][y] = true;       cout<< "-";      Get_coord (Wuyi, y); Vis[51][y] = true;       cout<< "-";   Y + = 1;       }//Small frame content for (int i = <=; i + = 2) {Get_coord (I, one-by-one); vis[i][11] = true;   cout<< "-";  } color (10);  Get_coord (53, 3);   cout<< "Next Block:";   Color (11); Get_coord (54, 13);   cout<< "Start: Enter key"; Get_coord (54, 15);   cout<< "Pause: T key"; Get_coord (54, 17);   cout<< "Exit: Q Key";  Get_coord (54, 19);   cout<< "Score:"; Get_coord (54, 21);   cout<< "Rank:"; T_box::staticnum (0);} void Mark_init ()//initialize boundary, tag array {for (int j = 2; J <=; ++j) for (int i = 0; I <=; ++i) vis[i][ J = true; for (int j = 23°c; J <=; ++j) for (int i = 0; I <=; ++i) vis[i][j] = true;}  void Set_box ()//set various graphics {for (int i = 0; i < 4; ++i)//1 square {g[0][0][i].x = 0;  G[0][0][I].Y = 0; G[0][0][i].       Col = 14;  g[0][1][i].x = 0;  G[0][1][I].Y = 1; G[0][1][i].       Col = 14;  g[0][2][i].x = 2;  G[0][2][I].Y = 0; G[0][2][i].       Col = 14;  g[0][3][i].x = 2;  G[0][3][I].Y = 1; G[0][3][i].    Col = 14; }//2 vertical bar g[1][0][0].x = 0; G[1][0][0].Y = 0; G[1][0][0].    Col = 3; g[1][1][0].x = 0; G[1][1][0].Y = 1; G[1][1][0].    Col = 3; g[1][2][0].x = 0; G[1][2][0].Y = 2; G[1][2][0].    Col = 3; g[1][3][0].x = 0; G[1][3][0].Y = 3; G[1][3][0].    Col = 3; g[1][0][1].x = 0; G[1][0][1].Y = 0; G[1][0][1].    Col = 3; g[1][1][1].x = 2; G[1][1][1].Y = 0; G[1][1][1].    Col = 3; g[1][2][1].x = 4; G[1][2][1].Y = 0; G[1][2][1].    Col = 3; g[1][3][1].x = 6; G[1][3][1].Y = 0; G[1][3][1].    Col = 3; g[1][0][2].x = 0; G[1][0][2].y = 0; G[1][0][2].    Col = 3; g[1][1][2].x = 0; G[1][1][2].Y = 1; G[1][1][2].    Col = 3; g[1][2][2].x = 0; G[1][2][2].Y = 2; G[1][2][2].    Col = 3; g[1][3][2].x = 0; G[1][3][2].Y = 3; G[1][3][2].    Col = 3; g[1][0][3].x = 0; G[1][0][3].Y = 0; G[1][0][3].    Col = 3; g[1][1][3].x = 2; G[1][1][3].Y = 0; G[1][1][3].    Col = 3; g[1][2][3].x = 4; G[1][2][3].Y = 0; G[1][2][3].    Col = 3; g[1][3][3].x = 6; G[1][3][3].Y = 0; G[1][3][3].    Col = 3; 3 g[2][0][0].x = 0; G[2][0][0].Y = 0; G[2][0][0].    Col = 4; g[2][1][0].x = 0; G[2][1][0].Y = 1; G[2][1][0].    Col = 4; g[2][2][0].x = 2; G[2][2][0].Y = 1; G[2][2][0].    Col = 4; g[2][3][0].x = 2; G[2][3][0].Y = 2; G[2][3][0].    Col = 4; g[2][0][1].x = 0; G[2][0][1].Y = 0; G[2][0][1].    Col = 4; g[2][1][1].x = 2; G[2][1][1].Y = 0; G[2][1][1].    Col = 4; g[2][2][1].x = 0; G[2][2][1].Y = 1; G[2][2][1].    Col = 4; g[2][3][1].x =-2; G[2][3][1].Y = 1; G[2][3][1].    Col = 4; g[2][0][2].x = 0; G[2][0][2]. y = 0; G[2][0][2].    Col = 4; g[2][1][2].x = 0; G[2][1][2].Y = 1; G[2][1][2].    Col = 4; g[2][2][2].x = 2; G[2][2][2].Y = 1; G[2][2][2].    Col = 4; g[2][3][2].x = 2; G[2][3][2].Y = 2; G[2][3][2].    Col = 4; g[2][0][3].x = 0; G[2][0][3].Y = 0; G[2][0][3].    Col = 4; g[2][1][3].x = 2; G[2][1][3].Y = 0; G[2][1][3].    Col = 4; g[2][2][3].x = 0; G[2][2][3].Y = 1; G[2][2][3].    Col = 4; g[2][3][3].x =-2; G[2][3][3].Y = 1; G[2][3][3].    Col = 4; 4 g[3][0][0].x = 0; G[3][0][0].Y = 0; G[3][0][0].    Col = 5; g[3][1][0].x = 0; G[3][1][0].Y = 1; G[3][1][0].    Col = 5; g[3][2][0].x =-2; G[3][2][0].Y = 1; G[3][2][0].    Col = 5; g[3][3][0].x = 2; G[3][3][0].Y = 1; G[3][3][0].    Col = 5; g[3][0][1].x = 0; G[3][0][1].Y = 0; G[3][0][1].    Col = 5; g[3][1][1].x = 0; G[3][1][1].Y = 1; G[3][1][1].    Col = 5; g[3][2][1].x = 2; G[3][2][1].Y = 1; G[3][2][1].    Col = 5; g[3][3][1].x = 0; G[3][3][1].Y = 2; G[3][3][1].    Col = 5; g[3][0][2].x = 0; g[3][0][2].Y = 0; G[3][0][2].    Col = 5; g[3][1][2].x = 2; G[3][1][2].Y = 0; G[3][1][2].    Col = 5; g[3][2][2].x = 2; G[3][2][2].Y = 1; G[3][2][2].    Col = 5; g[3][3][2].x = 4; G[3][3][2].Y = 0; G[3][3][2].    Col = 5; g[3][0][3].x = 0; G[3][0][3].Y = 0; G[3][0][3].    Col = 5; g[3][1][3].x = 0; G[3][1][3].Y = 1; G[3][1][3].    Col = 5; g[3][2][3].x =-2; G[3][2][3].Y = 1; G[3][2][3].    Col = 5; g[3][3][3].x = 0; G[3][3][3].Y = 2; G[3][3][3].    Col = 5; 5 g[4][0][0].x = 0; G[4][0][0].Y = 0; G[4][0][0].    Col = 6; g[4][1][0].x = 0; G[4][1][0].Y = 1; G[4][1][0].    Col = 6; g[4][2][0].x = 0; G[4][2][0].Y = 2; G[4][2][0].    Col = 6; g[4][3][0].x = 2; G[4][3][0].Y = 2; G[4][3][0].    Col = 6; g[4][0][1].x = 0; G[4][0][1].Y = 0; G[4][0][1].    Col = 6; g[4][1][1].x = 0; G[4][1][1].Y = 1; G[4][1][1].    Col = 6; g[4][2][1].x = 2; G[4][2][1].Y = 0; G[4][2][1].    Col = 6; g[4][3][1].x = 4; G[4][3][1].Y = 0; G[4][3][1].    Col = 6; g[4][0][2].x = 0; g[4][0][2].Y = 0; G[4][0][2].    Col = 6; g[4][1][2].x = 2; G[4][1][2].Y = 0; G[4][1][2].    Col = 6; g[4][2][2].x = 2; G[4][2][2].Y = 1; G[4][2][2].    Col = 6; g[4][3][2].x = 2; G[4][3][2].Y = 2; G[4][3][2].    Col = 6; g[4][0][3].x = 0; G[4][0][3].Y = 0; G[4][0][3].    Col = 6; g[4][1][3].x = 0; G[4][1][3].Y = 1; G[4][1][3].    Col = 6; g[4][2][3].x =-2; G[4][2][3].Y = 1; G[4][2][3].    Col = 6; g[4][3][3].x =-4; G[4][3][3].Y = 1; G[4][3][3].    Col = 6; 6 g[5][0][0].x = 0; G[5][0][0].Y = 0; G[5][0][0].    Col = 9; g[5][1][0].x = 0; G[5][1][0].Y = 1; G[5][1][0].    Col = 9; g[5][2][0].x = 0; G[5][2][0].Y = 2; G[5][2][0].    Col = 9; g[5][3][0].x =-2; G[5][3][0].Y = 2; G[5][3][0].    Col = 9; g[5][0][1].x = 0; G[5][0][1].Y = 0; G[5][0][1].    Col = 9; g[5][1][1].x = 0; G[5][1][1].Y = 1; G[5][1][1].    Col = 9; g[5][2][1].x = 2; G[5][2][1].Y = 1; G[5][2][1].    Col = 9; g[5][3][1].x = 4; G[5][3][1].Y = 1; G[5][3][1].    Col = 9; g[5][0][2].x = 0; G[5][0][2].Y = 0; G[5][0][2].    Col = 9; g[5][1][2].x = 2; G[5][1][2].Y = 0; G[5][1][2].    Col = 9; g[5][2][2].x = 0; G[5][2][2].Y = 1; G[5][2][2].    Col = 9; g[5][3][2].x = 0; G[5][3][2].Y = 2; G[5][3][2].    Col = 9; g[5][0][3].x = 0; G[5][0][3].Y = 0; G[5][0][3].    Col = 9; g[5][1][3].x = 2; G[5][1][3].Y = 0; G[5][1][3].    Col = 9; g[5][2][3].x = 4; G[5][2][3].Y = 0; G[5][2][3].    Col = 9; g[5][3][3].x = 4; G[5][3][3].Y = 1; G[5][3][3]. Col = 9;}    void check ()//check whether the {int Incr = 0 can be eliminated;       for (int j = x; j >= 4;--j)//check each line//Y-1 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ * * * * *  Suppose can be eliminated for (int i = <=; i + = 2) if (!vis[i][j]) {flag = false; break          ;          } if (flag)//The current line can be eliminated {incr++;//Add-on operation for (int i = <=; i + = 2)//eliminate the current full line mark first              {Vis[i][j] = false;              Get_coord (i, j);        cout<< "";  }//move the above line down for (int c_j = j-1, C_j >= 5;--c_j) for (int c_i =, c_i <=, c_i + = 2)                  if (Vis[c_i][c_j])//If already marked {Vis[c_i][c_j] = false;                  Vis[c_i][c_j+1] = true;                  CO[C_I][C_J+1] = Co[c_i][c_j];                  Co[c_i][c_j] = 0;                  Get_coord (C_i, C_j);                  cout<< "";                  Color (co[c_i][c_j+1]);                  Get_coord (C_i, c_j+1);              cout<< "";       } j + +; }} t_box::staticnum (incr*2);}    void Exit () {Sleep (20000); Exit (0);}   void T_box::P ause () {Move (0,-1);   Print_box ();       while (1) {Char ch = getch ();   if (ch = =) break;     }}void Tetris ()//main program {Srand (Time (0));//Take the system to//tt_fram ();    Set frame Set_box ();   Set up various graphics T_box CD, Tempa, TEMPB;   The initial value of each falling block is tempb.set ();   Tempb.next_box (Tempa);   for (int i = 0; ;   ++i){if (!) (       i%2)) Tempa.set ();       else Tempb.set ();       if (i%2) Tempb.next_box (TEMPA), cd = Tempa;       else Tempa.next_box (TEMPB), cd = TEMPB; while (1) {cd.print_box ();//Output graphics if (CD.              Stop ()) {color (6);              Get_coord (20, 10);              cout<< "t_t Game over t_t";          Exit ();          } Sleep (350); Cd.erase_box (); Erase the Graphics CD.          Move (0, 1);            if (Kbhit ())//Determine if there is a button pressed, if there is a spin or accelerator key is possible {char ch = getch (); Switch (CH) {case ' a ': CD. Move (-2, 0);    break; Move left one case ' d ': CD.  Move (2, 0);    break; Move right One grid case ' W ': CD. Transform ();                   break; Transform grid case ' s ': CD. Move (0, 2);  break; Sharp drop Box case ' T ': CD. Pause ();                break;  Case ' Q ': Exit ();             break;   Default:break; If none is not executed}} if (CD.       Stop ()) break;       } cd.print_box (); Cd.       Mark (); Check ();    Check whether}}void hidecursor ()//Hide cursor {console_cursor_info Cursor_info = {1, 0} can be eliminated; Setconsolecursorinfo (GetStdHandle (std_output_handle), &cursor_info);}   int main () {hidecursor ();//Hide cursor BOOL flag = TRUE; Memset (Vis, false, sizeof (VIS)); Mark Memset (Co, 0, sizeof (CO));   Used for recording color Mark_init (); Tag perimeter inaccessible tt_fram ();             Set the frame while (1) {if (flag) {while (1) {Char ch = getch ();         if (ch = =) break;         } Tetris ();      Flag = false;   } else Tetris (); } return 0;}


Tetris Games (c + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.