diff --git a/俄罗斯方块.c b/俄罗斯方块.cpp similarity index 60% rename from 俄罗斯方块.c rename to 俄罗斯方块.cpp index 664487d..0585a17 100644 --- a/俄罗斯方块.c +++ b/俄罗斯方块.cpp @@ -25,6 +25,7 @@ struct Tetris } ; HANDLE hOut; //̨ /********/ +void HideCursor(); int color(int c); //ɫ void gotoxy(int x,int y); //ȡĻλ void Game(); //Ϸ @@ -34,18 +35,24 @@ void rule(); // void Make(struct Tetris *); // void Print(struct Tetris *); //ӡ void Clean(struct Tetris *); //ۼ - /** - *ɫ - */ +int ifMove(struct Tetris *); //жǷƶ10 +void Flag(struct Tetris *); // +void Full(struct Tetris *); // жϷǷ + + /*ؿ̨Ĺ*/ +void HideCursor() +{ + CONSOLE_CURSOR_INFO cursor_info = {1, 0}; + SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); +} + /**ɫ */ int color(int c) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c); return 0; } -/** - *ȡĻλ - */ +/*ȡĻλ */ void gotoxy(int x,int y) { COORD pos; @@ -56,10 +63,8 @@ void gotoxy(int x,int y) -/** - *ҳ - */ -void main() +/*ҳ */ +int main() { /*ͼ*/ color(11); @@ -174,11 +179,10 @@ void main() case 4: break; } + return 0; } -/** - *˵ - */ +/*˵ */ void explation() { int i,j = 1; @@ -196,22 +200,22 @@ void explation() } } color(12); - gotoxy(16,9); + gotoxy(16,8); printf("1/ͨ ƶ"); - gotoxy(16,11); + gotoxy(16,10); printf("2/ͨ ʹת"); - gotoxy(16,13); + gotoxy(16,12); printf("3/ͨ ٷ"); - gotoxy(16,15); + gotoxy(16,14); printf("4/ͨոͣϷ"); + gotoxy(16,16); + printf("5/ͨ Esc ҳ"); getch(); // system("cls"); main(); } -/** - *Ϸ - */ +/*Ϸ */ void rule() { int i,j = 1; @@ -248,11 +252,12 @@ void rule() system("cls"); main(); } +/*Ϸ */ void Game() { gotoxy(FrameX+Frame_width-7,FrameY-2); color(11); - printf("Ȥζ˹"); + printf("ҵĶ˹"); gotoxy(FrameX+2*Frame_width+3,FrameY+7); color(2); printf("***********"); @@ -268,34 +273,27 @@ void Game() gotoxy(FrameX+2*Frame_width+3,FrameY+19); printf("ոͣϷ"); gotoxy(FrameX+2*Frame_width+3,FrameY+15); - printf("Esc:˳Ϸ"); - gotoxy(FrameX,FrameY); //ӡ + printf("Esc:ҳ"); + color(6); - printf(""); - gotoxy(FrameX+2*Frame_width-2,FrameY); - printf(""); - gotoxy(FrameX,FrameY+Frame_height); - printf(""); - gotoxy(FrameX+2*Frame_width-2,FrameY+Frame_height); - printf(""); - for(i=2;i<2*Frame_width-2;i+=2) + for(i=0;i<=2*Frame_width-2;i+=2) { gotoxy(FrameX+i,FrameY); printf(""); //ӡϺ } - for(i=2;i<2*Frame_width-2;i+=2) + for(i=0;i<=2*Frame_width-2;i+=2) { gotoxy(FrameX+i,FrameY+Frame_height); printf(""); //ӡº a[FrameX+i][FrameY+Frame_height]=2; //ºΪϷ߿򣬷ֹ } - for(i=1;ix][tetris->y]=b[0]; //ķλõͼ״̬ @@ -334,8 +330,8 @@ void Make(struct Tetris *tetris) { 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; } @@ -414,18 +410,18 @@ void Make(struct Tetris *tetris) case 12: // 7 { color(1); - a[tetris->x-2][tetris->y]=b[1]; + 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-2][tetris->y-1]=b[3]; break; } case 13: // ˳90 7 { color(2); - a[tetris->x-2][tetris->y]=b[1]; - a[tetris->x-4][tetris->y]=b[2]; - a[tetris->x][tetris->y-1]=b[3]; + a[tetris->x+2][tetris->y-1]=b[1]; + a[tetris->x-2][tetris->y]=b[2]; + a[tetris->x+2][tetris->y]=b[3]; break; } @@ -450,9 +446,9 @@ void Make(struct Tetris *tetris) case 16: // 7 { color(9); - a[tetris->x+2][tetris->y]=b[1]; + a[tetris->x+2][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-1]=b[3]; break; } @@ -460,8 +456,8 @@ void Make(struct Tetris *tetris) { color(10); a[tetris->x-2][tetris->y]=b[1]; - a[tetris->x-4][tetris->y]=b[2]; - a[tetris->x][tetris->y+1]=b[3]; + a[tetris->x+2][tetris->y]=b[2]; + a[tetris->x+2][tetris->y+1]=b[3]; break; } @@ -485,9 +481,7 @@ void Make(struct Tetris *tetris) } } -/** - *ӡ - */ +/*ӡ*/ void Print(struct Tetris *tetris) { for(i=0;i<4;i++) //b[4]еÿΪ1 @@ -524,9 +518,7 @@ void Make(struct Tetris *tetris) } -/** - * ʼϷ - */ +/* ʼϷ*/ void play() { int n; @@ -535,7 +527,8 @@ void play() tetris->number=0; tetris->speed=300; tetris->score=0; - tetris->level=1; + tetris->level=1; + HideCursor(); while(1) { Flag(tetris); @@ -550,23 +543,23 @@ void play() // while(1) { - label:PrintTetris(tetris); + loop:Print(tetris); Sleep(tetris->speed); - CleanTetris(tetris); + Clean(tetris); Temp1=tetris->x; Temp2=tetris->flag; //סǰ˹ if(kbhit()) //жǷм룬ch { ch=getch(); - if(ch==75) // 󶯣ĺ2 + if(ch==75) //󶯣ĺ2 { tetris->x-=2; } - if(ch==77) // Ҷĺ2 + if(ch==77) //Ҷĺ2 { tetris->x+=2; } - if(ch==80) // + if(ch==80) // { if(ifMove(tetris)!=0) { @@ -577,7 +570,7 @@ void play() tetris->y=FrameY+Frame_height-2; } } - if(ch==72) // ,ǰ˳ʱת90 + if(ch==72) //,ǰ˳ʱת90 { if( tetris->flag>=2 && tetris->flag<=3 ) //ֱ߷ { @@ -620,33 +613,33 @@ void play() ch=getch(); if(ch == 32) { - goto label; + goto loop; } } } } - if(ch == 27) + if(ch == 27) //esc { system("cls"); memset(a,0,6400*sizeof(int)); //ʼa - welcome(); + main(); } - if(ifMove(tetris)==0) //ɶЧ + if(ifMove(tetris)==0) { tetris->x=Temp1; tetris->flag=Temp2; } else //ɶִв { - goto label; + goto loop; } } - tetris->y++; //ûвָƶ - if(ifMove(tetris)==0) //ƶҲɶڴ˴ + tetris->y++; + if(ifMove(tetris)==0) { tetris->y--; - PrintTetris(tetris); - Del_Fullline(tetris); + Print(tetris); + Full(tetris); break; } } @@ -658,25 +651,25 @@ for(i=tetris->y-2;iy+2;i++) gotoxy(29,7); printf("\n"); color(6); - printf(" @=================================|||||======================================@ \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|* *| \n"); + printf(" @=================================|||||======================================@ \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" |* *| \n"); color(4); - printf("|* *| \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf("|| || \n"); - printf(" @============================================================================@ \n"); + printf(" |* *| \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" || || \n"); + printf(" @============================================================================@ \n"); if(MessageBox(NULL,"Ϸ.\nҪһô?","Game Over",MB_YESNO|MB_ICONQUESTION)==IDYES) { system("cls"); @@ -694,9 +687,8 @@ for(i=tetris->y-2;iy+2;i++) Clean(tetris); } } -/** - * ۼ - */ + +/*ۼ*/ void Clean(struct Tetris *tetris) { for(i=0;i<4;i++) //b[4]4Ԫأѭ4ԪأÿԪصֵΪ0 @@ -716,3 +708,217 @@ void Clean(struct Tetris *tetris) } } } + +/* ˹*/ +void Flag(struct Tetris *tetris) +{ + tetris->number++; //סĸ + srand(time(NULL)); //ʼ + if(tetris->number==1) + { + tetris->flag = rand()%19+1; //סһ + } + tetris->next = rand()%19+1; //סһ +} + +/*жϷǷƶ */ +int ifMove(struct Tetris *tetris) +{ + if(a[tetris->x][tetris->y]!=0) + { + return 0; + } + else + { + if( + ( // + tetris->flag==1 && + ( a[tetris->x][tetris->y+1]==0&& + a[tetris->x+2][tetris->y+1]==0&& + a[tetris->x+2][tetris->y]==0) + ) + || + ( //һ + tetris->flag==2 && + ( a[tetris->x-2][tetris->y]==0&& + a[tetris->x+2][tetris->y]==0&& + a[tetris->x+4][tetris->y]==0) + ) + || + ( //1 + tetris->flag==3 && + ( a[tetris->x][tetris->y-1]==0&& + a[tetris->x][tetris->y+1]==0&& + a[tetris->x][tetris->y-2]==0) + ) + || + ( //T + tetris->flag==4 && + ( a[tetris->x+2][tetris->y]==0&& + a[tetris->x-2][tetris->y]==0&& + a[tetris->x][tetris->y+1]==0) + ) + || + ( //˳90 T + tetris->flag==5 && + ( a[tetris->x-2][tetris->y]==0&& + a[tetris->x][tetris->y-1]==0&& + a[tetris->x][tetris->y+1]==0) + ) + || + ( //˳180 T + tetris->flag==6 && + ( a[tetris->x][tetris->y-1]==0&& + a[tetris->x+2][tetris->y]==0&& + a[tetris->x-2][tetris->y]==0) + ) + || + ( //˳270 T + tetris->flag==7 && + ( a[tetris->x][tetris->y-1]==0&& + a[tetris->x][tetris->y+1]==0&& + a[tetris->x+2][tetris->y]==0) + ) + || + ( //Z + tetris->flag==8 && + ( a[tetris->x-2][tetris->y]==0&& + a[tetris->x][tetris->y+1]==0&& + a[tetris->x+2][tetris->y+1]==0) + ) + || + ( //˳90 Z + tetris->flag==9 && + ( a[tetris->x+2][tetris->y]==0&& + a[tetris->x+2][tetris->y-1]==0&& + a[tetris->x][tetris->y+1]==0) + ) + || + ( // Z + tetris->flag==10 && + ( a[tetris->x+2][tetris->y]==0&& + a[tetris->x][tetris->y+1]==0&& + a[tetris->x-2][tetris->y+1]==0) + ) + || + ( //˳90 Z + tetris->flag==11 && + ( a[tetris->x+2][tetris->y]==0&& + a[tetris->x+2][tetris->y+1]==0&& + a[tetris->x][tetris->y-1]==0) + ) + || + ( //7 + tetris->flag==12 && + ( a[tetris->x-2][tetris->y-1]==0&& + a[tetris->x][tetris->y-1]==0&& + a[tetris->x][tetris->y+1]==0) + ) + || + ( //˳90 7 + tetris->flag==13 && + ( a[tetris->x-2][tetris->y]==0&& + a[tetris->x+2][tetris->y]==0&& + a[tetris->x+2][tetris->y-1]==0) + ) + || + ( //˳180 7 + tetris->flag==14 && + ( a[tetris->x][tetris->y-2]==0&& + a[tetris->x][tetris->y-1]==0&& + a[tetris->x+2][tetris->y]==0) + ) + || + ( //˳270 7 + tetris->flag==15 && + ( a[tetris->x+2][tetris->y]==0&& + a[tetris->x+4][tetris->y]==0&& + a[tetris->x][tetris->y+1]==0) + ) + || + ( // 7 + tetris->flag==16 && + ( a[tetris->x+2][tetris->y-1]==0&& + a[tetris->x][tetris->y+1]==0&& + a[tetris->x][tetris->y-1]==0) + ) + || + ( // ˳90 7 + tetris->flag==17 && + ( a[tetris->x-2][tetris->y]==0&& + a[tetris->x-4][tetris->y]==0&& + a[tetris->x][tetris->y+1]==0) + ) + || + ( //˳180 7 + tetris->flag==18 && + ( a[tetris->x][tetris->y-2]==0&& + a[tetris->x][tetris->y-1]==0&& + a[tetris->x-2][tetris->y]==0) + ) + || + ( //˳270 7 + tetris->flag==19 && + ( a[tetris->x+2][tetris->y]==0&& + a[tetris->x+4][tetris->y]==0&& + a[tetris->x][tetris->y-1]==0) + ) + ) + { + return 1; + } + + + } + return 0; +} +/*жϷǷ */ +void Full(struct Tetris *tetris) +{ + int k,del_rows=0; + for(j=FrameY+Frame_height-1;j>=FrameY+1;j--) + { + k=0; + for(i=FrameX+2;iFrameY;k--) + { + for(i=FrameX+2;iscore+=100*del_rows; + if( del_rows>0 && ( tetris->score%1000==0 || tetris->score/1000>tetris->level-1 ) ) + { + tetris->speed-=20; + tetris->level++; + } +} +