c语言 编写俄罗斯方块的程序 要求有详细的中文注释

最好有流程图 加中文注释 越详尽越好 本人是初学者 太难也看不懂 简单点就好 拜托各位高手啦..如果满意会追加100分
最新回答
▎黎子°

2024-09-30 02:13:21

刚学c的时候编的,所以程序规范性以及代码的执行效率都不高,而且程序超长。但应该没有太难的语句。
#include<stdio.h>
#include<string.h>
#include<bios.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
int dx[4],dy[4]; /*定义全局变量*/
int zt1,zt2,str[15][19];
/*str[15][19]是把整个屏幕分为15*19个方格,每一个方格用一个数组单元表示,
如果=15,则这个方格已被占用,=0,则还是空的*/
int cx[8][5][4],cy[8][5][4]; /*该变量表示每种状态下,旋转时坐标的改变*/
int x,y,j,ji,c;
int maxzt[8]={0,2,1,4,2,2,4,4}; /*各个种类的方块分别有几种状态*/
cir() /*旋转的处理函数*/
{ dx[0]=dx[0]+cx[zt1][zt2][0];dy[0]=dy[0]+cy[zt1][zt2][0];
dx[2]=dx[2]+cx[zt1][zt2][2];dy[2]=dy[2]+cy[zt1][zt2][2];
dx[3]=dx[3]+cx[zt1][zt2][3];dy[3]=dy[3]+cy[zt1][zt2][3];
}
jiance() /*检测旋转或移动能否进行的函数,能则j=1,不能j=0*/
{ j=1;
for(ji=0;ji<4;ji++)
{ x=dx[ji];y=dy[ji];
if(str[x][y]!=' ') j=0;
}
c=bioskey(1);
if(c!=0) c=bioskey(0);
}
main()
{ int dotx[4],doty[4],score; /*dotx[]doty[]表示一个方块个点的坐标*/
int ddx,ddy;
int rzt1,rzt2,i,u,t=1;
int a[5],b[11],o,p,an,bn;
int rotx[4],roty[4],spd=0;
begin: system("cls"); /*游戏初始化阶段*/
printf("londing...");
for(i=0;i<12;i++) /*变量初始阶段*/
{ for(u=0;u<19;u++)
str[i][u]=' ';
}
for(i=0;i<12;i++)
{ str[i][0]='-';str[i][18]='-'; }
for(u=0;u<19;u++)
{ str[0][u]='|';str[11][u]='|'; }
cx[1][1][0]=1;cx[1][1][2]=-1;cx[1][1][3]=-2; /*对旋转变量进行赋值*/
cy[1][1][0]=1;cy[1][1][2]=-1;cy[1][1][3]=-2;
cx[1][2][0]=-1;cx[1][2][2]=1;cx[1][2][3]=2;
cy[1][2][0]=-1;cy[1][2][2]=1;cy[1][2][3]=2;

cx[2][1][0]=0;cx[2][1][2]=0;cx[2][1][3]=0;
cy[2][1][0]=0;cy[2][1][2]=0;cy[2][1][3]=0;

cx[3][1][0]=1;cx[3][1][2]=-1;cx[3][1][3]=1;
cy[3][1][0]=-1;cy[3][1][2]=1;cy[3][1][3]=1;
cx[3][2][0]=1;cx[3][2][2]=-1;cx[3][2][3]=-1;
cy[3][2][0]=1;cy[3][2][2]=-1;cy[3][2][3]=1;
cx[3][3][0]=-1;cx[3][3][2]=1;cx[3][3][3]=-1;
cy[3][3][0]=1;cy[3][3][2]=-1;cy[3][3][3]=-1;
cx[3][4][0]=-1;cx[3][4][2]=1;cx[3][4][3]=1;
cy[3][4][0]=-1;cy[3][4][2]=1;cy[3][4][3]=-1;

cx[4][1][0]=-1;cx[4][1][2]=1;cx[4][1][3]=2;
cy[4][1][0]=1;cy[4][1][2]=1;cy[4][1][3]=0;
cx[4][2][0]=1;cx[4][2][2]=-1;cx[4][2][3]=-2;
cy[4][2][0]=-1;cy[4][2][2]=-1;cy[4][2][3]=0;

cx[5][1][0]=1;cx[5][1][2]=1;cx[5][1][3]=0;
cy[5][1][0]=-1;cy[5][1][2]=1;cy[5][1][3]=2;
cx[5][2][0]=-1;cx[5][2][2]=-1;cx[5][2][3]=0;
cy[5][2][0]=1;cy[5][2][2]=-1;cy[5][2][3]=-2;

cx[6][1][0]=1;cx[6][1][2]=-1;cx[6][1][3]=0;
cy[6][1][0]=-1;cy[6][1][2]=1;cy[6][1][3]=2;
cx[6][2][0]=1;cx[6][2][2]=-1;cx[6][2][3]=-2;
cy[6][2][0]=1;cy[6][2][2]=-1;cy[6][2][3]=0;
cx[6][3][0]=-1;cx[6][3][2]=1;cx[6][3][3]=0;
cy[6][3][0]=1;cy[6][3][2]=-1;cy[6][3][3]=-2;
cx[6][4][0]=-1;cx[6][4][2]=1;cx[6][4][3]=2;
cy[6][4][0]=-1;cy[6][4][2]=1;cy[6][4][3]=0;

cx[7][1][0]=-1;cx[7][1][2]=1;cx[7][1][3]=2;
cy[7][1][0]=1;cy[7][1][2]=-1;cy[7][1][3]=0;
cx[7][2][0]=-1;cx[7][2][2]=1;cx[7][2][3]=0;
cy[7][2][0]=-1;cy[7][2][2]=1;cy[7][2][3]=2;
cx[7][3][0]=1;cx[7][3][2]=-1;cx[7][3][3]=-2;
cy[7][3][0]=-1;cy[7][3][2]=1;cy[7][3][3]=0;
cx[7][4][0]=1;cx[7][4][2]=-1;cx[7][4][3]=0;
cy[7][4][0]=1;cy[7][4][2]=-1;cy[7][4][3]=-2;

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
***************************************/

/* Head Files */
#include <stdio.h>
#include <bios.h>
#include <stdlib.h>
#include <graphics.h>

/*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];
/***************************************
□□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□
□□□□ □□□□ □■□□ □□□□ ■□□□ □□□□ ■■□□ □□□□ □■□□
■■□□ ■■□□ ■■□□ □■■□ ■■□□ ■□□□ ■□□□ ■■■□ □■□□
■■□□ □■■□ ■□□□ ■■□□ □■□□ ■■■□ ■□□□ □□■□ ■■□□

□□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ □□□□ ■□□□
□□□□ ■□□□ □□□□ ■■□□ □□□□ ■□□□ □□□□ □■□□ □□□□ ■□□□
□□■□ ■□□□ ■■■□ □■□□ □■□□ ■■□□ ■■■□ ■■□□ □□□□ ■□□□
■■■□ ■■□□ ■□□□ □■□□ ■■■□ ■□□□ □■□□ □■□□ ■■■■ ■□□□
***************************************/

int screen[25];
/*Record the fixed blocks*/
/********************************
screen[n]
bit 9-0
********************************/

/* Function Welcome */
void Welcome(void)
{
gotoxy(37,10);
printf("TETRIS");
gotoxy(1,19);
printf("Version :1.1\nProgramBy:chichujn\n\nKEY: q-Quit; w-Rotate ; a-Left; d-Right; s-Speedup.\n\n");
printf("Press any key to start...");
getch();
}

/* Function Data_Init */
void Data_Init(void)
{
int i;

all[0]=0x00CC0014;
all[1]=0x00C60024;
all[2]=0x04C80014;
all[3]=0x006C0024;
all[4]=0x08C40014;
all[5]=0x008E0024;
all[6]=0x0C880014;
all[7]=0x00E20024;
all[8]=0x044C0014;
all[9]=0x002E0024;
all[10]=0x088C0014;
all[11]=0x00E80024;
all[12]=0x0C440014;
all[13]=0x004E0024;
all[14]=0x08C80014;
all[15]=0x00E40024;
all[16]=0x04C40014;
all[17]=0x000F0034;
all[18]=0x88880004;

for(i=0;i<25;++i)
screen[i]=0;
}

/* 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;

bottom=Block_Position(block,0);
left=Block_Position(block,1);

for(i=0;i<25;++i)
for(j=0;j<10;++j)
{
if(( (screen[i]>>(9-j))&0x0001 )==0)
setfillstyle(SOLID_FILL,BLACK);
else
setfillstyle(SOLID_FILL,WHITE);
bar( (j+1)*16, (i+1)*16, (j+1)*16+14, (i+1)*16+14 );
}

setfillstyle(SOLID_FILL,WHITE);
for(i=0;i<16;++i)
if( ( ((block>>(31-i))&0x0001)==1 ) && ((bottom-3+i/4)>=0) )
bar( (left+i%4+1)*16, (bottom+i/4-3+1)*16, (left+i%4+1)*16+14, (bottom+i/4-3+1)*16+14 );
}

/* Function delay */
void delay(int n)
{
long t1,t2;

t1=t2=biostime(0,0);
while(t2-t1<n)
t2=biostime(0,0);
}

/* 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);
}

n=(int)(block>>4)&0x000F;
if(n==0)
tmp+=0x0030;
if(n==1)
tmp+=0x0010;
if(n==2)
tmp-=0x0010;
if(n==3)
tmp-=0x0030;

if(Block_Check(tmp))
return(block);
return(tmp);
}

/* 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;

bottom=Block_Position(block,0);
left=Block_Position(block,1);
right=Block_Position(block,2);

if(left==0x000F||right>9||bottom>24)
return(1);
for(i=0;i<16;++i)
if( ((block>>(31-i))&1) && ( (screen[bottom-3+i/4]>>(9-left-i%4) ) &1 ) )
return(2);
return(0);
}

/* Function Block_Move */
/***********************************
Move the block
n=0:down
n=1:left
n=2:right
***********************************/
long Block_Move(long block,int n)
{
long tmp;

tmp=block;
if(n==0)
tmp+=0x0100;
if(n==1)
tmp-=0x0001;
if(n==2)
tmp+=0x0001;
if(Block_Check(tmp))
return(block);
return(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;

bottom=Block_Position(block,0);
left=Block_Position(block,1);
score=0;

for(i=0;i<16;++i)
if( ((block>>(31-i))&0x0001)==1 && ((((int)block>>8)&0x00FF)-i/4)<0 )
return(-1);

tmp=left>6?( ( (block>>28) &0x000F) >> (left-6) ):( ( (block>>28) &0x000F) << (6-left) );
screen[bottom-3]=screen[bottom-3]|tmp;
tmp=left>6?( ( (block>>24) &0x000F) >> (left-6) ):( ( (block>>24) &0x000F) << (6-left) );
screen[bottom-2]=screen[bottom-2]|tmp;
tmp=left>6?( ( (block>>20) &0x000F) >> (left-6) ):( ( (block>>20) &0x000F) << (6-left) );
screen[bottom-1]=screen[bottom-1]|tmp;
tmp=left>6?( ( (block>>16) &0x000F) >> (left-6) ):( ( (block>>16) &0x000F) << (6-left) );
screen[bottom]=screen[bottom]|tmp;

for(i=0;i<25;++i)
if(screen[i]==0x03FF)
{
score++;
for(j=i;j>0;--j)
screen[j]=screen[j-1];
}

return(score);
}

/* Function Block_Next */
/**********************************
Display the next block
**********************************/
void Block_Next(long next)
{
int i;
int bottom;

setfillstyle(SOLID_FILL,BLACK);
bar(306,98,328,120);

setfillstyle(SOLID_FILL,WHITE);
bottom=Block_Position(next,0);
for(i=0;i<16;++i)
if((next>>(31-i))&1)
bar( (i%4+1)*6+300, (bottom+i/4-3+1)*6+110, (i%4+1)*6+304, (bottom+i/4-3+1)*6+114 );
}

/* 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;

itoa(n,ch,10);
setcolor(color);
outtextxy(left,bottom,ch);
}

/* Function Game */
int Game()
{
long block;
long block_tmp;
long next;

int i;
int tmp;
int t;
int speed_tmp;
int Game_Speed;
int Game_Score;

char key;
char* score;
char* speed;

Game_Score=0;
Game_Speed=speed_tmp=1;
t=0;
block=Block_Init();
Display(block);
setcolor(WHITE);
settextstyle(4,0,3);
outtextxy(250,50,"Score:");
outtextxy(250,80,"SPeed:");
outtextxy(250,110,"Next :");
rectangle(14,14,176,416);
Graph_Text(300,50,Game_Score,WHITE);
Graph_Text(300,80,Game_Speed,WHITE);
next=Block_Init();
Block_Next(next);

while(1)
{
if(bioskey(1)!=0)
{
key=getch();
if(key=='q'||key=='Q')
break;
if(key=='s'||key=='S')
speed_tmp=10;
else
speed_tmp=Game_Speed;
}
delay(1);
++t;
if(t==30000)
t=0;
if(t%(11-speed_tmp)==0)
{
block_tmp=block;
block=Block_Move(block,0);
if(block_tmp==block)
{
tmp=Game_Score;
Graph_Text(300,50,Game_Score,BLACK);
Game_Score+=Screen_Refresh(block);
if(Game_Score<tmp)
return(tmp);
Graph_Text(300,50,Game_Score,WHITE);
if(Game_Score>tmp&&Game_Score%20==0&&Game_Speed<10&&Game_Score>0)
{
Graph_Text(300,80,Game_Speed,BLACK);
Game_Speed++;
Graph_Text(300,80,Game_Speed,WHITE);
}
block=next;
next=Block_Init();
Block_Next(next);
speed_tmp=Game_Speed;
}
Display(block);
}
if(key=='A'||key=='a'||key=='D'||key=='d'||key=='W'||key=='w')
{
if(key=='A'||key=='a')
block=Block_Move(block,1);
if(key=='D'||key=='d')
block=Block_Move(block,2);
if(key=='W'||key=='w')
block=Block_Rotate(block);
key='m';
Display(block);
}
}
return(Game_Score);
}

/* Function Main */
int main()
{
int gd=DETECT,gm;
int score;

Welcome();
Data_Init();
initgraph(&gd,&gm,"");
registerbgidriver(EGAVGA_driver);
score=Game();
closegraph();
gotoxy(35,12);
printf("Game Over");
gotoxy(1,23);
printf("Your score:%d\nPress any key to quit...",score);

getch();
}
酷炸小仙女

2024-09-30 07:30:39

http://down.cnzz.cn//Download.aspx?id=72336&llid=0

直接复制到迅雷下载。别忘了给分^_^

http://www.gougou.com/search?search=c%E8%AF%AD%E8%A8%80%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97%E7%A8%8B%E5%BA%8F&restype=-1&id=10000001&ty=0&pattern=0
试一试这个
可爱的害羞鬼

2024-09-30 07:45:09

别心急,知道么