C + + Tetris game without graphics library of Tetris _c language

Source: Internet
Author: User
Tags rand sleep

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

#include <stdio.h> #include <stdlib.h> #include <windows.h> #include <time.h> #include <
conio.h> #define MOD #define SIZE_N #define SIZE_M int cur_x,cur_y;
 
int score,mark,next,map[size_n][size_m],gamespeed=300;  int shape[28][6]={{0,-1,0,-2,1,0}, {0,1,1,0,2,0}, { -1,0,0,1,0,2}, {0,-1,-1,0,-2,0}, {0,-1,0,1,-1,0}, {0,1,1,0,-1,0},  {1,0,0,-1,0,1}, {1,0,-1,0,0,-1}, { -1,1,0,1,1,0}, {0,-1,1,0,1,1}, { -1,0,0,-1,1,-1}, { -1,-1,-1,0,0,1}, { -1,0,0,1,1,1}, {0,1,1,-1,1,0}, { -1,0,0,1,1,1}, {0,1,1,-1,1,0}, { -1,0,0,-1,0,-2}, { -1,0,-2,0,0,1}, {0,1,0,2,1,0}, {0,-1,1,0,2,0}, {0, 1,1,0,1,1}, {0,-1,1,0,1,-1}, { -1,0,0,-1,-1,-1}, { -1,0,-1,1,0,1}, {0,1,0,2,0,3}, {1,0,2,0,3,0}, {0,-1,0,-2,0,-3}, {-
 
1,0,-2,0,-3,0}};
  void Gotoxy (int x,int y) {COORD C; C.x=x-1;
  C.y=y-1;
SetConsoleCursorPosition (GetStdHandle (Std_output_handle), c);
  } void Gameover () {int i,j,flag=0;
    for (j=1;j<size_m-1;j++) {if (map[1][j]!=0) {flag=1;break; } if (fLag==1) {for (i=1;i<size_n-1;i++) {gotoxy (2,i+1);
      for (j=1;j<size_m-1;j++) {printf ("-");
    }puts ("");
    } gotoxy (7,9);
    printf ("GAME over!");
    Gotoxy (1,size_n+1);
  Exit (0);
  } void Showmap (int id) {int i,j;
  Gotoxy (1,1); if (id!=-1) {for (i=0;i<size_n;i++) {for (j=0;j<size_m;j++) {if (i==0&&j==0 | | i==0&&j ==size_m-1 | | j==0&&i==size_n-1 | |
        j==size_m-1&&i==size_n-1) printf ("");
        else if (i==0 | | i==size_n-1) printf ("--");
        else if (j==0 | | j==size_m-1) printf ("|");
        else if (map[i][j]==2) printf ("");
          else if (i==cur_x+shape[id][0] && j==cur_y+shape[id][1] | |
          I==CUR_X+SHAPE[ID][2] && J==cur_y+shape[id][3] | |
          I==CUR_X+SHAPE[ID][4] && J==cur_y+shape[id][5] | |
        i==cur_x && j==cur_y) printf ("");  
      else if (map[i][j]==0) printf ("");
      } if (i==1) printf ("Next:"); if (i==11) printf ("Equal parts:%d", score);
      if (i==14) printf ("Speed:%d", score/100+1);
    Puts ("");
    } else {mark=1; for (i=0;i<size_n;i++) {for (j=0;j<size_m;j++) {if i==0&&j==0 | | i==0&&j==size_m-1 | | j= =0&&i==size_n-1 | |
        j==size_m-1&&i==size_n-1) printf ("");
        else if (i==0 | | i==size_n-1) printf ("--");
        else if (j==0 | | j==size_m-1) printf ("|");
        else if (map[i][j]==2) printf ("");  
      else if (map[i][j]==0) printf ("");
      } if (i==1) printf ("Next:");
      if (i==11) printf ("Score:%d", score);
      if (i==14) printf ("Speed:%d", score/100+1);
    Puts (""); } gotoxy (30,6);
  printf ("");
  for (i=0;i<6;i=i+2) {gotoxy (30+2*shape[id][i+1],6+shape[id][i]); printf (""); } gotoxy (30,6);
  printf ("");
  for (i=0;i<6;i=i+2) {gotoxy (30+2*shape[next][i+1],6+shape[next][i]); printf ("");
Sleep (gamespeed);
  } void init (int id) {int i,j;
  memset (map,0,sizeof (map)); FoR (i=0;i<size_n;i++) {for (j=0;j<size_m;j++) if (i==size_n-1 | | j==0 | | j==size_m-1) map[i][j]=-1; } cur_x=0;
  cur_y=5;
Showmap (ID);
  int judge_in (int x,int y,int ID) {int i;
  if (map[x][y]!=0) return 0;
  for (i=0;i<6;i=i+2) {if (map[x+shape[id][i] [y+shape[id][i+1]]!=0) return 0;
}return 1;
  } void Fun_score () {int i,j,ii,jj;
    for (i=1;i<size_n-1;i++) {int flag=0;
    for (j=1;j<size_m-1;j++) {if (map[i][j]!=2) {flag=1;break}
      } if (flag==0) {int k=3;
        while (k--) {gotoxy (2,i+1);
            for (ii=1;ii<size_m-1;ii++) {if (map[i][ii]==2) {if (k%2==1) printf ("");
          else printf ("");
      }}sleep (100);
      for (ii=i;ii>1;ii--) {for (jj=1;jj<size_m-1;jj++) MAP[II][JJ]=MAP[II-1][JJ];
      } showmap (-1);
      score+=10;
    if (score%100==0 && score!=0) gamespeed-=50;
   
  int main () {int i,id,set=1; Srand (Time (N)ull)); Id=rand ()%mod;
  Id= (id+mod)%mod; Next=rand ()%mod;
   
  next= (next+mod)%mod;
   
  init (ID);
    while (1) {here:mark=0;
      if (set==0) {id=next; Next=rand ()%mod;
      next= (next+mod)%mod;
      cur_x=0;cur_y=5;
    Set=1;
      while (!kbhit ()) {Gameover ();
      if (judge_in (cur_x+1,cur_y,id) ==1) cur_x++;
        else {map[cur_x][cur_y]=2;
        for (i=0;i<6;i=i+2) map[cur_x+shape[id][i] [cur_y+shape[id][i+1]]=2;
        Fun_score ();
      set=0;
      } if (mark!=1) Showmap (ID);
    goto here;
    } char key;
     
    Key=getch ();
      if (key==72) {int tmp=id;
      id++;
      if (id%4==0 && id!=0) id=id-4;
    if (judge_in (cur_x,cur_y,id)!=1) id=tmp;
    else if (key==80 && judge_in (cur_x+1,cur_y,id) ==1) cur_x++;
    else if (key==75 && judge_in (cur_x,cur_y-1,id) ==1) cur_y--;
    else if (key==77 && judge_in (cur_x,cur_y+1,id) ==1) cur_y++; else if (key==27) {Gotoxy (1,sizE_N+1); exit (0);}
  } getch (); 
return 0;
 }

The above is the entire content of this article, I hope that you learn C + + program to help.

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.