俄罗斯方块的c语言源代码 api实现 VC6.0环境下的俄罗斯方块C语言源代码

\u6c42\u4e00\u4efd\u7528C\u8bed\u8a00\u7f16\u5199\u7684\u4fc4\u7f57\u65af\u65b9\u5757\u7684\u6e90\u4ee3\u7801\uff01

\u4fc4\u7f57\u65af\u65b9\u5757C\u6e90\u4ee3\u7801
#include
#include
#include
#include
#define ZL 4 //\u5750\u6807\u589e\u91cf, \u4e0d\u4f7f\u6e38\u620f\u7a97\u53e3\u9760\u8fb9
#define WID 36 //\u6e38\u620f\u7a97\u53e3\u7684\u5bbd\u5ea6
#define HEI 20 //\u6e38\u620f\u7a97\u53e3\u7684\u9ad8\u5ea6
int i,j,Ta,Tb,Tc; // Ta,Tb,Tc\u7528\u4e8e\u8bb0\u4f4f\u548c\u8f6c\u6362\u65b9\u5757\u53d8\u91cf\u7684\u503c
int a[60][60]={0}; //\u6807\u8bb0\u6e38\u620f\u5c4f\u5e55\u5404\u5750\u6807\u70b9\uff1a0,1,2\u5206\u522b\u4e3a\u7a7a\u3001\u65b9\u5757\u3001\u8fb9\u6846
int b[4]; //\u6807\u8bb04\u4e2a"\u53e3"\u65b9\u5757\uff1a1\u6709\uff0c0\u65e0\uff0c\u7c7b\u4f3c\u5f00\u5173
int x,y, level,score,speed; //\u65b9\u5757\u4e2d\u5fc3\u4f4d\u7f6e\u7684x,y\u5750\u6807\uff0c\u6e38\u620f\u7b49\u7ea7\u3001\u5f97\u5206\u548c\u6e38\u620f\u901f\u5ea6
int flag,next; //\u5f53\u524d\u8981\u64cd\u4f5c\u7684\u65b9\u5757\u7c7b\u578b\u5e8f\u53f7\uff0c\u4e0b\u4e00\u4e2a\u65b9\u5757\u7c7b\u578b\u5e8f\u53f7
void gtxy(int m, int n); //\u4ee5\u4e0b\u58f0\u660e\u8981\u7528\u5230\u7684\u81ea\u7f16\u51fd\u6570
void gflag( ); //\u83b7\u5f97\u4e0b\u4e00\u65b9\u5757\u5e8f\u53f7
void csh( ); //\u521d\u59cb\u5316\u754c\u9762
void start( ); //\u5f00\u59cb\u90e8\u5206
void prfk ( ); //\u6253\u5370\u65b9\u5757
void clfk( ); //\u6e05\u9664\u65b9\u5757
void mkfk( ); //\u5236\u4f5c\u65b9\u5757
void keyD( ); //\u6309\u952e\u64cd\u4f5c
int ifmov( ); //\u5224\u65ad\u65b9\u5757\u80fd\u5426\u79fb\u52a8\u6216\u53d8\u4f53
void clHA( ); //\u6e05\u9664\u6ee1\u884c\u7684\u65b9\u5757
void clNEXT( ); //\u6e05\u9664\u8fb9\u6846\u5916\u7684NEXT\u65b9\u5757
int main( )
{ csh( );
while(1)
{start( ); //\u5f00\u59cb\u90e8\u5206
while(1)
{ prfk( );
Sleep(speed); //\u5ef6\u65f6
clfk( );
Tb=x;Tc=flag; //\u4e34\u5b58\u5f53\u524dx\u5750\u6807\u548c\u5e8f\u53f7\uff0c\u4ee5\u5907\u64a4\u9500\u64cd\u4f5c
keyD( );
y++; //\u65b9\u5757\u5411\u4e0b\u79fb\u52a8
if (ifmov( )==0) { y--; prfk( ); dlHA( ); break;} //\u4e0d\u53ef\u52a8\u653e\u4e0b,\u5220\u884c,\u8de8\u51fa\u5faa\u73af
}
for(i=y-2;i<y+2;i++){ if (i==ZL) { j=0; } } //\u65b9\u5757\u89e6\u5230\u6846\u9876
if (j==0) { system("cls");gtxy(10,10);printf("\u6e38\u620f\u7ed3\u675f\uff01"); getch(); break; }
clNEXT( ); //\u6e05\u9664\u6846\u5916\u7684NEXT\u65b9\u5757
}
return 0;
}
void gtxy(int m, int n) //\u63a7\u5236\u5149\u6807\u79fb\u52a8
{COORD pos; //\u5b9a\u4e49\u53d8\u91cf
pos.X = m; //\u6a2a\u5750\u6807
pos.Y = n; //\u7eb5\u5750\u6807
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void csh( ) //\u521d\u59cb\u5316\u754c\u9762
{gtxy(ZL+WID/2-5,ZL-2); printf("\u4fc4\u7f57\u65af\u65b9\u5757"); //\u6253\u5370\u6e38\u620f\u540d\u79f0
gtxy(ZL+WID+3,ZL+7); printf("******* NEXT\uff1a"); //\u6253\u5370\u83dc\u5355\u4fe1\u606f
gtxy(ZL+WID+3,ZL+13); printf("**********");
gtxy(ZL+WID+3,ZL+15); printf("Esc \uff1a\u9000\u51fa\u6e38\u620f");
gtxy(ZL+WID+3,ZL+17); printf("\u2191\u952e\uff1a\u53d8\u4f53");
gtxy(ZL+WID+3,ZL+19); printf("\u7a7a\u683c\uff1a\u6682\u505c\u6e38\u620f");
gtxy(ZL,ZL); printf("\u2554"); gtxy(ZL+WID-2,ZL); printf("\u2557"); //\u6253\u5370\u6846\u89d2
gtxy(ZL,ZL+HEI); printf("\u255a"); gtxy(ZL+WID-2,ZL+HEI); printf("\u255d");
a[ZL][ZL+HEI]=2; a[ZL+WID-2][ZL+HEI]=2; //\u8bb0\u4f4f\u6709\u56fe\u6848
for(i=2;i<WID-2;i+=2) {gtxy(ZL+i,ZL); printf("\u2550"); } //\u6253\u5370\u4e0a\u6a2a\u6846
for(i=2;i<WID-2;i+=2) {gtxy(ZL+i,ZL+HEI); printf("\u2550"); a[ZL+i][ZL+HEI]=2; } //\u4e0b\u6846
for(i=1;i<HEI;i++) { gtxy(ZL,ZL+i); printf("\u2551"); a[ZL][ZL+i]=2; } //\u5de6\u7ad6\u6846\u8bb0\u4f4f\u6709\u56fe\u6848
for(i=1;i<HEI;i++) {gtxy(ZL+WID-2,ZL+i); printf("\u2551"); a[ZL+WID-2][ZL+i]=2; } //\u53f3\u6846
CONSOLE_CURSOR_INFO cursor_info={1,0}; //\u4ee5\u4e0b\u662f\u9690\u85cf\u5149\u6807\u7684\u8bbe\u7f6e
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
level=1; score=0; speed=400;
gflag( ); flag=next; //\u83b7\u5f97\u4e00\u4e2a\u5f53\u524d\u65b9\u5757\u5e8f\u53f7
}
void gflag( ) //\u83b7\u5f97\u4e0b\u4e00\u4e2a\u65b9\u5757\u7684\u5e8f\u53f7
{ srand((unsigned)time(NULL)); next = rand()%19+1; }
void start( ) //\u5f00\u59cb\u90e8\u5206
{ gflag( ); Ta=flag; flag=next; //\u4fdd\u5b58\u5f53\u524d\u65b9\u5757\u5e8f\u53f7\uff0c\u5c06\u4e0b\u4e00\u65b9\u5757\u5e8f\u53f7\u4e34\u65f6\u64cd\u4f5c
x=ZL+WID+6; y=ZL+10; prfk( ); //\u7ed9x,y\u8d4b\u503c\uff0c\u5728\u6846\u5916\u6253\u5370\u51fa\u4e0b\u4e00\u65b9\u5757
flag=Ta; x=ZL+WID/2; y=ZL-1; //\u53d6\u56de\u5f53\u524d\u65b9\u5757\u5e8f\u53f7\uff0c\u5e76\u7ed9x,y\u8d4b\u503c
}
void prfk ( ) //\u6253\u5370\u4fc4\u7f57\u65af\u65b9\u5757
{ for(i=0;i<4;i++) {b[i]=1; } //\u6570\u7ec4b[4]\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u90fd\u4e3a1
mkfk ( ); //\u5236\u4f5c\u4fc4\u7f57\u65af\u65b9\u5757
for( i= x-2; i<=x+4; i+=2 ) //\u6253\u5370\u65b9\u5757
{ for(j=y-2;jZL ){ gtxy(i,j); printf("\u25a1"); } } }
gtxy(ZL+WID+3,ZL+1); printf("level : %d",level); //\u4ee5\u4e0b\u6253\u5370\u83dc\u5355\u4fe1\u606f
gtxy(ZL+WID+3,ZL+3); printf("score : %d",score);
gtxy(ZL+WID+3,ZL+5); printf("speed : %d",speed);
}
void clfk( ) //\u6e05\u9664\u4fc4\u7f57\u65af\u65b9\u5757
{ for(i=0;i<4;i++) { b[i]=0; } //\u6570\u7ec4b[4]\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u90fd\u4e3a0
mkfk ( ); //\u5236\u4f5c\u4fc4\u7f57\u65af\u65b9\u5757
for( i=x-2; i<=x+4; i+=2 ) //\u6e05\u9664\u65b9\u5757
{ for(j=y-2;jZL ){ gtxy(i,j); printf(" "); } } }
}
void mkfk( ) //\u5236\u4f5c\u4fc4\u7f57\u65af\u65b9\u5757
{ a[x][ y]=b[0]; //\u65b9\u5757\u4e2d\u5fc3\u4f4d\u7f6e\u72b6\u6001: 1-\u6709,0-\u65e0
switch(flag) //\u51716\u5927\u7c7b\uff0c19\u79cd\u5c0f\u7c7b\u578b
{ case 1: { a[x][y-1]=b[1]; a[x+2][y-1]=b[2]; a[x+2][y]=b[3]; break; } //\u7530\u5b57\u65b9\u5757
case 2: { a[x-2][y]=b[1]; a[x+2][y]=b[2]; a[x+4][y]=b[3]; break; } //\u76f4\u7ebf\u65b9\u5757:----
case 3: { a[x][y-1]=b[1]; a[x][y-2]=b[2]; a[x][y+1]=b[3]; break; } //\u76f4\u7ebf\u65b9\u5757: |
case 4: { a[x-2][y]=b[1]; a[x+2][y]=b[2]; a[x][y+1]=b[3]; break; } //T\u5b57\u65b9\u5757
case 5: { a[x][y-1]=b[1]; a[x][y+1]=b[2]; a[x-2][y]=b[3]; break; } //T\u5b57\u987a\u65f6\u9488\u8f6c90\u5ea6
case 6: { a[x][y-1]=b[1]; a[x-2][y]=b[2]; a[x+2][y]=b[3]; break; } //T\u5b57\u987a\u8f6c180\u5ea6
case 7: { a[x][y-1]=b[1]; a[x][y+1]=b[2]; a[x+2][y]=b[3]; break; } //T\u5b57\u987a\u8f6c270\u5ea6
case 8: { a[x][y+1]=b[1]; a[x-2][y]=b[2]; a[x+2][y+1]=b[3]; break; } //Z\u5b57\u65b9\u5757
case 9: { a[x][y-1]=b[1]; a[x-2][y]=b[2]; a[x-2][y+1]=b[3]; break; } //Z\u5b57\u987a\u8f6c90\u5ea6
case 10: { a[x][y-1]=b[1]; a[x-2][y-1]=b[2]; a[x+2][y]=b[3]; break; } //Z\u5b57\u987a\u8f6c180\u5ea6
case 11: { a[x][y+1]=b[1]; a[x+2][y-1]=b[2]; a[x+2][ y]=b[3]; break; } //Z\u5b57\u987a\u8f6c270\u5ea6
case 12: { a[x][y-1]=b[1]; a[x][y+1]=b[2]; a[x-2][y-1]=b[3]; break; } //7\u5b57\u65b9\u5757
case 13: {a[x-2][y]=b[1]; a[x+2][y-1]=b[2]; a[x+2][y]=b[3]; break; } //7\u5b57\u987a\u8f6c90\u5ea6
case 14: { a[x][y-1]=b[1]; a[x][y+1]=b[2]; a[x+2][y+1]=b[3]; break; } //7\u5b57\u987a\u8f6c180\u5ea6
case 15: { a[x-2][y]=b[1]; a[x-2][y+1]=b[2]; a[x+2][y]=b[3]; break; } //7\u5b57\u987a\u8f6c270\u5ea6
case 16: { a[x][y+1]=b[1]; a[x][y-1]=b[2]; a[x+2][y-1]=b[3]; break; } //\u50127\u5b57\u65b9\u5757
case 17: { a[x-2][y]=b[1]; a[x+2][y+1]=b[2]; a[x+2][y]=b[3]; break; } //\u50127\u5b57\u987a\u8f6c90\u5ea6
case 18: { a[x][y-1]=b[1]; a[x][y+1]=b[2]; a[x-2][y+1]=b[3]; break; } //\u50127\u5b57\u987a\u8f6c180\u5ea6
case 19: { a[x-2][y]=b[1]; a[x-2][y-1]=b[2]; a[x+2][y]=b[3]; break; } //\u50127\u5b57\u987a\u8f6c270\u5ea6
}
}
void keyD( ) //\u6309\u952e\u64cd\u4f5c
{ if (kbhit( ))
{ int key;
key=getch();
if (key==224)
{ key=getch();
if (key==75) { x-=2; } //\u6309\u4e0b\u5de6\u65b9\u5411\u952e\uff0c\u4e2d\u5fc3\u6a2a\u5750\u6807\u51cf2
if (key==77) { x+=2; } //\u6309\u4e0b\u53f3\u65b9\u5411\u952e\uff0c\u4e2d\u5fc3\u6a2a\u5750\u6807\u52a02
if (key==72) //\u6309\u4e0b\u5411\u4e0a\u65b9\u5411\u952e,\u65b9\u5757\u53d8\u4f53
{ if (flag>=2 && flag<=3 ) { flag++; flag%=2; flag+=2; }
if ( flag>=4 && flag<=7 ) { flag++; flag%=4; flag+=4; }
if (flag>=8 && flag<=11 ) { flag++; flag%=4; flag+=8; }
if (flag>=12 && flag<=15 ) { flag++; flag%=4; flag+=12; }
if ( flag>=16 && flag<=19 ) { flag++; flag%=4; flag+=16; } }
}
if (key==32) //\u6309\u7a7a\u683c\u952e\uff0c\u6682\u505c
{ prfk( ); while(1) { if (getch( )==32) { clfk( );break;} } } //\u518d\u6309\u7a7a\u683c\u952e\uff0c\u7ee7\u7eed\u6e38\u620f
if (ifmov( )==0) { x=Tb; flag=Tc; } //\u5982\u679c\u4e0d\u53ef\u52a8\uff0c\u64a4\u9500\u4e0a\u9762\u64cd\u4f5c
else { prfk( ); Sleep(speed); clfk( ); Tb=x;Tc=flag;} //\u5982\u679c\u53ef\u52a8\uff0c\u6267\u884c\u64cd\u4f5c
}
}
int ifmov( ) //\u5224\u65ad\u80fd\u5426\u79fb\u52a8
{ if (a[x][y]!=0) { return 0; } //\u65b9\u5757\u4e2d\u5fc3\u5904\u6709\u56fe\u6848\u8fd4\u56de0\uff0c\u4e0d\u53ef\u79fb\u52a8
else{ if ( (flag==1 && ( a[x][ y-1]==0 && a[x+2][y-1]==0 && a[x+2][y]==0 ) ) ||
(flag==2 && ( a[x-2][y]==0 && a[x+2][y]==0 && a[x+4][y]==0 ) ) ||
(flag==3 && ( a[x][y-1]==0 && a[x][y-2]==0 && a[x][y+1]==0 ) ) ||
(flag==4 && ( a[x-2][y]==0 && a[x+2][y]==0 && a[x][y+1]==0 ) ) ||
(flag==5 && ( a[x][y-1]==0 && a[x][y+1]==0 && a[x-2][y]==0 ) ) ||
(flag==6 && ( a[x][ y-1]==0 && a[x-2][y]==0 && a[x+2][y]==0 ) ) ||
(flag==7 && ( a[x][y-1]==0 && a[x][y+1]==0 && a[x+2][y]==0 ) ) ||
(flag==8 && ( a[x][y+1]==0 && a[x-2][y]==0 && a[x+2][y+1]==0 ) ) ||
(flag==9 && ( a[x][y-1]==0 && a[x-2][y]==0 && a[x-2][y+1]==0 ) ) ||
(flag==10 && ( a[x][y-1]==0 && a[x-2][y-1]==0 && a[x+2][y]==0 ) ) ||
(flag==11 && ( a[x][y+1]==0 && a[x+2][y-1]==0 && a[x+2][y]==0 ) ) ||
(flag==12 && ( a[x][y-1]==0 && a[x][y+1]==0 && a[x-2][y-1]==0 ) ) ||
( flag==13 && ( a[x-2][y]==0 && a[x+2][y-1]==0 && a[x+2][y]==0 ) ) ||
( flag==14 && ( a[x][y-1]==0 && a[x][y+1]==0 && a[x+2][y+1]==0 ) ) ||
(flag==15 && ( a[x-2][y]==0 && a[x-2][y+1]==0 && a[x+2][y]==0 ) ) ||
(flag==16 && ( a[x][y+1]==0 && a[x][y-1]==0 && a[x+2][y-1]==0 ) ) ||
( flag==17 && ( a[x-2][y]==0 && a[x+2][y+1]==0 && a[x+2][y]==0 ) ) ||
(flag==18 && ( a[x][y-1]==0 &&a[x][y+1]==0 && a[x-2][y+1]==0 ) ) ||
(flag==19 && ( a[x-2][y]==0 && a[x-2][y-1]==0
&& a[x+2][y]==0 ) ) ) { return 1; }
}
return 0; //\u5176\u5b83\u60c5\u51b5\u8fd4\u56de0
}
void clNEXT( ) //\u6e05\u9664\u6846\u5916\u7684NEXT\u65b9\u5757
{ flag = next; x=ZL+WID+6; y=ZL+10; clfk( ); }
void clHA( ) //\u6e05\u9664\u6ee1\u884c\u7684\u65b9\u5757
{ int k, Hang=0; //k\u662f\u67d0\u884c\u65b9\u5757\u4e2a\u6570, Hang\u662f\u5220\u9664\u7684\u65b9\u5757\u884c\u6570
for(j=ZL+HEI-1;j>=ZL+1;j--) //\u5f53\u67d0\u884c\u6709WID/2-2\u4e2a\u65b9\u5757\u65f6\uff0c\u5219\u4e3a\u6ee1\u884c
{ k=0; for(i=ZL+2;i<ZL+WID-2;i+=2)
{ if (a[i][j]==1) //\u7ad6\u5750\u6807\u4ece\u4e0b\u5f80\u4e0a\uff0c\u6a2a\u5750\u6807\u7531\u5de6\u81f3\u53f3\u4f9d\u6b21\u5224\u65ad\u662f\u5426\u6ee1\u884c
{ k++; //\u4e0b\u9762\u5c06\u64cd\u4f5c\u5220\u9664\u884c
if (k==WID/2-2) { for(k=ZL+2;k<ZL+WID-2;k+=2)
{ a[k][j]=0; gtxy(k,j); printf(" "); Sleep(1); }
for(k=j-1;k>ZL;k--)
{ for(i=ZL+2;i<ZL+WID-2;i+=2) //\u5df2\u5220\u884c\u6570\u4e0a\u9762\u6709\u65b9\u5757\uff0c\u5148\u6e05\u9664\u518d\u5168\u90e8\u4e0b\u79fb\u4e00\u884c
{ if(a[i][k]==1) { a[i][k]=0; gtxy(i,k); printf(" ");a[i][k+1]=1;
gtxy(i,k+1); printf("\u25a1"); } }
}
j++; //\u65b9\u5757\u4e0b\u79fb\u540e\uff0c\u91cd\u65b0\u5224\u65ad\u5220\u9664\u884c\u662f\u5426\u6ee1\u884c
Hang++; //\u8bb0\u5f55\u5220\u9664\u65b9\u5757\u7684\u884c\u6570
}
}
}
}
score+=100*Hang; //\u6bcf\u5220\u9664\u4e00\u884c\uff0c\u5f97100\u5206
if ( Hang>0 && (score%500==0 || score/500> level-1 ) ) //\u5f97\u5206\u6ee1500\u901f\u5ea6\u52a0\u5feb\u5347\u4e00\u7ea7
{ speed-=20; level++; if(speed<200)speed+=20; }
}

#include
#include
#include
#define mDRAW 5
#define mLINE 6
#define mADOWN 7
#define mGEN 8
#define mLEFT 75
#define mRIGHT 77
#define mSPACE 57
#define mDOWN 80
#define mESC 1
#define TIMEINT 2
#define MAXX 9
#define MAXY 30
#define BACKCOLOR BLACK
#define WINX 50
#define WINY 470
#define GAP 6
#define AREAX (WINX+GAP)
#define AREAY (WINY-GAP)
#define BOXW 15

int oldarea[MAXY+1][MAXX];
int area[MAXY+1][MAXX];
int actW,actH,actX,actY;
int curX,curY,curColor,curW,curH;
int newX,newY,newColor,newW,newH;
int active;
int box[4][4];
int FORCOLOR;
int MESSAGE;
int BOX[7][4][4]={
{
{1,1,1,1},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,1,0},
{1,0,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,1,0},
{0,0,1,0},
{0,0,0,0},
{0,0,0,0}

},{
{1,1,1,0},
{0,1,0,0},
{0,0,0,0},
{0,0,0,0}
},{
{1,1,0,0},
{0,1,1,0},
{0,0,0,0},
{0,0,0,0}
},{
{0,1,1,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}
}
};


void init();
void draw();
int genBox();
int getKey();
void lineFull();
int moveLeft();
int moveRight();
int moveDown();
int rotate();
int getW();
int getH();
void clearOldBox();
void putNewBox();
int collisionRotate(int box[][4]);
void getMessage();
void dispatchMessage();
int timeCome();
void fallDown();
int gameOver();

main()
{
int i;
init();
do
{
getMessage();
dispatchMessage();
}
while(!gameOver());

getch();
closegraph();
}

void getMessage()
{
if(MESSAGE) return;
if(timeCome())
{
MESSAGE=mADOWN;
return;
}
if(bioskey(1))
{
MESSAGE=bioskey(0)>>8;
return;
}
}

void dispatchMessage()
{
switch(MESSAGE)
{
case mLEFT: moveLeft();break;
case mRIGHT: moveRight();break;
case mADOWN: moveDown();break;
case mSPACE: rotate();break;
case mDOWN: fallDown(); break;
case mDRAW: draw();break;
case mLINE: lineFull();break;
case mGEN: genBox();break;
case mESC: closegraph(); exit(0);
default: MESSAGE=0;
}
}

void fallDown()
{
while(active)
{
moveDown(); draw();
}

MESSAGE=mLINE;
}

int timeCome()
{

static long tm, old;
tm=biostime(0,tm);
if(tm-old<TIMEINT) return 0;
else
{
old=tm; return 1;
}
}

void init()
{
int i,j,x1,y1,x2,y2;
int driver=DETECT, mode=0;
randomize();
registerbgidriver(EGAVGA_driver);
initgraph(&driver,&mode,"");
cleardevice();
setfillstyle(SOLID_FILL,BLUE);
bar(0,0,639,479);
x1=AREAX;
y1=AREAY-BOXW*MAXY;
x2=AREAX+MAXX*BOXW;
y2=AREAY;
rectangle(--x1,--y1,++x2,++y2);
setfillstyle(SOLID_FILL,BLACK);
bar(++x1,++y1,--x2,--y2);
y1=AREAY-MAXY*BOXW; y2=AREAY;
setcolor(DARKGRAY);
for(i=0;i<MAXX;i++)
{
x1=AREAX+i*BOXW;
line(x1,y1,x1,y2);
}

x1=AREAX; x2=x1+MAXX*BOXW;
for(j=0;j<MAXY;j++)
{
y1=AREAY-j*BOXW;
line(x1,y1,x2,y1);
}
for(j=0;j<MAXY;j++)
for(i=0;i<MAXX;i++)
area[j][i]=oldarea[j][i]=0;
actX=0; actY=0; actW=MAXX-1; actH=MAXY-1;
draw();
MESSAGE=mGEN;
}

int genBox()
{
int i,j,boxidx;
boxidx=random(7); FORCOLOR=random(7)+1;
for(j=0;j<4;j++)
for(i=0;i<4;i++)
box[j][i]=BOX[boxidx][j][i];
curW=getW(); curH=getH();
curX=(MAXX+curW)/2;
if(curX+curW>=MAXX)curX=MAXX-1-curW;
curY=MAXY-1-curH;
newX=curX; newY=curY; actX=curX;actY=curY;
actW=newW=curW; actH=newH=curH;
active=1;
if(collision(box)) return 0;
putNewBox();
draw(); MESSAGE=0;
return 1;
}

void lineFull()
{
int row,col, rowEnd,full,i,j;
rowEnd=newY+newH;
if(rowEnd>=MAXY-1) rowEnd=MAXY-2;
for(row=newY; row<=rowEnd;)
{
full=1;
for(col=0;col<MAXX;col++)
if(!area[row][col]){full=0; break;}
if(!full){++row; continue;}
for(j=row; j<MAXY-1;j++)
for(i=0;i<MAXX;i++)
area[j][i]=area[j+1][i];
actX=0;actY=row; actW=MAXX-1; actH=MAXY-1-row;
draw(); rowEnd--;
}
MESSAGE=mGEN;
}

void draw()
{
int row,col,x1,y1,x2,y2;
for(row=actY;row<=actY+actH;row++)
for(col=actX;col<=actX+actW;col++)
if(area[row][col]!=oldarea[row][col])
{
if(area[row][col]==0)
setfillstyle(SOLID_FILL,BACKCOLOR);
else
setfillstyle(SOLID_FILL,FORCOLOR);

x1=AREAX+col*BOXW; x2=x1+BOXW;
y1=AREAY-(row+1)*BOXW; y2=y1+BOXW;
bar(++x1,++y1,--x2,--y2);
oldarea[row][col]=area[row][col];
}

MESSAGE=0;
}

int moveLeft()
{
newX=curX-1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveRight()
{
newX=curX+1; clearOldBox();
if(collision(box))
{
newX=curX;
putNewBox();
MESSAGE=0;
return 0;
}
putNewBox();
actW=curW+1; actX=curX; curX=newX;
MESSAGE=mDRAW;
return 1;
}

int moveDown()
{
int i,j;
newY=curY-1;
clearOldBox();
if(collision(box))
{
newY=curY;
putNewBox();
active=0;
MESSAGE=mLINE;
return 0;
}
putNewBox();
actH=curH+1; actY=newY; curY=newY;
MESSAGE=mDRAW;
return 1;
}

int rotate()
{
int newBox[4][4];
int i,j;
clearOldBox();

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[j][i]=0;

for(j=0;j<4;j++)
for(i=0;i<4;i++)
newBox[curW-i][j]=box[j][i];
newW=curH; newH=curW;
if(collisionRotate(newBox))
{
newW=curW; newH=curH; newX=curX; newY=curY;
putNewBox();
MESSAGE=0;
return 0;
}

for(j=0;j<4;j++)
for(i=0;i<4;i++)
box[j][i]=newBox[j][i];
putNewBox();
actH=newH>curH? newH:curH;
actW=curX+actH-newX;
actX=newX; actY=newY; curX=newX;
curY=newY; curW=newW; curH=newH;
MESSAGE=mDRAW;
return 1;
}

int getW()
{
int i,j;
for(i=3;i>0;i--)
for(j=0;j<4;j++)
if(box[j][i]) return i;
return 0;
}

int getH()
{
int i,j;
for(j=3;j>0;j--)
for(i=0;i<4;i++)
if(box[j][i]) return j;
return 0;
}


void clearOldBox()
{
int i,j;
for(j=0;j<=curH; j++)
for(i=0;i<=curW; i++)
if(box[j][i])
area[curY+j][curX+i]=0;
}

void putNewBox()
{
int i,j;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(box[j][i])
area[newY+j][newX+i]=FORCOLOR;
}

int collision(int cbox[][4])
{
int i,j;
if(newX<0) return 1;
if(newX+newW>=MAXX) return 1;
if(newY<0) return 1;
for(j=0;j<=newH;j++)
for(i=0;i<=newW;i++)
if(area[newY+j][newX+i]&&cbox[j][i]) return 1;
return 0;
}

int collisionRotate(int cbox[][4])
{
int i,j;
if(newX+newW>=MAXX) newX=MAXX-1-newW;
if(newY+newH>=MAXY) newY=MAXY-1-newH;
if(collision(cbox)) return 1;
for(i=0;i<=newW;i++)
for(j=0;j<=newH;j++)
if(area[newY+j][newX+i])
{
newX-=newW-i+1; goto L;
}
L: return collision(cbox);
}

int gameOver()
{
if(!active &&(curY+curH>MAXY-3)) return 1;
else return 0;
}

TC下面的

/************************************
* Desc: 俄罗斯方块游戏
* By: hoodlum1980
* Email: [email protected]
* Date: 2008.03.12 22:30
************************************/
#include <stdio.h>
#include <bios.h>
#include <dos.h>
#include <graphics.h>
#include <string.h>
#include <stdlib.h>
#define true 1
#define false 0
#define BoardWidth 12
#define BoardHeight 23
#define _INNER_HELPER /*inner helper method */
/*Scan Codes Define*/
enum KEYCODES
{
K_ESC =0x011b,
K_UP =0x4800, /* upward arrow */
K_LEFT =0x4b00,
K_DOWN =0x5000,
K_RIGHT =0x4d00,
K_SPACE =0x3920,
K_P =0x1970
};

/* the data structure of the block */
typedef struct tagBlock
{
char c[4][4]; /* cell fill info array, 0-empty, 1-filled */
int x; /* block position cx [ 0,BoardWidht -1] */
int y; /* block position cy [-4,BoardHeight-1] */
char color; /* block color */
char size; /* block max size in width or height */
char name; /* block name (the block's shape) */
} Block;

/* game's global info */
int FrameTime= 1300;
int CellSize= 18;
int BoardLeft= 30;
int BoardTop= 30;

/* next block grid */
int NBBoardLeft= 300;
int NBBoardTop= 30;
int NBCellSize= 10;

/* score board position */
int ScoreBoardLeft= 300;
int ScoreBoardTop=100;
int ScoreBoardWidth=200;
int ScoreBoardHeight=35;
int ScoreColor=LIGHTCYAN;

/* infor text postion */
int InfoLeft=300;
int InfoTop=200;
int InfoColor=YELLOW;

int BorderColor=DARKGRAY;
int BkGndColor=BLACK;
int GameRunning=true;
int TopLine=BoardHeight-1; /* top empty line */
int TotalScore=100;
char info_score[20];
char info_help[255];
char info_common[255];

/* our board, Board[x][y][0]-isFilled, Board[x][y][1]-fillColor */
unsigned char Board[BoardWidth][BoardHeight][2];
char BufferCells[4][4]; /* used to judge if can rotate block */
Block curBlock; /* current moving block */
Block nextBlock; /* next Block to appear */

/* function list */
int GetKeyCode();
int CanMove(int dx,int dy);
int CanRotate();
int RotateBlock(Block *block);
int MoveBlock(Block *block,int dx,int dy);
void DrawBlock(Block *block,int,int,int);
void EraseBlock(Block *block,int,int,int);
void DisplayScore();
void DisplayInfo(char* text);
void GenerateBlock(Block *block);
void NextBlock();
void InitGame();
int PauseGame();
void QuitGame();

/*Get Key Code */
int _INNER_HELPER GetKeyCode()
{
int key=0;
if(bioskey(1))
{
key=bioskey(0);
}
return key;
}

/* display text! */
void _INNER_HELPER DisplayInfo(char *text)
{
setcolor(BkGndColor);
outtextxy(InfoLeft,InfoTop,info_common);
strcpy(info_common,text);
setcolor(InfoColor);
outtextxy(InfoLeft,InfoTop,info_common);
}

/* create a new block by key number,
* the block anchor to the top-left corner of 4*4 cells
*/
void _INNER_HELPER GenerateBlock(Block *block)
{
int key=(random(13)*random(17)+random(1000)+random(3000))%7;
block->size=3;/* because most blocks' size=3 */
memset(block->c,0,16);
switch(key)
{
case 0:
block->name='T';
block->color=RED;
block->c[1][0]=1;
block->c[1][1]=1, block->c[2][1]=1;
block->c[1][2]=1;
break;
case 1:
block->name='L';
block->color=YELLOW;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1, block->c[2][2]=1;
break;
case 2:
block->name='J';
block->color=LIGHTGRAY;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1, block->c[0][2]=1;
break;
case 3:
block->name='z';
block->color=CYAN;
block->c[0][0]=1, block->c[1][0]=1;
block->c[1][1]=1, block->c[2][1]=1;
break;
case 4:
block->name='5';
block->color=LIGHTBLUE;
block->c[1][0]=1, block->c[2][0]=1;
block->c[0][1]=1, block->c[1][1]=1;
break;
case 5:
block->name='o';
block->color=BLUE;
block->size=2;
block->c[0][0]=1, block->c[1][0]=1;
block->c[0][1]=1, block->c[1][1]=1;
break;
case 6:
block->name='I';
block->color=GREEN;
block->size=4;
block->c[1][0]=1;
block->c[1][1]=1;
block->c[1][2]=1;
block->c[1][3]=1;
break;
}
}

/* get next block! */
void NextBlock()
{
/* copy the nextBlock to curBlock */
curBlock.size=nextBlock.size;
curBlock.color=nextBlock.color;
curBlock.x=(BoardWidth-4)/2;
curBlock.y=-curBlock.size;
memcpy(curBlock.c,nextBlock.c,16);
/* generate nextBlock and show it */
EraseBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
GenerateBlock(&nextBlock);
nextBlock.x=1,nextBlock.y=0;
DrawBlock(&nextBlock,NBBoardLeft,NBBoardTop,NBCellSize);
}

/* rotate the block, update the block struct data */
int _INNER_HELPER RotateCells(char c[4][4],char blockSize)
{
char temp,i,j;
switch(blockSize)
{
case 3:
temp=c[0][0];
c[0][0]=c[2][0], c[2][0]=c[2][2], c[2][2]=c[0][2], c[0][2]=temp;
temp=c[0][1];
c[0][1]=c[1][0], c[1][0]=c[2][1], c[2][1]=c[1][2], c[1][2]=temp;
break;
case 4: /* only 'I' block arived here! */
c[1][0]=1-c[1][0], c[1][2]=1-c[1][2], c[1][3]=1-c[1][3];
c[0][1]=1-c[0][1], c[2][1]=1-c[2][1], c[3][1]=1-c[3][1];
break;
}
}

/* judge if the block can move toward the direction */
int CanMove(int dx,int dy)
{
int i,j,tempX,tempY;
for(i=0;i<curBlock.size;i++)
{
for(j=0;j<curBlock.size;j++)
{
if(curBlock.c[i][j])
{
/* cannot move leftward or rightward */
tempX = curBlock.x + i + dx;
if(tempX<0 || tempX>(BoardWidth-1)) return false; /* make sure x is valid! */
/* cannot move downward */
tempY = curBlock.y + j + dy;
if(tempY>(BoardHeight-1)) return false; /* y is only checked lower bound, maybe negative!!!! */
/* the cell already filled, we must check Y's upper bound before check cell ! */
if(tempY>=0 && Board[tempX][tempY][0]) return false;
}
}
}
return true;
}

/* judge if the block can rotate */
int CanRotate()
{
int i,j,tempX,tempY;
/* update buffer */
memcpy(BufferCells, curBlock.c, 16);
RotateCells(BufferCells,curBlock.size);
for(i=0;i<curBlock.size;i++)
{
for(j=0;j<curBlock.size;j++)
{
if(BufferCells[i][j])
{
tempX=curBlock.x+i;
tempY=curBlock.y+j;
if(tempX<0 || tempX>(BoardWidth-1))
return false;
if(tempY>(BoardHeight-1))
return false;
if(tempY>=0 && Board[tempX][tempY][0])
return false;
}
}
}
return true;
}

/* draw the block */
void _INNER_HELPER DrawBlock(Block *block,int bdLeft,int bdTop,int cellSize)
{
int i,j;
setfillstyle(SOLID_FILL,block->color);
for(i=0;i<block->size;i++)
{
for(j=0;j<block->size;j++)
{
if(block->c[i][j] && (block->y+j)>=0)
{
floodfill(
bdLeft+cellSize*(i+block->x)+cellSize/2,
bdTop+cellSize*(j+block->y)+cellSize/2,
BorderColor);
}
}
}
}

/* Rotate the block, if success, return true */
int RotateBlock(Block *block)
{
char temp,i,j;
int b_success;
if(block->size==2)
return true;
if(( b_success=CanRotate()))
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
memcpy(curBlock.c,BufferCells,16);
DrawBlock(block,BoardLeft,BoardTop,CellSize);
}
return b_success;
}

/* erase a block, only fill the filled cell with background color */
void _INNER_HELPER EraseBlock(Block *block,int bdLeft,int bdTop,int cellSize)
{
int i,j;
setfillstyle(SOLID_FILL,BkGndColor);
for(i=0;i<block->size;i++)
{
for(j=0;j<block->size;j++)
{
if(block->c[i][j] && (block->y+j>=0))
{
floodfill(
bdLeft+cellSize*(i+block->x)+cellSize/2,
bdTop+cellSize*(j+block->y)+cellSize/2,
BorderColor);
}
}
}
}

/* move by the direction if can, donothing if cannot
* return value: true - success, false - cannot move toward this direction
*/
int MoveBlock(Block *block,int dx,int dy)
{
int b_canmove=CanMove(dx,dy);
if(b_canmove)
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
curBlock.x+=dx;
curBlock.y+=dy;
DrawBlock(block,BoardLeft,BoardTop,CellSize);
}
return b_canmove;
}

/* drop the block to the bottom! */
int DropBlock(Block *block)
{
EraseBlock(block,BoardLeft,BoardTop,CellSize);
while(CanMove(0,1))
{
curBlock.y++;
}
DrawBlock(block,BoardLeft,BoardTop,CellSize);
return 0;/* return value is assign to the block's alive */
}

/* init the graphics mode, draw the board grid */
void InitGame()
{
int i,j,gdriver=DETECT,gmode;
struct time sysTime;
/* draw board cells */
memset(Board,0,BoardWidth*BoardHeight*2);
memset(nextBlock.c,0,16);
strcpy(info_help,"P: Pause Game. --by hoodlum1980");
initgraph(&gdriver,&gmode,"");
setcolor(BorderColor);
for(i=0;i<=BoardWidth;i++)
{
line(BoardLeft+i*CellSize, BoardTop, BoardLeft+i*CellSize, BoardTop+ BoardHeight*CellSize);
}
for(i=0;i<=BoardHeight;i++)
{
line(BoardLeft, BoardTop+i*CellSize, BoardLeft+BoardWidth*CellSize, BoardTop+ i*CellSize);
}
/* draw board outer border rect */
rectangle(BoardLeft-CellSize/4, BoardTop-CellSize/4,
BoardLeft+BoardWidth*CellSize+CellSize/4,
BoardTop+BoardHeight*CellSize+CellSize/4);

/* draw next block grids */
for(i=0;i<=4;i++)
{
line(NBBoardLeft+i*NBCellSize, NBBoardTop, NBBoardLeft+i*NBCellSize, NBBoardTop+4*NBCellSize);
line(NBBoardLeft, NBBoardTop+i*NBCellSize, NBBoardLeft+4*NBCellSize, NBBoardTop+ i*NBCellSize);
}

/* draw score rect */
rectangle(ScoreBoardLeft,ScoreBoardTop,ScoreBoardLeft+ScoreBoardWidth,ScoreBoardTop+ScoreBoardHeight);
DisplayScore();

/* set new seed! */
gettime(&sysTime);
srand(sysTime.ti_hour*3600+sysTime.ti_min*60+sysTime.ti_sec);

GenerateBlock(&nextBlock);
NextBlock(); /* create first block */
setcolor(DARKGRAY);
outtextxy(InfoLeft,InfoTop+20,"Up -rotate Space-drop");
outtextxy(InfoLeft,InfoTop+35,"Left-left Right-right");
outtextxy(InfoLeft,InfoTop+50,"Esc -exit");
DisplayInfo(info_help);
}

/* set the isFilled and fillcolor data to the board */
void _INNER_HELPER FillBoardData()
{
int i,j;
for(i=0;i<curBlock.size;i++)
{
for(j=0;j<curBlock.size;j++)
{
if(curBlock.c[i][j] && (curBlock.y+j)>=0)
{
Board[curBlock.x+i][curBlock.y+j][0]=1;
Board[curBlock.x+i][curBlock.y+j][1]=curBlock.color;
}
}
}
}

/* draw one line of the board */
void _INNER_HELPER PaintBoard()
{
int i,j,fillcolor;
for(j=max((TopLine-4),0);j<BoardHeight;j++)
{
for(i=0;i<BoardWidth;i++)
{
fillcolor=Board[i][j][0]? Board[i][j][1]:BkGndColor;
setfillstyle(SOLID_FILL,fillcolor);
floodfill(BoardLeft+i*CellSize+CellSize/2,BoardTop+j*CellSize+CellSize/2,BorderColor);
}
}
}

/* check if one line if filled full and increase the totalScore! */
void _INNER_HELPER CheckBoard()
{
int i,j,k,score=10,sum=0,topy,lines=0;
/* we find the top empty line! */
j=topy=BoardHeight-1;
do
{
sum=0;
for(i=0;i< BoardWidth; i++)
{
sum+=Board[i][topy][0];
}
topy--;
} while(sum>0 && topy>0);

/* remove the full filled line (max remove lines count = 4) */
do
{
sum=0;
for(i=0;i< BoardWidth; i++)
sum+=Board[i][j][0];

if(sum==BoardWidth)/* we find this line is full filled, remove it! */
{
/* move the cells data down one line */
for(k=j; k > topy;k--)
{
for(i=0;i<BoardWidth;i++)
{
Board[i][k][0]=Board[i][k-1][0];
Board[i][k][1]=Board[i][k-1][1];
}
}
/*make the top line empty! */
for(i=0;i<BoardWidth;i++)
{
Board[i][topy][0]=0;
Board[i][topy][1]=0;
}
topy++; /* move the topline downward one line! */
lines++; /* lines <=4 */
TotalScore+=score;
score*=2; /* adding: 10, 30, 70, 150 */
}
else
j--;
} while(sum>0 && j>topy && lines<4);
/* speed up the game when score is high, minimum is 400 */
FrameTime=max(1200-100*(TotalScore/200), 400);
TopLine=topy;/* update the top line */
/* if no lines remove, only add 1: */
if(lines==0)
TotalScore++;
}

/* display the score */
void _INNER_HELPER DisplayScore()
{
setcolor(BkGndColor);
outtextxy(ScoreBoardLeft+5,ScoreBoardTop+5,info_score);
setcolor(ScoreColor);
sprintf(info_score,"Score: %d",TotalScore);
outtextxy(ScoreBoardLeft+5,ScoreBoardTop+5,info_score);
}

/* we call this function when a block is inactive. */
void UpdateBoard()
{
FillBoardData();
CheckBoard();
PaintBoard();
DisplayScore();
}

/* pause the game, and timer handler stop move down the block! */
int PauseGame()
{
int key=0;
DisplayInfo("Press P to Start or Resume!");
while(key!=K_P && key!=K_ESC)
{
while(!(key=GetKeyCode())){}
}
DisplayInfo(info_help);
return key;
}

/* quit the game and do cleaning work. */
void QuitGame()
{
closegraph();
}

/* the entry point function. */
void main()
{
int i,flag=1,j,key=0,tick=0;
InitGame();
if(PauseGame()==K_ESC)
goto GameOver;
/* wait until a key pressed */
while(key!=K_ESC)
{
/* wait until a key pressed */
while(!(key=GetKeyCode()))
{
tick++;
if(tick>=FrameTime)
{
/* our block has dead! (can't move down), we get next block */
if(!MoveBlock(&curBlock,0,1))
{
UpdateBoard();
NextBlock();
if(!CanMove(0,1))
goto GameOver;
}
tick=0;
}
delay(100);
}
switch(key)
{
case K_LEFT:
MoveBlock(&curBlock,-1,0);
break;
case K_RIGHT:
MoveBlock(&curBlock,1,0);
break;
case K_DOWN:
MoveBlock(&curBlock,0,1);
break;
case K_UP:
RotateBlock(&curBlock);
break;
case K_SPACE:
DropBlock(&curBlock);
break;
case K_P:
PauseGame();
break;
}
}
GameOver:
DisplayInfo("GAME OVER! Press any key to exit!");
getch(); /* wait the user Press any key. */
QuitGame();
}

是DX吧

扩展阅读:俄罗斯方块免费版 ... 老式俄罗斯方块 ... c语言代码生成器 ... 代码生成器 ... 俄罗斯方块代码大全 ... python基础代码大全 ... c语言必背100代码 ... c写俄罗斯方块代码 ... 俄罗斯方块游戏代码python ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网