srand(time(0)); /*对随机数函数rand()进行初始化*/ zt1=rand()%7+1; /*生成第一、二个方块*/ if(zt1==2) zt2=1; if(zt1==1||zt1==4||zt1==5) zt2=rand()%2+1; if(zt1==3||zt1==6||zt1==7) zt2=rand()%4+1; rzt1=rand()%7+1; if(rzt1==2) rzt2=1; if(rzt1==1||rzt1==4||rzt1==5) rzt2=rand()%2+1; if(rzt1==3||rzt1==6||rzt1==7) rzt2=rand()%4+1; score=0; for(o=1;o<11;o++) b[o]=0; switch(zt1*10+zt2) /*zt1和zt2分别代表方块的种类和状态,这步是根据这两个变量确定方块的四个点的坐标*/ { case 11: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=7; doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=2; break; case 12: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=5; doty[0]=4;doty[1]=3;doty[2]=2;doty[3]=1; break; case 21: dotx[0]=5;dotx[1]=6;dotx[2]=5;dotx[3]=6; doty[0]=1;doty[1]=1;doty[2]=2;doty[3]=2; break; case 31: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=5; doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1; break; case 32: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6; doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=2; break; case 33: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=5; doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2; break; case 34: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5; doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=2; break; case 41: dotx[0]=6;dotx[1]=5;dotx[2]=5;dotx[3]=4; doty[0]=2;doty[1]=2;doty[2]=1;doty[3]=1; break; case 42: dotx[0]=5;dotx[1]=5;dotx[2]=6;dotx[3]=6; doty[0]=3;doty[1]=2;doty[2]=2;doty[3]=1; break; case 51: dotx[0]=4;dotx[1]=5;dotx[2]=5;dotx[3]=6; doty[0]=2;doty[1]=2;doty[2]=1;doty[3]=1; break; case 52: dotx[0]=5;dotx[1]=5;dotx[2]=6;dotx[3]=6; doty[0]=1;doty[1]=2;doty[2]=2;doty[3]=3; break; case 61: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=6; doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1; break; case 62: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6; doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=3; break; case 63: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=4; doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2; break; case 64: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5; doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=1; break; case 71: dotx[0]=6;dotx[1]=5;dotx[2]=4;dotx[3]=4; doty[0]=2;doty[1]=2;doty[2]=2;doty[3]=1; break; case 72: dotx[0]=5;dotx[1]=5;dotx[2]=5;dotx[3]=6; doty[0]=3;doty[1]=2;doty[2]=1;doty[3]=1; break; case 73: dotx[0]=4;dotx[1]=5;dotx[2]=6;dotx[3]=6; doty[0]=1;doty[1]=1;doty[2]=1;doty[3]=2; break; case 74: dotx[0]=6;dotx[1]=6;dotx[2]=6;dotx[3]=5; doty[0]=1;doty[1]=2;doty[2]=3;doty[3]=3; break; } switch(rzt1*10+rzt2) /*确定第二个方块各个点的坐标*/ { case 11: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=7; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=2; break; case 12: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=5; roty[0]=4;roty[1]=3;roty[2]=2;roty[3]=1; break; case 21: rotx[0]=5;rotx[1]=6;rotx[2]=5;rotx[3]=6; roty[0]=1;roty[1]=1;roty[2]=2;roty[3]=2; break; case 31: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=5; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1; break; case 32: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=2; break; case 33: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=5; roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2; break; case 34: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5; roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=2; break; case 41: rotx[0]=6;rotx[1]=5;rotx[2]=5;rotx[3]=4; roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1; break; case 42: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=3;roty[1]=2;roty[2]=2;roty[3]=1; break; case 51: rotx[0]=4;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1; break; case 52: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=1;roty[1]=2;roty[2]=2;roty[3]=3; break; case 61: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1; break; case 62: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3; break; case 63: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4; roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2; break; case 64: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5; roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1; break; case 71: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1; break; case 72: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1; break; case 73: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2; break; case 74: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5; roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3; break; } system("cls"); /*显示初始阶段*/ printf("\n\n\n"); /*游戏区域下移3*/ for(u=0;u<19;u++) { for(i=0;i<12;i++) printf("%c",str[i][u]); printf("\n"); } gotoxy(16,5);printf("--------"); gotoxy(16,12);printf("--------"); for(i=6;i<12;i++) { gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); } for(i=6;i<12;i++) { gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); } for(i=0;i<4;i++) { gotoxy(rotx[i]+14,roty[i]+6);printf("%c",15); } begin2: delay(26000); /*游戏开始,延迟1*/ speed: delay(10000); /*加速,延迟2*/ gotoxy(16,14);printf("Score:%d",score); for(i=0;i<4;i++) { gotoxy(dotx[i]+1,doty[i]+4);printf(" "); ddx=dotx[i];ddy=doty[i]; str[ddx][ddy]=' '; } an=an-0.4; /*表示按键是否一直按着,用于方块落地后的移动*/ c=bioskey(1); /*按键处理部分*/ /*bioskey(1)是用来检测是否按下案件的函数*/ if(c!=0) { c=bioskey(0); if(c==8292||c==19712) { for(i=0;i<4;i++) { dx[i]=dotx[i]+1;dy[i]=doty[i]; } jiance(); for(i=0;i<4;i++) dotx[i]=(j)? dx[i] : dotx[i]; an=(j||bn); } if(c==7777||c==19200) { for(i=0;i<4;i++) { dx[i]=dotx[i]-1;dy[i]=doty[i]; } jiance(); for(i=0;i<4;i++) dotx[i]=(j)? dx[i] : dotx[i]; an=(j||bn); } if(c==6512) /*暂停的处理*/ { while(1) { c=bioskey(0); if(c==6512) break; } goto begin3; } if(c==8051||c==20480) spd=1; /*加速(spd==1表示加速状态)*/ if(c==4471||c==18432) /*旋转的处理*/ { for(i=0;i<4;i++) { dx[i]=dotx[i];dy[i]=doty[i]; } /*dx[]与dy[]是临时变量,这样一旦判断为不能旋转,就可方便的回复旋转前的坐标*/ cir(); /*旋转*/ jiance(); /*判断旋转是否能进行*/ for(i=0;i<4;i++) { dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; } /*根据jiance()得到的j值,判断是对dotx[]与doty[]赋旋转后的还是旋转前的值*/ if(j==1) /*如果旋转可已经行,就对原方块的状态进行改变*/ { an=(j||bn);zt2=zt2+1; if(zt2>maxzt[zt1]) zt2=1; goto overif; /*结束旋转的处理*/ } for(i=0;i<4;i++) { dx[i]=dotx[i]+1;dy[i]=doty[i]; } /*如果不能旋转,再判断坐标右移一个后能否旋转*/ cir(); jiance(); for(i=0;i<4;i++) { dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; } if(j==1) { an=(j||bn);zt2=zt2+1; if(zt2>maxzt[zt1]) zt2=1; goto overif; } if(dotx[2]==1) goto overif; for(i=0;i<4;i++) { dx[i]=dotx[i]-1;dy[i]=doty[i]; } /*判断坐标左移一个后能否旋转*/ cir(); jiance(); for(i=0;i<4;i++) { dotx[i]=(j)? dx[i] : dotx[i]; doty[i]=(j)?dy[i] : doty[i]; } if(j==1) { an=(j||bn);zt2=zt2+1; if(zt2>maxzt[zt1]) zt2=1; goto overif; } overif: ; } } begin3: for(i=0;i<4;i++) /*方块下移的处理*/ { dx[i]=dotx[i];dy[i]=doty[i]+1; } jiance(); bn=j; for(i=0;i<4;i++) doty[i]=(j)? dy[i] : doty[i]; for(i=0;i<4;i++) { gotoxy(dotx[i]+1,doty[i]+4);printf("%c",15); ddx=dotx[i];ddy=doty[i]; str[ddx][ddy]=15; } if(j==1&&spd==1) { spd=0;goto speed; } if(j==1||an>0) goto begin2; for(u=17;u>0;u--) /*方块停止下移(方块移动到底了)的处理*/ { for(i=1;i<11;i++) /*判断每一行是否排满*/ if(str[i][u]==15) b[i]=1; if (b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9]+b[10]<10) { for(o=1;o<11;o++) b[o]=0; continue; } for(o=1;o<11;o++) b[o]=0; a[t]=u;t++; } score+=(t)*(t-1)/2; for(i=1;i<11;i++) if(str[i][1]==15) b[i]=1; if (b[1]+b[2]+b[3]+b[4]+b[5]+b[6]+b[7]+b[8]+b[9]+b[10]>0 &&t==1) goto over; for(o=1;o<11;o++) b[o]=0; if(t==1) goto ran; switch(t) /*消除方块的处理,t=要消除的函数+1*/ { case 5: for(u=a[4];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; } /*将要消除的行中,最上面一行,上面的方格整体下移,下面的case 4,3,2类似*/ case 4: for(u=a[3];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; } case 3: for(u=a[2];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; } case 2: for(u=a[1];u>1;u--) { for(i=1;i<11;i++) str[i][u]=str[i][u-1]; } } t=1; for(u=1;u<18;u++) { for(i=1;i<11;i++) { gotoxy(i+1,u+4); printf("%c",str[i][u]); } } ran: zt1=rzt1;zt2=rzt2;rzt1=rand()%7+1; /*生成下两个方块*/ if(rzt1==2) rzt2=1; if(rzt1==1||rzt1==4||rzt1==5) rzt2=rand()%2+1; if(rzt1==3||rzt1==6||rzt1==7) rzt2=rand()%4+1; for(i=0;i<4;i++) { dotx[i]=rotx[i];doty[i]=roty[i]; gotoxy(dotx[i]+1,doty[i]+4);printf("%c",15); } switch(rzt1*10+rzt2) { case 11: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=7; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=2; break; case 12: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=5; roty[0]=4;roty[1]=3;roty[2]=2;roty[3]=1; break; case 21: rotx[0]=5;rotx[1]=6;rotx[2]=5;rotx[3]=6; roty[0]=1;roty[1]=1;roty[2]=2;roty[3]=2; break; case 31: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=5; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1; break; case 32: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=2; break; case 33: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=5; roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2; break; case 34: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5; roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=2; break; case 41: rotx[0]=6;rotx[1]=5;rotx[2]=5;rotx[3]=4; roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1; break; case 42: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=3;roty[1]=2;roty[2]=2;roty[3]=1; break; case 51: rotx[0]=4;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=2;roty[1]=2;roty[2]=1;roty[3]=1; break; case 52: rotx[0]=5;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=1;roty[1]=2;roty[2]=2;roty[3]=3; break; case 61: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1; break; case 62: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3; break; case 63: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4; roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2; break; case 64: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5; roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1; break; case 71: rotx[0]=6;rotx[1]=5;rotx[2]=4;rotx[3]=4; roty[0]=2;roty[1]=2;roty[2]=2;roty[3]=1; break; case 72: rotx[0]=5;rotx[1]=5;rotx[2]=5;rotx[3]=6; roty[0]=3;roty[1]=2;roty[2]=1;roty[3]=1; break; case 73: rotx[0]=4;rotx[1]=5;rotx[2]=6;rotx[3]=6; roty[0]=1;roty[1]=1;roty[2]=1;roty[3]=2; break; case 74: rotx[0]=6;rotx[1]=6;rotx[2]=6;rotx[3]=5; roty[0]=1;roty[1]=2;roty[2]=3;roty[3]=3; break; } for(i=6;i<12;i++) /*刷新一下用来显示下一个方块的那个区域*/ { gotoxy(16,i);printf("|");gotoxy(23,i);printf("|"); } for(i=6;i<12;i++) for(u=17;u<23;u++) { gotoxy(u,i);printf("%c",32); } for(i=0;i<4;i++) { gotoxy(rotx[i]+14,roty[i]+6);printf("%c",15); } c=bioskey(1);an=0; if(c!=0) c=bioskey(0); if(spd==1) { spd=0;goto speed; } goto begin2; over: system("cls"); gotoxy(36,11);printf("GAME OVER"); bioskey(0); system("cls"); printf("Your score is %d\n\n",score); printf("Press 'Q' to exit\nIf you want to play again,please press other keys."); score=0; c=bioskey(0); if(c!=4209) goto begin; }
黛眉浅淡
2024-09-30 07:06:27
自己倒是写过一个.400多行.不过注释都是写的英文,而且是很早以前写的.不好看,比前面那位兄台的好处在于是图形界面的,(* ̄▽ ̄*)....... /*************************************** TETRIS by chichujn version 1.1 start at 2008.01.18 finish at 2008.01.24 modify at 2008.02.27 ***************************************/
/*Struct of block*/ /************************************** Shape of block: The block is put in 4x4 area First row, bit 31-28; Second row, bit 27-24...
Postion of block: bit 15-8, bottom bit 7-4, wide bit 3-0, left **************************************/ long all[19]; /*************************************** □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □■□□ □□□□ ■□□□ □□□□ ■■□□ □□□□ □■□□ ■■□□ ■■□□ ■■□□ □■■□ ■■□□ ■□□□ ■□□□ ■■■□ □■□□ ■■□□ □■■□ ■□□□ ■■□□ □■□□ ■■■□ ■□□□ □□■□ ■■□□
/* Function Block_Init */ long Block_Init() { randomize(); return(all[random(19)]); }
/* Function Block_Position */ /*********************************** Return position param of the block n=0,bottom n=1,left n=2,right ************************************/ int Block_Position(long block,int n) { if(n==0) return( (int)block>>8 ); if(n==1) return( (int)block&0x000F ); if(n==2) return( ((int)block&0x000F) + ( ((int)block>>4)&0x000F ) ); }
/* Function Diplay */ /************************************** Display the blocks. Postion: 16,16;32,16;48,16... 16,32;32,32;48,32... Width of the blocks: 14 pixels Color: White **************************************/ void Display(long block) { int i,j; int bottom; int left;
/* Function Block_Rotate */ long Block_Rotate(long block) { int n; long tmp;
tmp=block; switch( (int)(block>>16) ) { case 0x00C6:tmp=0x04C80000+(tmp&0x0000FFFF);break; case 0x04C8:tmp=0x00C60000+(tmp&0x0000FFFF);break; case 0x006C:tmp=0x08C40000+(tmp&0x0000FFFF);break; case 0x08C4:tmp=0x006C0000+(tmp&0x0000FFFF);break; case 0x008E:tmp=0x0C880000+(tmp&0x0000FFFF);break; case 0x0C88:tmp=0x00E20000+(tmp&0x0000FFFF);break; case 0x00E2:tmp=0x044C0000+(tmp&0x0000FFFF);break; case 0x044C:tmp=0x008E0000+(tmp&0x0000FFFF);break; case 0x002E:tmp=0x088C0000+(tmp&0x0000FFFF);break; case 0x088C:tmp=0x00E80000+(tmp&0x0000FFFF);break; case 0x00E8:tmp=0x0C440000+(tmp&0x0000FFFF);break; case 0x0C44:tmp=0x002E0000+(tmp&0x0000FFFF);break; case 0x000F:tmp=0x88880000+(tmp&0x0000FFFF);break; case 0x8888:tmp=0x000F0000+(tmp&0x0000FFFF);break; case 0x004E:tmp=0x08C80000+(tmp&0x0000FFFF);break; case 0x08C8:tmp=0x00E40000+(tmp&0x0000FFFF);break; case 0x00E4:tmp=0x04C40000+(tmp&0x0000FFFF);break; case 0x04C4:tmp=0x004E0000+(tmp&0x0000FFFF);break; default:return(block); }
/* Function Block_Check */ /*********************************** Check the status of the block return 0:Not out of edge and no crash return 1:Out of edge return 2:Crash with other blocks ***********************************/ int Block_Check(long block) { int i; int bottom; int left; int right;
/* Function Block_Move */ /*********************************** Move the block n=0:down n=1:left n=2:right ***********************************/ long Block_Move(long block,int n) { long tmp;
/* Function Screen_Refresh */ /*********************************** If the block touchs the bottom or crashes with other blocks. Fresh the screen. ***********************************/ int Screen_Refresh(long block) { int i,j; int score; int left; int bottom; int tmp;
/* Function Block_Next */ /********************************** Display the next block **********************************/ void Block_Next(long next) { int i; int bottom;
/* Function Graph_Text */ /********************************* In graph mode,put a number on screen position:left,bottom number:n *********************************/ void Graph_Text(int left,int bottom,int n,int color) { char *ch;