diff --git a/俄罗斯方块.c b/俄罗斯方块.c index 6342cb7..9af7cf3 100644 --- a/俄罗斯方块.c +++ b/俄罗斯方块.c @@ -1,216 +1,221 @@ -/********ͷļ*********/ -#include -#include -#include -#include - - -/********궨********/ -#define FrameX 13 -#define FrameY 3 -#define Frame_height 20 //Ϸڸ߶ -#define Frame_width 18//Ϸڿ - - -/********ȫֱ*********/ -int i,j,Temp,Temp1,Temp2; -int a[80][80]={0};//a2,1,0ֱ߿򣬷ͼ -int b[4];//ÿַ鶼ĸɣbĸλԹͼ -struct Tetris +/*******ͷ *******/ +#include //׼⣨printfscanf +#include //dos棨ȡ̨λáɫ +#include //ռkbhit()getch() +#include //ڻ +/******* *******/ +#define FrameX 13 //ϷϽǵXΪ13 +#define FrameY 3 //ϷϽǵYΪ13 +#define Frame_height 20 //Ϸڵĸ߶Ϊ20 +#define Frame_width 18 //ϷڵĿΪ18 +/******* ȫ *******/ +int i,j,Temp,Temp1,Temp2; //temp,temp1,temp2ڼסתֵ +//ϷĻͼ2,1,0ֱʾλΪϷ߿򡢷顢ͼ;ʼΪͼ +int a[80][80]={0}; +int b[4]; //4""飺1ʾз飬0ʾ޷ +struct Tetris //˹Ľṹ { - int x; //ķx - int y; //ķy - int flag;//Ƿ͵ - int next;//һ͵ - int speed;//˹ٶ - int number;//˹ĸ - int score;//Ϸ - int level;//Ϸȼ + int x; //ķx + int y; //ķy + int flag; //Ƿ͵ + int next; //һ˹͵ + int speed; //˹ƶٶ + int number; //˹ĸ + int score; //Ϸķ + int level; //Ϸĵȼ }; -HANDLE hOut;//̨ - - -/*************/ -int color(int c); -static void gotoxy(int x, int y);//ƶλ -void DrawGameframe(); //Ϸ߿ -void Flag(struct Tetris * );//˹ -void MakeTetris(struct Tetris *);//˹ -void PrintTetris(struct Tetris *);//ӡ˹ -void CleanTetris(struct Tetris *);//˹ĺۼ -int ifMove(struct Tetris *);//жܷƶ -void Del_Fullline(struct Tetris *);//жǷ -void GamePlay();//ʼϷ -void regulation();//Ϸ -void explation();//˵ -void welcome();//ӭ -void Replay(struct Tetris *);//¿ʼϷ -void title();//ӭϷ -void close();//رϷ - - -/*****ɫ******/ -int color(int c){ - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);//ɫ +HANDLE hOut; //̨ +/******* *******/ +int color(int c); //ɫ +void gotoxy(int x, int y); //Ƶָλ +void DrwaGameframe(); //Ϸ߿ +void Flag(struct Tetris *); //͵ +void MakeTetris(struct Tetris *); //˹ +void PrintTetris(struct Tetris *); //ӡ˹ +void CleanTetris(struct Tetris *); //˹ĺۼ +int ifMove(struct Tetris *); //жǷƶֵΪ1ƶ򣬲ƶ +void Del_Fullline(struct Tetris *); //жǷУɾеĶ˹ +void Gameplay(); //ʼϷ +void regulation(); //Ϸ +void explation(); //˵ +void welcom(); //ӭ +void Replay(struct Tetris * tetris); //¿ʼϷ +void title(); //ӭϷı +void flower(); //ӭϵַװλ +void close(); +/** + * ɫ ˺ľԣ1ֻWindowsϵͳʹ 2ܸı䱳ɫ + */ +int color(int c) +{ + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c); //ɫ return 0; -} - - -/*****ȡĻλ*******/ -static void gotoxy(int x, int y){ -COORD point = { x, y };//Ҫõλx,y -HANDLE HOutput = GetStdHandle(STD_OUTPUT_HANDLE);//ʹGetStdHandle(STD_OUTPUT_HANDLE)ȡ׼ľ -SetConsoleCursorPosition(HOutput, point);//ùλ +} //رϷ +/** + * ȡĻλ + */ +void gotoxy(int x, int y) +{ + COORD pos; + pos.X = x; // + pos.Y = y; // + SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } +/** + * ӭϷı + */ +void title() +{ + color(15); //ɫ + gotoxy(28,3); + printf("Ȥ ζ ˹ \n"); // + color(11); //ɫ + gotoxy(18,5); + printf(""); // + gotoxy(18,6); // + printf(""); // + gotoxy(18,7); + printf(""); + color(14); //ɫ + gotoxy(26,6); + printf(""); // + gotoxy(28,7); // + printf(""); + color(10); //ɫ + gotoxy(36,6); // + printf(""); // + gotoxy(36,7); + printf(""); -/*****ӭ********/ -void title(){ - color(15); - gotoxy(30,3); - printf("Ȥζ˹\n"); - // - color(11); - gotoxy(18,5); - printf(""); - gotoxy(18,6); - printf(""); - gotoxy(18,7); - printf(""); - //Tַ - color(14); - gotoxy(26,6); - printf(""); - gotoxy(28,7); - printf(""); - //Zַ - color(10); - gotoxy(36,6); - printf(""); - gotoxy(36,7); - printf(""); - //ַ - color(13); + color(13); //ɫ gotoxy(45,5); - printf(""); - gotoxy(45,6); - printf(""); - gotoxy(45,7); - printf(""); + printf(""); // + gotoxy(45,6); // + printf(""); // + gotoxy(45,7); // + printf(""); gotoxy(45,8); - printf(""); - //ֱ߷ - color(12); - gotoxy(56,6); - printf(""); - gotoxy(52,7); - printf("\n"); - //7ַ -} - + printf(""); -/*******˵ѡ߿*******/ -void welcome() + color(12); //ɫ + gotoxy(56,6); + printf(""); // + gotoxy(52,7); // + printf(""); +} +/** + * ˵ѡ߿ + */ +void welcom() { int n; - int i,j=1; - color(14); - for(i=9;i<=20;i++){ - for(j=15;j<=60;j++){ - gotoxy(j,i); - if(i==9||i==20) printf("="); - else if(j==15||j==59) printf("::"); + int i,j = 1; + color(14); //ɫ߿ + for (i = 9; i <= 20; i++) //ѭy꣬ӡ±߿=== + { + for (j = 15; j <= 60; j++) //ѭx꣬ӡұ߿|| + { + gotoxy(j, i); + if (i == 9 || i == 20) printf("="); //±߿=== + else if (j == 15 || j == 59) printf("||"); //ұ߿|| } - } -/*******˵ѡ********/ - color(12); - gotoxy(25,12); - printf("1.ʼϷ"); - gotoxy(40,12); + } +/** + * ˵ѡ + */ + color(12); //ɫΪɫ + gotoxy(25, 12); //ʾλ + printf("1.ʼϷ"); //֡1.ʼϷ + gotoxy(40, 12); printf("2.˵"); - gotoxy(25,17); + gotoxy(25, 17); printf("3.Ϸ"); - gotoxy(40,17); - printf("4.˳"); + gotoxy(40, 17); + printf("4.˳"); gotoxy(21,22); - printf("ѡ[1,2,3,4]:[ ]\b\b"); - scanf("%d",&n); - switch(n){ - case 1: - system("cls"); - DrawGameframe(); - Gameplay(); - break; - case 2: - break; - case 3: - break; - case 4: + color(3); + printf("ѡ[1 2 3 4]:[ ]\b\b"); + color(14); + scanf("%d", &n); //ѡ + switch (n) + { + case 1: //1 + system("cls"); // + DrwaGameframe(); + Gameplay(); + break; + case 2: //2 + explation(); break; - } + case 3: + regulation(); //3 + break; + case 4: + close(); //4 + break; + } } - - -/*******Ϸ*******/ -void DrawGameframe() +/** + * Ϸ + */ +void DrwaGameframe() { - /***********Ϸ***********/ - gotoxy(FrameX+Frame_width-7,FrameY-2); - color(11); - printf("Ȥζ˹"); - /**********һַ********/ - gotoxy(FrameX+2*Frame_width+3,FrameY+7); - color(2); - printf("**********"); - gotoxy(FrameX+2*Frame_width+13,FrameY+7); - color(3); - printf("һַ"); - gotoxy(FrameX+2*Frame_width+3,FrameY+13); - color(2); - printf("**********"); - /**********˵***********/ + gotoxy(FrameX+Frame_width-5,FrameY-2); //ϷƵʾλ + color(11); //ɫΪɫ + printf("Ȥζ˹"); //ӡϷ + gotoxy(FrameX+2*Frame_width+3,FrameY+7); //ϱ߿ʾλ + color(2); //ɫΪɫ + printf("**********"); //ӡһַϱ߿ + gotoxy(FrameX+2*Frame_width+13,FrameY+7); + color(3); //ɫΪɫ + printf("һַ飺"); + gotoxy(FrameX+2*Frame_width+3,FrameY+13); + color(2); + printf("**********"); //ӡһַ±߿ gotoxy(FrameX+2*Frame_width+3,FrameY+17); - color(14); - printf(":ת"); + color(14); //ɫΪɫ + printf("ת"); gotoxy(FrameX+2*Frame_width+3,FrameY+19); printf("ոͣϷ"); - gotoxy(FrameX+2*Frame_width+3,FrameY+15); - printf("Esc:˳Ϸ"); - /*********Ϸ߿***********/ - gotoxy(FrameX,FrameY); + gotoxy(FrameX+2*Frame_width+3,FrameY+15); + printf("Esc ˳Ϸ"); + gotoxy(FrameX,FrameY); color(12); - printf("X"); - gotoxy(FrameX+2*Frame_width-2,FrameY); - printf("["); - gotoxy(FrameX,FrameY+Frame_height); - printf("^"); - gotoxy(FrameX+2*Frame_width-2,FrameY+Frame_height); - printf("a"); - a[FrameX][FrameY+Frame_height]=2; - a[FrameX+2*Frame_width-2][FrameY+Frame_height]=2;//ֹ - for(i=2;i<2*Frame_width-2;i+=2){ - gotoxy(FrameX+i,FrameY); - printf("T"); - } - for(i=2;i<2*Frame_width-2;i+=2){ - gotoxy(FrameX+i,FrameY+Frame_height); - printf("T"); - a[FrameX+i][FrameY+Frame_height]=2; - } - for(i=1;iflag) //7࣬19 { case 1: /*ַ - */ + */ { color(10); a[tetris->x][tetris->y-1]=b[1]; @@ -236,19 +241,19 @@ void MakeTetris(struct Tetris *tetris) a[tetris->x+4][tetris->y]=b[3]; break; } - case 3: /*ֱ߷ + case 3: /*ֱ߷ - + */ { color(13); a[tetris->x][tetris->y-1]=b[1]; - a[tetris->x][tetris->y+1]=b[2]; - a[tetris->x][tetris->y+2]=b[3]; + a[tetris->x][tetris->y-2]=b[2]; + a[tetris->x][tetris->y+1]=b[3]; break; } case 4: /*Tַ - */ + */ { color(11); a[tetris->x-2][tetris->y]=b[1]; @@ -286,7 +291,7 @@ void MakeTetris(struct Tetris *tetris) break; } case 8: /* Zַ - */ + */ { color(14); a[tetris->x][tetris->y+1]=b[1]; @@ -294,7 +299,7 @@ void MakeTetris(struct Tetris *tetris) a[tetris->x+2][tetris->y+1]=b[3]; break; } - case 9: /* ˳ʱZַ + case 9: /* ˳ʱZַ */ { @@ -304,7 +309,7 @@ void MakeTetris(struct Tetris *tetris) a[tetris->x-2][tetris->y+1]=b[3]; break; } - case 10: /* תZַ + case 10: /* תZַ */ { color(14); @@ -313,7 +318,7 @@ void MakeTetris(struct Tetris *tetris) a[tetris->x+2][tetris->y]=b[3]; break; } - case 11: /* ˳ʱZַ + case 11: /* ˳ʱZַ */ { @@ -323,8 +328,8 @@ void MakeTetris(struct Tetris *tetris) a[tetris->x-2][tetris->y]=b[3]; break; } - case 12: /* 7ַ - + case 12: /* 7ַ + */ { color(12); @@ -362,8 +367,8 @@ void MakeTetris(struct Tetris *tetris) break; } case 16: /* ת7ַ - - */ + + */ { color(12); a[tetris->x][tetris->y+1]=b[1]; @@ -380,7 +385,7 @@ void MakeTetris(struct Tetris *tetris) a[tetris->x+2][tetris->y]=b[3]; break; } - case 18: /* ˳ʱת1807ַ + case 18: /* ˳ʱת1807ַ */ { @@ -401,40 +406,44 @@ void MakeTetris(struct Tetris *tetris) } } } - - - -/******ӡ˹*******/ -void PrintTetris(struct Tetris *tetris){ - for(i=0;i<4;i++) b[i]=1; - MakeTetris(tetris); - for(i=tetris->x-2;i<=tetris->x+4;i+=2){ - for(j=tetris->y-1;j<=tetris->y+1;j++){ - if(a[i][j]==1&&j>FrameY){ - gotoxy(i,j); - printf(""); - } - } +/** + * ӡ˹ + */ +void PrintTetris(struct Tetris *tetris) +{ + for(i=0;i<4;i++) //b[4]4Ԫأѭ4ԪأÿԪصֵΪ1 + { + b[i]=1; //b[4]ÿԪصֵΪ1 + } + MakeTetris(tetris); //Ϸ + for( i=tetris->x-2; i<=tetris->x+4; i+=2 ) + { + for(j=tetris->y-2;j<=tetris->y+1;j++) //ѭпֵܳλ + { + if( a[i][j]==1 && j>FrameY ) //λз + { + gotoxy(i,j); + printf(""); //ӡ߿ڵķ + } + } } - //ӡ˵Ϣ - gotoxy(FrameX+2*Frame_width+3,FrameY+1); + //ӡ˵Ϣ + gotoxy(FrameX+2*Frame_width+3,FrameY+1); //ôӡλ color(4); - printf("level:"); - color(12); - printf("%d",tetris->level); + printf("level : "); + color(12); + printf(" %d",tetris->level); //ȼ gotoxy(FrameX+2*Frame_width+3,FrameY+3); color(4); - printf("score:"); - color(12); - printf("%d",tetris->score); - gotoxy(FrameX+2*Frame_width+3,FrameY+3); + printf("score : "); + color(12); + printf(" %d",tetris->score); // + gotoxy(FrameX+2*Frame_width+3,FrameY+5); color(4); - printf("speed"); - color(12); - printf("%dms",tetris->speed); -} - - + printf("speed : "); + color(12); + printf(" %dms",tetris->speed); //ٶ +} /** * жǷƶ */ @@ -494,11 +503,12 @@ int ifMove(struct Tetris *tetris) } return 0; } - - - +/** + * ˹ĺۼ + */ void CleanTetris(struct Tetris *tetris) { + int n; for(i=0;i<4;i++) //b[4]4Ԫأѭ4ԪأÿԪصֵΪ0 { b[i]=0; //b[4]ÿԪصֵΪ0 @@ -506,7 +516,7 @@ void CleanTetris(struct Tetris *tetris) MakeTetris(tetris); //˹ for( i = tetris->x - 2;i <= tetris->x + 4; i+=2 ) //X XΪķ { - for(j = tetris->y-2;j <= tetris->y + 1;j++) /* + for(j = tetris->y-2;j <= tetris->y + 1;j++) /* X */ @@ -519,13 +529,12 @@ void CleanTetris(struct Tetris *tetris) } } } - - /** * жǷвɾеĶ˹ */ void Del_Fullline(struct Tetris *tetris) //ijFrame_width-2ʱ { + int n; int k,del_rows=0; //ֱڼ¼ijзĸɾı for(j=FrameY+Frame_height-1;j>=FrameY+1;j--) { @@ -555,7 +564,7 @@ void Del_Fullline(struct Tetris *tetris) // printf(" "); a[i][k+1]=1; gotoxy(i,k+1); - printf(""); + printf(""); } } } @@ -571,20 +580,41 @@ void Del_Fullline(struct Tetris *tetris) // tetris->speed-=20; tetris->level++; } -} - - -/******˹͵*******/ -void Flag(struct Tetris *tetris){ - tetris->number++; - srand(time(NULL)); - if(tetris->number==1){ - tetris->flag=rand()%19+1; - } - tetris->next=rand()%19+1; -} - - + if(tetris->speed==0) + { + system("cls"); // + gotoxy(17,18); + color(14); + printf("ͨ"); + gotoxy(32,20); + printf("ѡ1/2"); + color(11); + scanf("%d", &n); //12 + switch (n) //֧ + { + case 1: + system("cls"); + Replay(tetris); //¿ʼϷ + break; + case 2: + exit(0); + break; + } + } +} +/** + * ˹͵ + */ +void Flag(struct Tetris *tetris) +{ + tetris->number++; //סĸ + srand(time(NULL)); //ʼ + if(tetris->number==1) + { + tetris->flag = rand()%19+1; //סһ + } + tetris->next = rand()%19+1; //סһ +} /** * ʼϷ */ @@ -607,7 +637,8 @@ void Gameplay() PrintTetris(tetris); //ôӡ˹鷽 tetris->x=FrameX+Frame_width; //Ϸķx tetris->y=FrameY-1; //Ϸķy - tetris->flag=Temp; // + tetris->flag=Temp; + // while(1) //Ʒ鷽ֱ鲻 { label:PrintTetris(tetris); //ӡ˹ @@ -626,17 +657,6 @@ void Gameplay() { tetris->x+=2; } - if(ch==80) // - { - if(ifMove(tetris)!=0) - { - tetris->y+=2; - } - if(ifMove(tetris)==0) - { - tetris->y=FrameY+Frame_height-2; - } - } if(ch==72) // ,ǰ˳ʱת90 { if( tetris->flag>=2 && tetris->flag<=3 ) //ֱ߷ @@ -670,6 +690,17 @@ void Gameplay() tetris->flag+=16; } } + if(ch==80) // + { + if(ifMove(tetris)!=0) + { + tetris->y+=2; + } + if(ifMove(tetris)==0) + { + tetris->y=FrameY+Frame_height-2; + } + } if(ch == 32) //ոͣ { PrintTetris(tetris); @@ -688,9 +719,9 @@ void Gameplay() if(ch == 27) { system("cls"); - memset(a,0,6400*sizeof(int)); - title(); //ʼa - welcome(); + memset(a,0,6400*sizeof(int)); //ʼa + title(); + welcom(); } if(ifMove(tetris)==0) //ɶЧ { @@ -710,20 +741,13 @@ void Gameplay() Del_Fullline(tetris); break; } - }//Ϸ鴥λ + } +//Ϸ鴥λ for(i=tetris->y-2;iy+2;i++) { if(i==FrameY) { system("cls"); // - gotoxy(29,7); //ʾλ - printf(" \n"); - color(12); //ɫ - printf("\t\t\t \n"); - printf("\t\t\t \n"); - printf("\t\t\t \n"); - printf("\t\t\t \n"); - printf("\t\t\t \n"); gotoxy(17,18); color(14); printf("Ҫһ-------1"); @@ -752,21 +776,104 @@ for(i=tetris->y-2;iy+2;i++) CleanTetris(tetris); } } - - -/*******¿ʼϷ*******/ -void Replay(struct Tetris *tetris){ +/** + * ¿ʼϷ + */ +void Replay(struct Tetris *tetris) +{ + system("cls"); // + memset(a,0,6400*sizeof(int)); //ʼa飬򲻻ʾ飬Ϸֱӽ + DrwaGameframe(); //Ϸ + Gameplay(); //ʼϷ +} +/** + * ˵ + */ +void explation() +{ + int i,j = 1; + system("cls"); // + color(13); //ɫ + gotoxy(32,3); //ʾλ + printf("˵"); + color(2); + for (i = 6; i <= 16; i++) //±߿=== + { + for (j = 15; j <= 60; j++) //ұ߿|| + { + gotoxy(j, i); + if (i == 6 || i == 16) printf("="); + else if (j == 15 || j == 59) printf("||"); + } + } + color(3); + gotoxy(18,7); + printf("tip1: ҿͨ ƶ"); + color(10); + gotoxy(18,9); + printf("tip2: ͨ ʹת"); + color(14); + gotoxy(18,11); + printf("tip3: ͨ ٷ"); + color(11); + gotoxy(18,13); + printf("tip4: ոͣϷٰո"); + color(4); + gotoxy(18,15); + printf("tip5: ESC˳Ϸ"); + getch(); // + system("cls"); // + main(); // +} +/** + * Ϸ + */ +void regulation() +{ + int i,j = 1; + system("cls"); + color(13); + gotoxy(34,3); + printf("Ϸ"); + color(2); + for (i = 6; i <= 18; i++) //±߿=== + { + for (j = 12; j <= 70; j++) //ұ߿|| + { + gotoxy(j, i); + if (i == 6 || i == 18) printf("="); + else if (j == 12 || j == 69) printf("||"); + } + } + color(12); + gotoxy(16,7); + printf("tip1: ͬ״СĻϷ£ͨ"); + gotoxy(22,9); + printf("λúͷʹĻײƴ"); + gotoxy(22,11); + printf("һл"); + color(14); + gotoxy(16,13); + printf("tip2: ÿһУ100"); + color(11); + gotoxy(16,15); + printf("tip3: ÿۼ1000֣һȼ"); + color(10); + gotoxy(16,17); + printf("tip4: ȼʹٶȼӿ죬ϷѶȼӴ"); + getch(); // system("cls"); - memset(a,0,6400*sizeof(int)); - DrawGameframe(); - Gameplay(); -} - - -/**************/ + welcom(); +} +/** +* ˳ +*/ +void close() +{ + exit(0); +} int main(){ title(); - welcome(); - return 0; + welcom(); }