C language to achieve tetris small game

  • 2020-05-27 06:39:50
  • OfStack

C language to achieve tetris small game production code, the specific content is as follows


#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
 
 
#define TTY_PATH "/dev/tty" 
#define STTY_ON "stty raw -echo -F" 
#define STTY_OFF "stty -raw echo -F" 
 
int map[21][14]; 
char direct; 
 
int node[7][4][16]={ 
 {{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},// A rectangle  
 {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}, 
 {0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}, 
 {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}}, 
 {{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},// A square  
 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}, 
 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}, 
 {1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}}, 
 {{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},//3 Edge to add 1 midpoint  
 {0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}, 
 {0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0}, 
 {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}}, 
 {{0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0},// Type right hoe  
 {0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0}, 
 {0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0}, 
 {1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0}}, 
 {{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},// Left hoe type  
 {0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}, 
 {0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0}, 
 {0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0}}, 
 {{0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0},// Right meander  
 {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}, 
 {0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0}, 
 {0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0}}, 
 {{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},// Left meander  
 {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}, 
 {0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}, 
 {1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0}} 
 }; 
 
typedef struct block 
{ 
 int x; 
 int y; 
 int blockType; 
 int blockDirect; 
}Block; 
Block bl; 
 
void init_map()// Initialize border  
{ 
 int i,j; 
 for(i=0; i<21; i++) 
 for(j=0; j<14; j++) 
 { 
 if(j==0 || j==13) 
 map[i][j] = 200; 
 else if(i==20) 
 map[i][j] = 201; 
 else 
 map[i][j] = 0; 
 } 
} 
void new_block()// Generate a random tetris  
{ 
 int blockType = rand()%7; 
 int blockDirect = rand()%4; 
 int x = 1; 
 int y = 5; 
 bl.x = x; 
 bl.y = y; 
 bl.blockType = blockType; 
 bl.blockDirect = blockDirect; 
} 
 
void input()// Will move the tetris, into the map for marking  
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 { 
 map[bl.x+i][bl.y+j] = 1; 
 } 
} 
void output()// When moving, clear the previous tetris information on the map.  
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 { 
 map[bl.x+i][bl.y+j] = 0; 
 } 
} 
 
void change()// The Russian grid is fused and fixed after the collision  
{ 
 int i, j; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 { 
 map[bl.x+i][bl.y+j] = 10; 
 } 
 for(j=1; j<13; j++) 
 if(map[5][j] == 10) 
 { 
 system("clear"); 
 printf("game over !!!!!!!!!\n"); 
 exit(1); 
 } 
} 
 
 
void print_map()// Print the map and display the information  
{ 
 int i,j; 
 for(i=5; i<21; i++) 
 { 
 for(j=0; j<14; j++) 
 { 
 if(map[i][j]==200)// Left and right boundaries  
 printf("#"); 
 else if(map[i][j]==201)// Under the border  
 printf(" # "); 
 else if(map[i][j]==0)// Blank,  
 printf(" "); 
 else if(map[i][j]==1)// Moving tetris  
 printf(" * "); 
 else if(map[i][j]==10)// Fixed tetris  
 printf(" @ "); 
 } 
 printf("\n"); 
 } 
} 
void delLine(int n)// Line elimination  
{ 
 int i,j; 
 for(j = 1; j<13; j++) 
 map[n][j] = 0; 
 for(i = n; i>5 ; i--) 
 for(j = 1; j<13; j++) 
 if(map[i-1][j] != 1) 
 map[i][j] = map[i-1][j]; 
} 
 
void isFillLine()// Whether the row cancellation condition is satisfied  
{ 
 
 int i,j; 
 int fals; 
 for(i=19; i>5; i--) 
 { 
 fals = 1; 
 for(j=1; j<13; j++) 
 { 
 if(map[i][j] != 10) 
 { 
 fals = 0; 
 continue; 
 } 
 } 
 if(fals) 
 { 
 delLine(i); 
 } 
 } 
} 
void down()// Move down  
{ 
 int i, j; 
 int fale = 1; 
 for(i=3; i>=0; i--) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) 
 if(map[bl.x+i+1][bl.y+j] == 10 || map[bl.x+i+1][bl.y+j] == 201) 
 { 
 change(); 
 fale = 0; 
 new_block(); 
 isFillLine(); 
 return; 
 } 
 if(fale) 
 { 
 output(); 
 bl.x += 1; 
 input(); 
 } 
 
} 
void right()// Moves to the right  
{ 
 int i, j; 
 int fale = 1; 
 for(i=3; i>=0; i--) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) 
 if(map[bl.x+i][bl.y+j+1] == 10 || map[bl.x+i][bl.y+j+1] == 200) 
 { 
 fale = 0; 
 return; 
 } 
 if(fale) 
 { 
 output(); 
 bl.y += 1; 
 input(); 
 } 
 
} 
void left()// Shift to the left  
{ 
 int i, j; 
 int fale = 1; 
 for(i=3; i>=0; i--) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j] == 1) 
 if(map[bl.x+i][bl.y+j-1] == 10 || map[bl.x+i][bl.y+j-1] == 200) 
 { 
 fale = 0; 
 return; 
 } 
 if(fale) 
 { 
 output(); 
 bl.y -= 1; 
 input(); 
 } 
 
} 
 
void change_block()// Tetris morphing  
{ 
 int i,j; 
 output(); 
 int fals = 1; 
 bl.blockDirect += 1; 
 bl.blockDirect %= 4; 
 for(i=0; i<4; i++) 
 for(j=0; j<4; j++) 
 if(node[bl.blockType][bl.blockDirect][i*4+j]==1) 
 if(map[bl.x+i][bl.y+j] != 0 ) 
 { 
 fals = 0; 
 break; 
 } 
 if(fals) 
 { 
 input(); 
 }else 
 { 
 bl.blockDirect -= 1; 
 input(); 
 } 
} 
 
char in_direct()// Non-blocking input  
{ 
 fd_set fd; 
 struct timeval tv; 
 char ch; 
 FD_ZERO(&fd); 
 FD_SET(0, &fd); 
 tv.tv_sec = 0; 
 tv.tv_usec = 10; 
 if(select(1, &fd ,NULL, NULL, &tv) > 0) 
 { 
 ch = getchar(); 
 } 
 return ch; 
} 
int main()//q  Quit the game, a,d  Move left and right, space deformation  
{ 
 srand(time(NULL)); 
 init_map(); 
 new_block(); 
 input(); 
 char ch; 
 int num = 0; 
 while(1) 
 { 
 usleep(500000); 
 system(STTY_ON TTY_PATH); 
 ch = in_direct(); 
 system(STTY_OFF TTY_PATH); 
 system("clear"); 
 if(ch == 'a' && num <= 1) 
 { 
 left(); 
 print_map(); 
 num++; 
 continue; 
 }else if(ch == 'd' && num <= 1) 
 { 
 right(); 
 print_map(); 
 num++; 
 continue; 
 }else if(ch == ' ' && num <= 1 ) 
 { 
 change_block(); 
 print_map(); 
 num++; 
 continue; 
 }else if(ch == 'q') 
 { 
 system("clear"); 
 printf("gave over!!!!!\n"); 
 exit(0); 
 } 
 down(); 
 print_map(); 
 num = 0; 
 
 } 
 return 0; 
}

For more great articles on tetris, please click on the topic: tetris game collection to learn.

More interesting classic games to share with you:

C++ classic small game summary

python classic small game summary

JavaScript classic games play non-stop

java classic small game summary

javascript classic small game summary


Related articles: