- #include <stdio.h>
- #include <conio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <windows.h>
- //#define KEY 224
- #define UP 72
- #define DOWN 80
- #define LEFT 75
- #define RIGHT 77
- #define SPACE 32
- #define ESC 27
- #define MAPX 20
- #define MAPY 20
- #define MAPL 0+2
- #define MAPR MAPX-3
- #define MAPB MAPY-3
- #define KIND 7
- #define FORM 4
- #define SIZE 16
- int shape[KIND][FORM][SIZE] =
- {
- {//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,
- },
- {
- 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
- {
- 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,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,
- }
- },
- {//2
- {
- 0,0,1,0,
- 0,1,1,0,
- 0,1,0,0,
- 0,0,0,0,
- },
- {
- 0,0,0,0,
- 0,1,1,0,
- 0,0,1,1,
- 0,0,0,0,
- },
- {
- 0,0,1,0,
- 0,1,1,0,
- 0,1,0,0,
- 0,0,0,0,
- },
- {
- 0,0,0,0,
- 0,1,1,0,
- 0,0,1,1,
- 0,0,0,0,
- }
- },
- {//3
- {
- 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,
- 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,
- }
- },
- {//4
- {
- 0,0,0,0,
- 0,1,0,0,
- 1,1,1,0,
- 0,0,0,0,
- },
- {
- 0,0,0,0,
- 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,
- }
- },
- {//5
- {
- 0,0,0,0,
- 0,1,1,0,
- 0,0,1,0,
- 0,0,1,0,
- },
- {
- 0,0,0,0,
- 0,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,
- }
- },
- {//6
- {
- 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,0,1,1,
- 0,0,1,0,
- 0,0,1,0,
- 0,0,0,0,
- },
- {
- 0,0,0,0,
- 0,1,1,1,
- 0,0,0,1,
- 0,0,0,0,
- }
- }
- };
- int pool[MAPX][MAPY];
- int cx,cy,kind,form,score;
- HWND hwnd;
- HANDLE hout;
- CONSOLE_CURSOR_INFO info;
- void gotoxy(int x,int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(hout,coord);
- }
- void food()
- {
- int i,j,s;
- form = 0;
- cx = 8,cy = 0;
- kind = rand()%KIND;
- for(s=0,i=cx;i<cx+4;i++)
- for(j=cy;j<cy+4;j++,s++)
- if(shape[kind][form][s])
- gotoxy(i*2,j),puts("¡ö");
- }
- void init()
- {
- hwnd = FindWindow(NULL,NULL);
- hout = GetStdHandle(STD_OUTPUT_HANDLE);
- system("mode con cols=40 lines=22");
- info.bVisible = 0;
- info.dwSize = 25;
- score = 0;
- SetConsoleCursorInfo(hout,&info);
- SetConsoleTitle("¶íÂÞ˹·½¿éV1.1 by Á÷ÐDz»ËÀ");
- srand((unsigned)time(NULL));
- int i,j;
- for(i=0;i<MAPX;i++)
- for(j=0;j<MAPY;j++)
- {
- pool[i][j] = 0;
- if(i<MAPL || i>MAPR || j>MAPB)
- {
- pool[i][j] = -1;
- gotoxy(i*2,j);
- puts("¡õ");
- }
- }
- food();
- gotoxy(0,MAPY),printf("Score:%d",score);
- gotoxy(10,MAPY),printf("¡ü¡ý¡û¡úÒƶ¯ ESCÍ˳ö SPACEÔÝÍ£");
- }
- void copy(int x,int y)
- {
- int i,j,s;
- for(s=0,i=x;i<x+4;i++)
- for(j=y;j<y+4;j++,s++)
- if(pool[i][j] && shape[kind][form][s])
- return;
- for(i=cx;i<cx+4;i++)
- for(j=cy;j<cy+4;j++)
- if(pool[i][j] == -1)
- gotoxy(i*2,j),puts("¡õ");
- else if(pool[i][j] == 1)
- gotoxy(i*2,j),puts("¡ö");
- else
- gotoxy(i*2,j),puts(" ");
- for(s=0,i=x;i<x+4;i++)
- for(j=y;j<y+4;j++,s++)
- if(shape[kind][form][s])
- gotoxy(i*2,j),puts("¡ö");
- cx = x,cy = y;
- }
- void copy2(int x,int y)
- {
- int i,j,s;
- form++;
- form = form%4;
- for(s=0,i=x;i<x+4;i++)
- for(j=y;j<y+4;j++,s++)
- if(pool[i][j] && shape[kind][form][s])
- {
- form--;
- if(form<0)
- form = 3;
- return;
- }
- for(s=0,i=x;i<x+4;i++)
- for(j=y;j<y+4;j++,s++)
- if(shape[kind][form][s])
- gotoxy(i*2,j),puts("¡ö");
- else if(-1 == pool[i][j])
- gotoxy(i*2,j),puts("¡õ");
- else if(1 == pool[i][j])
- gotoxy(i*2,j),puts("¡ö");
- else
- gotoxy(i*2,j),puts(" ");
- }
- void map(int n)
- {
- int i,j;
- for(i=MAPL;i<=MAPR;i++)
- for(j=n;j>=1;j--)
- pool[i][j] = pool[i][j-1];
- score+=10;
- for(i=0;i<MAPX;i++)
- for(j=0;j<MAPY;j++)
- if(1 == pool[i][j])
- gotoxy(i*2,j),puts("¡ö");
- else if(-1 == pool[i][j])
- gotoxy(i*2,j),puts("¡õ");
- gotoxy(0,MAPY),printf("Score:%d",score);
- }
- void clear()
- {
- int i,j,k,w;
- int flag;
- for(j=MAPB;j>=0;)
- {
- flag = 1;
- for(i=MAPL;i<=MAPR;i++)
- {
- if(!pool[i][j])
- flag = 0;
- }
- if(flag)
- {
- for(k=MAPL;k<=MAPR;k++)
- for(w=0;w<=MAPB;w++)
- gotoxy(k*2,w),puts(" ");
- map(j);
- }
- else
- j--;
- }
- }
- int set()
- {
- char str[80];
- int i,j,s;
- int flag = 0;
- for(s=0,i=cx;i<cx+4;i++)
- for(j=cy;j<cy+4;j++,s++)
- if(shape[kind][form][s] && pool[i][j+1])
- {
- flag = 1;
- goto k1;
- }
- k1:
- if(!flag)return 0;
- for(s=0,i=cx;i<cx+4;i++)
- for(j=cy;j<cy+4;j++,s++)
- if(pool[i][j] == -1)
- continue;
- else
- {
- if(shape[kind][form][s] == 1)
- pool[i][j] = 1;
- }
- clear();
- for(j=2,i=MAPL;i<=MAPR;i++)
- if(pool[i][j] == 1)
- {
- sprintf(str,"ÄãÊäÁË!µÃ·Ö:%d",score);
- MessageBox(hwnd,str,"Á÷ÐDz»ËÀµÄ¶íÂÞ˹·½¿é",0);
- exit(0);
- }
- food();
- return 1;
- }
- void elsfk()
- {
- int i,j,s,f;
- char key;
- int time;
- init();
- for(time=50;;time--)
- {
- if(kbhit())
- {
- key = getch();
- if(key < 0) key = getch();
- switch(key)
- {
- case UP:copy2(cx,cy);break;
- case DOWN:
- while(1)
- {
- copy(cx,cy+1);
- if(set())break;
- }break;
- case LEFT:copy(cx-1,cy);break;
- case RIGHT:copy(cx+1,cy);break;
- case SPACE:break;
- case ESC:return;
- default:break;
- }
- }
- if(time<0)
- {
- if(1 == f)
- {
- set();
- f = 0;
- }
- f = 1;
- copy(cx,cy+1),time = 50;
- }
- Sleep(1);
- }
- }
- int main()
- {
- elsfk();
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/1510201513811.html
来源: http://www.codesnippet.cn/detail/1510201513811.html