Using C + + to implement Tetris _c language under Linux

Source: Internet
Author: User
Tags rand

This example for everyone to share the C + + implementation of Tetris game code, for your reference, the specific content as follows

1.block.c

#include <stdio.h> #include <termios.h> #include <unistd.h> #include <stdlib.h> #include < setjmp.h> #include <sys/time.h> #include <string.h> #include "block.h"//init for globle void INIT_FOR_GL   Oble (void) {x = X/2-2;
  The diamond appear postion flag_erase = 1;  Srand (Getpid ());  Srand num = rand ()% 7; Random appear-diamond = rand ()% 4;  Random appear-A-diamond-mode color = rand ()% 7 + 41;
  Random the diamond color next_num = rand ()% 7;
  Next_mode = rand ()% 4;
 
  Save_color = rand ()% 7 + 41;  Print_start_interface ();   Print game Start interface//Print_score ();   Print INIT score 0//Print_level ();
  Print init level 1}//print start interface void Print_start_interface (void) {int x, y;
  printf ("\33[2j");
  printf ("\33[%d;%dh\33[32m Score: \33[0m", P_y + ten, p_x + 25);
  printf ("\33[%d;%dh\33[32m Grade: \33[0m", P_y +, p_x + 25); for (x = p_x, y = p_y x <= 46;
  x + +) printf ("\33[%d;%dh\33[41m==\33[0m", y, X); for (x = p_x, y = p_y + 1; y <= y++) printf ("\33[%d;%dh\33[41m| |
  \33[0m ", y, X); for (x = p_x +, y = p_y + 1; y <= y++) printf ("\33[%d;%dh\33[41m| |
  \33[0m ", y, X); for (x = p_x + +, y = p_y + 1; y <= y++) printf ("\33[%d;%dh\33[41m| |
  \33[0m ", y, X);
  for (x = p_x +, y = p_y + 8; x <= + +) printf ("\33[%d;%dh\33[41m--\33[0m", y, X);
  for (x = p_x, y = p_y +; x <= + x + +) printf ("\33[%d;%dh\33[41m==\33[0m", y, X);
  printf ("\33[?25l");
Fflush (stdout);
  //erase last diamonds void Erase_last (void) {int J, x1, Y1, N;
  X1 = save_x + p_x + 2; for (j = 0, n = 0; J < + j) {if (J/4 >= shape[num][save_mode][16] && J% 4 = 0) {y1 = sav
      E_y + p_y + 1 + N;
      printf ("\33[%d;%dh", Y1, X1);
    n++; } if (J/4 >= shape[num][save_mode][16] && J% 4 >= shape[num][save_mode][17]) {if (shape[ Num][save_mode][J] = = 0) {printf ("\33[2c");
      } if (shape[num][save_mode][j] = = 1) {printf ("");
}} fflush (stdout);
  }//print Modes shape void Print_mode_shape (void) {int J, x1, Y1, N;
  int left_flag = 0;
  if (flag_erase = = 0) {erase_last ();
  } x1 = x + p_x + 2; for (j = 0, n = 0; J < + j) {if (J/4 >= shape[num][mode][16] && J% 4 = 0) {y1 = y + p_y
      + 1 + N;
      printf ("\33[%d;%dh", Y1, X1);
    n++; } if (J/4 >= shape[num][mode][16] && J% 4 >= shape[num][mode][17]) {if (Shape[num][mode)
      [j] = = 0) {printf ("\33[2c");
      } if (shape[num][mode][j] = = 1) {printf ("\33[%dm[]\33[0m", color);
  } fflush (stdout);
  printf ("\33[0m");
  Fflush (stdout);
  save_x = x;
  save_y = y;
  Save_mode = mode;
  Save_row = 4-shape[num][mode][16];
  Save_col = 4-shape[num][mode][17];
 
flag_erase = 0; }//store diamonds to matrix by color to flag void Store_flag_color (void) {int I, a = 0, b = 0;
      for (i = 0; i < i++) {if (I/4 >= shape[num][mode][16] && i% 4 = 0) {a++;
    b = 0; } if (I/4 >= shape[num][mode][16] && i% 4 >= shape[num][mode][17]) {if (shape[num][save_
      Mode][i] = = 0) {b = b + 2;
        } if (shape[num][save_mode][i] = = 1) {matirx[save_y + a-1][save_x + b] = color;
        b++;
        Matirx[save_y + a-1][save_x + b] = color;
      b++;
  //print the Save matrix Void Print_save_matrix (void) {int I, j, n = 0;
    for (i = 0; i < Y; i++) {printf ("\33[%d;%dh", i + p_y + 1, p_x + 2);
        for (j = 0; J < X; J +) {if (Matirx[i][j]!= 0) {n = (n + 1)% 2;
        fprintf (stdout, "\33[%DM", Matirx[i][j]); (n = = 1)?
      printf ("["): printf ("]");
        } if (matirx[i][j] = = 0) {printf ("\33[0m");
      printf (""); } fflush (StdoUT);
  }}//Change shape void Change_shape (void) {int i, n;
    for (i = 0; i < Save_row; i++) {if (num = = 6) {n = 4;
    else {n = 0;
      if ((x + N) >= X-save_col * 2 && save_col < Save_row) | |
    Judge_by_color (x, (mode + 1)% 4) = = 1) {return;
  } mode = (mode + 1)% 4;
  Fflush (stdout);
  Print_mode_shape ();
Fflush (stdout);
  }//move right void Move_right (void) {int i;
  if (x >= x-save_col * 2 | | | judge_by_color (x + 2, mode) = = 1) {return;
  x = x + 2;
  Print_mode_shape ();
Fflush (stdout);
  }//Move Left void Move_left (void) {int i;
  if (x <= 0 | | | judge_by_color (x-2, mode) = = 1) {return;
  } x = X-2;
  Print_mode_shape ();
Fflush (stdout);
  }//Move Down void Move_down () {y++;
    if (y >= y-save_row + 1 | | | judge_by_color (x, mode) = = 1) {Store_flag_color ();
    Game_over ();
    y = 0;
    Save_row = 0;
    Save_col = 0;
 
    x = X/2-2; num = nExt_num;
    mode = Next_mode;
    color = Save_color;
    Next_num = random ()% 7;
    Next_mode = random ()% 4;
    Save_color = random ()% 7 + 41;
    Print_next ();
    Flag_erase = 1;
    Destroy_line ();
    Fflush (stdout);
  Return
  } print_mode_shape ();
Fflush (stdout);
    } void Fall_down () {while (1) {y++;
      if (y >= y-save_row + 1 | | | judge_by_color (x, mode) = = 1) {Store_flag_color ();
      Game_over ();
      y = 0;
      Save_row = 0;
      Save_col = 0;
 
      x = X/2-2;
      num = Next_num;
      mode = Next_mode;
      color = Save_color;
      Next_num = rand ()% 7;
      Next_mode = rand ()% 4;
      Save_color = rand ()% 7 + 41;
      Print_next ();
      Flag_erase = 1;
      Destroy_line ();
      Fflush (stdout);
    Return
    } print_mode_shape ();
  Fflush (stdout);
  }//erase Next tip diamond void Erase_next (void) {int I, j, n = 0;
 for (i = 0; i < 4; i++) {printf ("\33[%d;%dh", p_y + 3 + N, p_x + x + 7);   n++;
    for (j = 0; J < 4; J +) {printf ("");
  } printf ("\33[30;4h\33[?25l");
Fflush (stdout);
  }//print Next tip diamond void Print_next (void) {int J, n = 0;
  Erase_next (); for (j = 0; J < + j) {if (J/4 >= shape[next_num][next_mode][16] && J% 4 = 0) {printf ("\3
      3[%d;%dh ", p_y + 3 + N, p_x + x + 7);
    n++;
      } if (J/4 >= shape[next_num][next_mode][16] && J% 4 >= shape[next_num][next_mode][17]) {
      if (shape[next_num][next_mode] = = 0) {printf ("\33[2c");
      } if (shape[next_num][next_mode][j] = = 1) {printf ("\33[%dm[]\33[0m", Save_color);  }}//print scores info void Print_score (void) {printf ("\33[%d;%dh\33[31m%d\33[0m", P_y +, p_x + x + 10)
  Score);
 
fprintf (stdout, "\33[%d;0h", p_y + 20 + 2);}
  Print grades info void Print_level (void) {printf ("\33[%d;%dh\33[31m%d\33[0m", P_y +, p_x + x +, level); fprintf (stdout, "\33[%d;0H ", p_y + 20 + 2);
  }//destroy A or lines void destroy_line (void) {int I, j, full;
  int A, b, C;
    for (i = 0; i < Y; i++) {full = 1;
      for (j = 0; J < X; J +) {if (matirx[i][j] = = 0) {full = 0; } if (full = = 1) {for (a = 0; a < i; a++) {for (b = 0; b < X; b++) {matirx[i-
        A][B] = matirx[i-a-1][b];
      } Print_save_matrix ();
      Score = score + 100;
        If (score% Level_score = = 0) {level = level + 1;
        if (level >= 9) level = 9;
        Change_level ();
 
      Print_level ();
    } print_score ();  rate void Change_level (void) {switch (level) {case 1:setitimer (itimer_real,
    &AMP;LEVEL_01, NULL);
  Break
    Case 2:setitimer (Itimer_real, &level_02, NULL);
  Break
    Case 3:setitimer (Itimer_real, &level_03, NULL);
  Break Case 4:setitimer (Itimer_real, &Level_04, NULL);
  Break
    Case 5:setitimer (Itimer_real, &level_05, NULL);
  Break
    Case 6:setitimer (Itimer_real, &level_06, NULL);
  Break
    Case 7:setitimer (Itimer_real, &level_07, NULL);
  Break
    Case 8:setitimer (Itimer_real, &level_08, NULL);
  Break
    Case 9:setitimer (Itimer_real, &level_09, NULL);
  Break
  Default:break;
  }//by The color to judge whether went across or not int judge_by_color (int x, int mode) {int I, a = 0, b = 0;
      for (i = 0; i < i++) {if (I/4 >= shape[num][mode][16] && i% 4 = 0) {a++;
    b = 0; } if (I/4 >= shape[num][mode][16] && i% 4 >= shape[num][mode][17]) {if (Shape[num][mode)
      [i] = = 0) {b = b + 2;
        } if (shape[num][mode][i] = = 1) {if (matirx[a + y-1][b + x]!= 0) {return 1;
      else B = b + 2; }}//control the Diamonds shape BY the key void Key_control (void) {int ch, flag = 1;
 
  struct Termios save, raw;
  Tcgetattr (0, &save);
  Cfmakeraw (&raw);
  Tcsetattr (0, 0, &raw);
      if (setjmp (env) = = 0) {while (flag) {ch = GetChar ();
      if (ch = = ' \ r ') {Fall_down ();
        } if (ch = = ' \33 ') {ch = GetChar ();
          if (ch = = ' [') {ch = GetChar ();
            Switch (CH) {case ' A ': Change_shape ();
          Break
            Case ' B ': Move_down ();
          Break
            Case ' C ': move_right ();
          Break
            Case ' D ': Move_left ();
          Break
      }} if (ch = = ' Q ' | | | ch = = ' Q ') {flag = 0;
    printf ("\33[%d;%dh\33[31m-----game interrupt Exit!-----\33[0m", p_y + y + 3, p_x);
  printf ("\33[%d;0h\33[?25h", p_y + y + 4);
} tcsetattr (0, 0, &save); }//reach the top line, the game are over void gAme_over (void) {int i; for (i = 0; i < X; i++) {if (Matirx[1][i]!= 0) {printf ("\33[31m\33[%d;%dh-------Game Over!-------
      -\33[0m ", p_y + y + 3, p_x);
      printf ("\33[0m\33[?25h\33[%d;0h", p_y + y + 4);
    LONGJMP (env, 2);
 }
  }
}

2.block.h

#ifndef _block_h_ #define _BLOCK_H_ #define P_X//init postion row;
#define P_Y 5//init postion col; #define X//game_window_size #define Y #define LEVEL_SCORE//need scores to upgrade Jmp_buf E
Nv                   static int x, y;                  Current diamonds postion static int flag_erase;        Erase flag static int num, mode, next_num, Next_mode; Current and next diamonds static int Save_row, Save_col, save_x, save_y, save_mode;//Save coordinate, save graph STA           tic int color, save_color, Flag_color;             Save the color of the next diamonds static int matirx[y][x] = {0};                   Save Diamonds ' Matrix static int level = 1;           Game levels static int score = 0;
Game scores typedef struct ITIMERVAL level;
Static level level_00 = {0, 0}, {0, 0}};
Static level level_01 = {0, 800000}, {1, 0}};
Static level level_02 = {0, 500000}, {0, 500000}}; Static level LEvel_03 = {0, 400000}, {0, 300000}};
Static level level_04 = {0, 300000}, {0, 300000}};
Static level level_05 = {0, 200000}, {0, 300000}};
Static level level_06 = {0, 150000}, {0, 300000}};
Static level level_07 = {0, 100000}, {0, 300000}};
Static level level_08 = {0, 80000}, {0, 300000}};
 
Static level level_09 = {0, 60000}, {0, 300000}}; Three-dimensional for saving diamonds and Diamonds ' shape://first-dimensional for kind of-diamonds-shape//second-dime
               Nsional for alterable ' s mode//third-dimensional for reality value of row and col static const int shape[7][4][18] = {  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 2, 1},//{0, 0, 0, 0, 0, 0, 1, 0, 0,   0, 1, 1, 0, 0, 1, 0, 1, 2},//[] [] [][][] [] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0,        2, 1},//[][][] [[] [] [[] {0 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 2}},//[]
 
              [] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 2, 1},//{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,  1, 1, 1, 2},//[] [] [[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 2, 1},//[]
 
               [][][] [] {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 2}},//[][][] [[] [] []  {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1//{1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, 1},//[[] [] {0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 2},//[
 
               ] [[] [][][] [] [] {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0}, 1, 2, 1}},//[][][] [] [] []  {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 2},//{0, 0, 0, 0, 0, 0, 0, 0, 0,    0, 1, 1, 0, 1, 1, 0, 2, 1},//[] {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 2},//[]
                [][]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 2, 1}},//[[] [] [] {0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
                1, 1, 0, 0, 1, 0, 1, 2},/{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 2, 1},//[] {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 2},//[[] [] [] {0, 0, 0, 0, 0, 0, 0, 0  , 0, 1, 1, 0, 0, 0, 1, 1, 2, 1}},//[[] [] [] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2},//{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2},//{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2},//[[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 2, 2} },//[[] {{0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3},//[] {0, 0, 0  , 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 0},//[] [][][][] {0, 0, 0, 1, 0, 0, 0, 1 0, 0, 0, 1, 0, 3},//[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 0}}//[]};
void init_for_globle (void);
void Print_start_interface (void);
void Print_mode_shape (void);
void Print_save_matrix (void);
void Change_shape (void);
void Move_left (void);
void Move_right (void);
void Move_down ();
void Fall_down ();
void Store_flag_color (void);
void Key_control (void);
void Erase_last (void);
void Destroy_line (void);
void Print_next (void);
void Erase_next (void);
void Change_level (void);
void Print_score (void);
void Print_level (void);
int judge_by_color (int x, int mode);
 
void Game_over (void);
 #endif

3.main.c

#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <setjmp.h >
#include "block.h"
 
int
main (int argc, char **argv)
{
  init_for_globle ();//init for globle
  Print_mode_shape ();//print-Diamond 
  Print_next ();    Print Next diamond
  Setitimer (itimer_real, &level_01, NULL);  Init one leve interval 800ms
  signal (SIGALRM, move_down);//diamond down base on the interval time
  Key_control ();   Using Zhe key to play games return
 
  0;
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.