寻求c语言版的俄罗斯方块程序
4个回答
展开全部
#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;
}
#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;
}
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
楼上的人说的很好,但bios.h 是TC 自带的,其它编译器通不过,现在用的较多的编译器是VC6.0 或VS 2008或C++builder,这些编译器通不过bios.h编译,若要完整的代码,把邮箱号留下,我发给你一个压缩包,最好学会用工具才能顺利通过编译,如果只学了C程序设计而没有真正会操作工具,俄罗斯方块程序
是调试不出来的。
是调试不出来的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
#define ESC 0x011b
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define ENTER 0x1c0d
#define F5 0x3f00
#define SPACE 0x3920
unsigned char far *rom_char_set=(char far *)0xF000FA6EL;
unsigned char far *video_buffer=(char far *)0xA0000000L;
int miny=172;
int Level=0;
int Score=0;
char color;
char elsfk='n';
char score[5];
char level[2];
struct CUBE
{int x[4];
int y[4];
int kind;
char color;
}cube,cube2;
void Set_Video_Mode(int mode)
{
union REGS inregs,outregs;
inregs.h.ah=0;
inregs.h.al=(unsigned char)mode;
int86(0x10,&inregs,&outregs);
}/*视频模式驱动*/
void point(int x,int y,char color)
{video_buffer[((y<<8)+(y<<6))+x]=color;
} /*画点函数*/
void Hline(int x1,int x2,int y,char color)
{int index;
for(index=x1;index<=x2;index++)
video_buffer[((y<<8)+(y<<6))+index]=color;
} /*画横线*/
void Sline(int y1,int y2,int x,char color)
{int index;
for(index=y1;index<=y2;index++)
video_buffer[((index<<8)+(index<<6))+x]=color;
} /*画竖线*/
void fillrectangle(int x1,int y1,int x2,int y2,char color)
{int i;
for(i=y1;i<=y2;i++)
if(i>0)
Hline(x1,x2,i,color);
if(y1>0&&elsfk=='y')
video_buffer[((y1<<8)+(y1<<6))+x1]=0;
}/*填充矩形*/
void Delay(int clicks)
{
unsigned int far *clock=(unsigned int far *)0x0000046CL;
unsigned int now;
now=*clock;
while(abs(*clock-now)<clicks){}
}/*延时函数*/
void Char_Show(int xc,int yc,char c,int color)
{
int offset,x,y;
char far *work_char;
unsigned char bit_mask=0x80;
work_char=rom_char_set+c*8;
offset=(yc<<8)+(yc<<6)+xc;
for(y=0;y<8;y++)
{
bit_mask=0x80;
for(x=0;x<8;x++)
{
if((*work_char&bit_mask))
video_buffer[offset+x]=color;
else if(!(*work_char&bit_mask))
video_buffer[offset+x]=0;
bit_mask=(bit_mask>>1);
}
offset+=320;
work_char++;
}
}/*显示字符*/
void String_Show(int x,int y,int color,char *string)
{
int index;
for(index=0;string[index]!=0;index++)
{
Char_Show(x+(index<<3),y,string[index],color);
}
}
/*显示字符串*/
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /*测试为1的位则显示*/
point(x+i*8+k,y+j,color);
}
/*显示汉字*/
void Game_Draw()
{char e12S[]={
/* 以下是 '俄' 的 12点阵宋体 字模,24 byte */
0x23,0x00,0x2D,0x40,0x45,0x20,0x5F,0xE0,
0xC5,0x00,0x47,0x20,0x4D,0x40,0x54,0x80,
0x44,0xA0,0x45,0x60,0x5C,0x20,0x00,0x00,
};
char luo12S[]={
/* 以下是 '罗' 的 12点阵宋体 字模,24 byte */
0x7F,0xE0,0x49,0x20,0x49,0x20,0x7F,0xE0,
0x04,0x00,0x0F,0xC0,0x10,0x80,0x69,0x00,
0x06,0x00,0x0C,0x00,0xF0,0x00,0x00,0x00,
};
char si12S[]={
/* 以下是 '斯' 的 12点阵宋体 字模,24 byte */
0x48,0x60,0xFD,0x80,0x49,0x00,0x79,0x00,
0x49,0xE0,0x79,0x40,0x49,0x40,0xFD,0x40,
0x2A,0x40,0x4A,0x40,0x84,0x40,0x00,0x00,
};
char fang12S[]={
/* 以下是 '方' 的 12点阵宋体 字模,24 byte */
0x04,0x00,0x02,0x40,0xFF,0xE0,0x08,0x00,
0x0F,0x80,0x08,0x80,0x08,0x80,0x10,0x80,
0x10,0x80,0x20,0x80,0xC7,0x00,0x00,0x00,
};
char kuai12S[]={
/* 以下是 '块' 的 12点阵宋体 字模,24 byte */
0x42,0x00,0x42,0x00,0x4F,0xC0,0xF2,0x40,
0x42,0x40,0x42,0x40,0x5F,0xE0,0x42,0x00,
0xF5,0x00,0x88,0x80,0x30,0x60,0x00,0x00,
};
char zuo12S[]={
/* 以下是 '作' 的 12点阵宋体 字模,24 byte */
0x12,0x00,0x12,0x00,0x27,0xE0,0x6A,0x00,
0xB2,0x00,0x23,0xC0,0x22,0x00,0x22,0x00,
0x23,0xE0,0x22,0x00,0x22,0x00,0x00,0x00,
};
char zhe12S[]={
/* 以下是 '者' 的 12点阵宋体 字模,24 byte */
0x08,0x40,0x7F,0x80,0x09,0x00,0x0A,0x40,
0xFF,0xE0,0x08,0x00,0x1F,0x80,0x30,0x80,
0xDF,0x80,0x10,0x80,0x1F,0x80,0x00,0x00,
};
char zhi12S[]={
/* 以下是 '志' 的 12点阵宋体 字模,24 byte */
0x04,0x00,0x04,0x40,0xFF,0xE0,0x04,0x00,
0x04,0x00,0x7F,0xC0,0x00,0x00,0x54,0x40,
0x52,0x20,0x90,0xA0,0x1F,0x80,0x00,0x00,
};
char ping12S[]={
/* 以下是 '平' 的 12点阵宋体 字模,24 byte */
0x7F,0xC0,0x04,0x00,0x24,0x80,0x14,0x80,
0x15,0x00,0xFF,0xE0,0x04,0x00,0x04,0x00,
0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00,
};
Hline(1,161,1,45);
Hline(161,319,1,45);
Hline(1,319,181,45);
Hline(162,279,45,45);
Hline(162,279,90,45);
Hline(162,279,135,45);
Sline(1,181,1,45);
Sline(1,181,161,45);
Sline(1,181,280,45);
Sline(1,181,319,45);
drawmat(e12S,12,292,5,48);
drawmat(luo12S,12,292,20,48);
drawmat(si12S,12,292,35,48);
drawmat(fang12S,12,292,50,48);
drawmat(kuai12S,12,292,65,48);
drawmat(zuo12S,12,292,100,48);
drawmat(zhe12S,12,292,115,48);
drawmat(zhi12S,12,292,145,48);
drawmat(ping12S,12,292,160,48);
String_Show(180,15,48,"Level:");
String_Show(180,61,48,"Next:");
String_Show(180,151,48,"Score:");
}/*画界面*/
int Color(int x,int y)
{if(y<0)return 0;
else {color= video_buffer[((y)<<6)+((y)<<8)+(x)];return color;}
}
void Draw_Cube(int x[],int y[],char color)
{fillrectangle(x[0],y[0],x[0]+8,y[0]+8,color);
fillrectangle(x[1],y[1],x[1]+8,y[1]+8,color);
fillrectangle(x[2],y[2],x[2]+8,y[2]+8,color);
fillrectangle(x[3],y[3],x[3]+8,y[3]+8,color);
}/*画方块*/
void Clean_Cube(int x[],int y[])
{fillrectangle(x[0],y[0],x[0]+8,y[0]+8,0);
fillrectangle(x[1],y[1],x[1]+8,y[1]+8,0);
fillrectangle(x[2],y[2],x[2]+8,y[2]+8,0);
fillrectangle(x[3],y[3],x[3]+8,y[3]+8,0);
}/*清除方块*/
void Preview(color)
{fillrectangle(cube2.x[0]+138,cube2.y[0]+130,cube2.x[0]+8+138,cube2.y[0]+8+130,color);
fillrectangle(cube2.x[1]+138,cube2.y[1]+130,cube2.x[1]+8+138,cube2.y[1]+8+130,color);
fillrectangle(cube2.x[2]+138,cube2.y[2]+130,cube2.x[2]+8+138,cube2.y[2]+8+130,color);
fillrectangle(cube2.x[3]+138,cube2.y[3]+130,cube2.x[3]+8+138,cube2.y[3]+8+130,color);
}/*方块下一个显示*/
void Cube_Value(struct CUBE *cube)
{ switch(cube->kind)
{case 0:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=92;
cube->y[0]=-8;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=15;break;
case 1 :
cube->x[0]=72;cube->x[1]=72;cube->x[2]=72;cube->x[3]=72;
cube->y[0]=-38;cube->y[1]=-28;cube->y[2]=-18;cube->y[3]=-8;
cube->color=15;break;
case 2:
cube->x[0]=72;cube->x[1]=62;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=2;break;
case 3:
cube->x[0]=82;cube->x[1]=72;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=2;break;
case 4:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=72;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=2;break;
case 5:
cube->x[0]=72;cube->x[1]=72;cube->x[2]=82;cube->x[3]=72;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=2;break;
case 6:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=4;break;
case 7:
cube->x[0]=72;cube->x[1]=72;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=11;break;
case 8:
cube->x[0]=82;cube->x[1]=62;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=11;break;
case 9:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-28;cube->y[2]=-18;cube->y[3]=-8;
cube->color=11;break;
case 10:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=62;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=11;break;
case 11:
cube->x[0]=82;cube->x[1]=82;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=14;break;
case 12:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=14;break;
case 13:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=72;cube->x[3]=72;
cube->y[0]=-28;cube->y[1]=-28;cube->y[2]=-18;cube->y[3]=-8;
cube->color=14;break;
case 14:
cube->x[0]=62;cube->x[1]=62;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=14;break;
case 15:
cube->x[0]=82;cube->x[1]=72;cube->x[2]=82;cube->x[3]=72;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=15;break;
case 16:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=15;break;
case 17:
cube->x[0]=72;cube->x[1]=72;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=48;break;
case 18:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=62;cube->x[3]=72;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=48;break;
}
}/*方块值初始化*/
void Cube_Judge()
{int x,y,i,n=0;
for(y=cube.y[3];y>=cube.y[0];y-=10)
{for(x=2;x<=152;x+=10)
if(Color(x,y)==0)break;
if(x>152)
{n++;
fillrectangle(2,y,160,y+8,0);
}
else if(x<=152&&n!=0)
{for(i=2;i<=152;i+=10)
if(Color(i,y)!=0)fillrectangle(i,y+n*10,i+8,y+n*10+8,color);
fillrectangle(2,y,160,y+8,0);
}
}
if(n!=0)
{for(y=cube.y[0]-10;y>=miny;y-=10)
for(x=2;x<=152;x+=10)
if(Color(x,y)!=0)
{fillrectangle(x,y+n*10,x+8,y+n*10+8,color);
fillrectangle(x,y,x+8,y+8,0);
}
miny=miny+n*10;
Score+=10*n;
Level=Score/200;
sprintf(score,"%d",Score);
sprintf(level,"%d",Level);
String_Show(240,15,48,level);
String_Show(240,151,48,score);
}
}/*方块下落及消除*/
void Game_Over()
{String_Show(40,90,40,"Game Over!");
getch();
exit(0);
}/*游戏结束!*/
void Symbol_Value()
{int i;
for(i=0;i<4;i++)
point(cube.x[i],cube.y[i],cube.color);
if(miny>cube.y[0])miny=cube.y[0];
elsfk='n';
Cube_Judge();
if(miny<=0)
Game_Over();
}/*符号赋值*/
void Cube_Change(int kind)
{ if(kind==0&&cube.y[0]+30<=172&&Color(cube.x[1],cube.y[0]+10)==0&&Color(cube.x[1],cube.y[0]+20)==0&&Color(cube.x[1],cube.y[1]+30)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]=cube.x[1];cube.x[2]=cube.x[0];cube.x[3]=cube.x[0];
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1]+10;cube.y[3]=cube.y[2]+10;
cube.kind=1;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==1&&cube.x[0]-10>=2&&cube.x[3]+20<=152&&Color(cube.x[0]-10,cube.y[0])==0&&Color(cube.x[3]+20,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]-=10;cube.x[2]+=10;cube.x[3]+=20;cube.y[1]=cube.y[0];cube.y[2]=cube.y[0];cube.y[3]=cube.y[0];
cube.kind=0;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==2&&Color(cube.x[0],cube.y[2]+10)==0&&cube.y[2]+10<=172)
{Clean_Cube(cube.x,cube.y);
cube.x[3]=cube.x[2];
cube.y[3]=cube.y[2]+10;
cube.kind=3;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==3&&Color(cube.x[0]-10,cube.y[0])==0&&cube.x[0]+10<=152&&Color(cube.x[0]+10,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]-=10;cube.x[1]+=10;cube.x[2]+=10;cube.y[1]-=10;cube.y[2]-=10;cube.y[3]-=10;
cube.kind=4;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==4&&Color(cube.x[3]+10,cube.y[3])==0&&Color(cube.x[3],cube.y[3]+10)==0&&cube.y[3]+10<=172)
{Clean_Cube(cube.x,cube.y);
cube.x[0]+=10;cube.y[1]+=10;cube.y[2]+=10;cube.y[3]+=10;
cube.kind=5;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==5&&cube.x[0]-10>=2&&Color(cube.x[0]-10,cube.y[2])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[3]=cube.x[2];cube.y[3]=cube.y[2];cube.x[1]-=10;cube.x[2]-=10;
cube.kind=2;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==7&&cube.x[0]-20>=2&&Color(cube.x[0]-20,cube.y[1])==0&&Color(cube.x[0]-10,cube.y[1])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]-20;cube.x[2]=cube.x[0]-10;cube.x[3]=cube.x[0];
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1];cube.y[3]=cube.y[2];
cube.kind=8;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==8&&cube.y[1]+10<=172&&cube.x[0]+10<=152&&Color(cube.x[0]+10,cube.y[0])==0&&Color(cube.x[0]+10,cube.y[1])==0&&Color(cube.x[0],cube.y[1]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]+10;cube.x[2]=cube.x[1];cube.x[3]=cube.x[1];
cube.y[1]=cube.y[0];cube.y[2]=cube.y[0]+10;cube.y[3]=cube.y[2]+10;
cube.kind=9;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==9&&cube.x[1]+10<=152&&Color(cube.x[0],cube.y[2])==0&&Color(cube.x[1]+10,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]+10;cube.x[2]=cube.x[1]+10;cube.x[3]=cube.x[0];
cube.y[1]=cube.y[0];cube.y[2]=cube.y[0];cube.y[3]=cube.y[0]+10;
cube.kind=10;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==10&&cube.y[3]+10<=172&&Color(cube.x[0],cube.y[3]+10)==0&&Color(cube.x[1],cube.y[3]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0];cube.x[2]=cube.x[0];cube.x[3]=cube.x[0]+10;
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1]+10;cube.y[3]=cube.y[2];
cube.kind=7;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==11&&cube.x[0]+10<=152&&Color(cube.x[3],cube.y[0])==0&&Color(cube.x[0]+10,cube.y[0])==0&&Color(cube.x[0]+10,cube.y[1])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0];cube.x[0]-=10;cube.x[2]=cube.x[1]+10;cube.x[3]=cube.x[2];cube.y[1]=cube.y[0];cube.y[2]=cube.y[0];cube.y[3]=cube.y[0]+10;
cube.kind=12;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==12&&cube.y[3]+10<=172&&Color(cube.x[1],cube.y[3])==0&&Color(cube.x[1],cube.y[3]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]+=10;cube.x[1]+=10;cube.x[2]-=10;cube.x[3]-=10;cube.y[2]=cube.y[3];cube.y[3]+=10;
cube.kind=13;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==13&&cube.x[1]+10<=152&&Color(cube.x[1],cube.y[2])==0&&Color(cube.x[1]+10,cube.y[2])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]-=10;cube.x[2]+=10;cube.x[3]+=20;cube.y[1]+=10;cube.y[3]=cube.y[2];
cube.kind=14;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==14&&cube.x[0]-10>=2&&cube.y[1]+10<=172&&Color(cube.x[0]-10,cube.y[1]+10)==0&&Color(cube.x[0],cube.y[1]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[2]-=20;cube.x[3]-=20;cube.y[2]+=10;cube.y[3]+=10;
cube.kind=11;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==15&&cube.x[0]+10<=152&&Color(cube.x[0]-10,cube.y[0])==0&&Color(cube.x[0]+10,cube.y[2])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]-=10;cube.x[1]+=10;cube.x[3]+=20;cube.y[1]-=10;cube.y[3]-=10;
cube.kind=16;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==16&&cube.y[3]+10<=172&&Color(cube.x[0],cube.y[2])==0&&Color(cube.x[0],cube.y[2]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]+=10;cube.x[1]-=10;cube.x[3]=cube.x[1];cube.y[1]+=10;cube.y[3]+=10;
cube.kind=15;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==17&&cube.x[0]-10>=2&&Color(cube.x[0]-10,cube.y[1])==0&&Color(cube.x[0]+10,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]+10;cube.x[2]=cube.x[0];cube.x[3]=cube.x[0]-10;
cube.y[1]=cube.y[0];cube.y[2]=cube.y[0]+10;cube.y[3]=cube.y[2];
cube.kind=18;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==18&&cube.y[3]+10<=172&&Color(cube.x[1],cube.y[2])==0&&Color(cube.x[1],cube.y[2]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0];cube.x[2]=cube.x[0]+10;cube.x[3]=cube.x[2];
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1];cube.y[3]=cube.y[2]+10;
cube.kind=17;
Draw_Cube(cube.x,cube.y,cube.color);
}
} /*方块变形*/
void Game_Play()
{int key,i,n=0;
struct CUBE * AA;
sprintf(score,"%d",Score);
sprintf(level,"%d",Level);
String_Show(240,15,48,level);
String_Show(240,151,48,score);
randomize();
cube.kind=rand()%19;AA=&cube;
Cube_Value(AA);
while(1)
{ while(!kbhit())
{Draw_Cube(cube.x,cube.y,cube.color);/*画出方块*/
if(elsfk=='n')
{randomize();
cube2.kind=rand()%19;
AA=&cube2;
Cube_Value(AA);/*方块赋值*/
Preview(cube2.color);
elsfk='y';
}
Delay(1);/*延时*/
n++;
if(n==11-Level)
{if(Color(cube.x[0],cube.y[0]+10)==0&&Color(cube.x[1],cube.y[1]+10)==0&&Color(cube.x[2],cube.y[2]+10)==0&&Color(cube.x[3],cube.y[3]+10)==0&&cube.y[3]<172)
{Clean_Cube(cube.x,cube.y);/*清除方块*/
for(i=0;i<4;i++)
cube.y[i]+=10;/*方块下落*/
}
else {Symbol_Value();Preview(0);cube=cube2;}
n=0;
}
}
key=bioskey(0);/*接收按键信息*/
if(elsfk=='y')
{Draw_Cube(cube.x,cube.y,cube.color);/*画出方块*/
if(key==UP||key==SPACE)
{Cube_Change(cube.kind);
}
/*上键或空格键变形*/
if(key==DOWN)
{if(Color(cube.x[0],cube.y[0]+10)==0&&Color(cube.x[1],cube.y[1]+10)==0&&Color(cube.x[2],cube.y[2]+10)==0&&Color(cube.x[3],cube.y[3]+10)==0&&cube.y[3]<172)
{Clean_Cube(cube.x,cube.y);/*清除方块*/
for(i=0;i<4;i++)
cube.y[i]+=10;/*方块下落*/
Draw_Cube(cube.x,cube.y,cube.color);/*画出方块*/
}
else {Symbol_Value();Preview(0);cube=cube2;}
}/*下键加速下落*/
if(key==LEFT)
{if(cube.x[0]-10>=2&&cube.x[1]-10>=2&&cube.x[2]-10>=2&&cube.x[3]-10>=2)
if(Color(cube.x[0]-10,cube.y[0])==0&&Color(cube.x[1]-10,cube.y[1])==0&&Color(cube.x[2]-10,cube.y[2])==0&&Color(cube.x[3]-10,cube.y[3])==0)
{Clean_Cube(cube.x,cube.y);
for(i=0;i<4;i++)
cube.x[i]-=10;
Draw_Cube(cube.x,cube.y,cube.color);
}
}/*左键方块左移*/
if(key==RIGHT)
{if(cube.x[0]+10<=152&&cube.x[1]+10<=152&&cube.x[2]+10<=152&&cube.x[3]+10<=152)
if(Color(cube.x[0]+10,cube.y[0])==0&&Color(cube.x[1]+10,cube.y[1])==0&&Color(cube.x[2]+10,cube.y[2])==0&&Color(cube.x[3]+10,cube.y[3])==0)
{Clean_Cube(cube.x,cube.y);
for(i=0;i<4;i++)
cube.x[i]+=10;
Draw_Cube(cube.x,cube.y,cube.color);
}
}/*右键方块右移*/
}
if(key==ESC)
{Set_Video_Mode(0x03);
exit(0);
}/*Esc结束游戏*/
if(key==F5)getch();/*F5暂停游戏*/
}/*游戏进行中*/
}
void main()
{Set_Video_Mode(0x13);
Game_Draw();
Game_Play();
}
#include<dos.h>
#include<stdlib.h>
#define ESC 0x011b
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define ENTER 0x1c0d
#define F5 0x3f00
#define SPACE 0x3920
unsigned char far *rom_char_set=(char far *)0xF000FA6EL;
unsigned char far *video_buffer=(char far *)0xA0000000L;
int miny=172;
int Level=0;
int Score=0;
char color;
char elsfk='n';
char score[5];
char level[2];
struct CUBE
{int x[4];
int y[4];
int kind;
char color;
}cube,cube2;
void Set_Video_Mode(int mode)
{
union REGS inregs,outregs;
inregs.h.ah=0;
inregs.h.al=(unsigned char)mode;
int86(0x10,&inregs,&outregs);
}/*视频模式驱动*/
void point(int x,int y,char color)
{video_buffer[((y<<8)+(y<<6))+x]=color;
} /*画点函数*/
void Hline(int x1,int x2,int y,char color)
{int index;
for(index=x1;index<=x2;index++)
video_buffer[((y<<8)+(y<<6))+index]=color;
} /*画横线*/
void Sline(int y1,int y2,int x,char color)
{int index;
for(index=y1;index<=y2;index++)
video_buffer[((index<<8)+(index<<6))+x]=color;
} /*画竖线*/
void fillrectangle(int x1,int y1,int x2,int y2,char color)
{int i;
for(i=y1;i<=y2;i++)
if(i>0)
Hline(x1,x2,i,color);
if(y1>0&&elsfk=='y')
video_buffer[((y1<<8)+(y1<<6))+x1]=0;
}/*填充矩形*/
void Delay(int clicks)
{
unsigned int far *clock=(unsigned int far *)0x0000046CL;
unsigned int now;
now=*clock;
while(abs(*clock-now)<clicks){}
}/*延时函数*/
void Char_Show(int xc,int yc,char c,int color)
{
int offset,x,y;
char far *work_char;
unsigned char bit_mask=0x80;
work_char=rom_char_set+c*8;
offset=(yc<<8)+(yc<<6)+xc;
for(y=0;y<8;y++)
{
bit_mask=0x80;
for(x=0;x<8;x++)
{
if((*work_char&bit_mask))
video_buffer[offset+x]=color;
else if(!(*work_char&bit_mask))
video_buffer[offset+x]=0;
bit_mask=(bit_mask>>1);
}
offset+=320;
work_char++;
}
}/*显示字符*/
void String_Show(int x,int y,int color,char *string)
{
int index;
for(index=0;string[index]!=0;index++)
{
Char_Show(x+(index<<3),y,string[index],color);
}
}
/*显示字符串*/
void drawmat(char *mat,int matsize,int x,int y,int color)
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /*测试为1的位则显示*/
point(x+i*8+k,y+j,color);
}
/*显示汉字*/
void Game_Draw()
{char e12S[]={
/* 以下是 '俄' 的 12点阵宋体 字模,24 byte */
0x23,0x00,0x2D,0x40,0x45,0x20,0x5F,0xE0,
0xC5,0x00,0x47,0x20,0x4D,0x40,0x54,0x80,
0x44,0xA0,0x45,0x60,0x5C,0x20,0x00,0x00,
};
char luo12S[]={
/* 以下是 '罗' 的 12点阵宋体 字模,24 byte */
0x7F,0xE0,0x49,0x20,0x49,0x20,0x7F,0xE0,
0x04,0x00,0x0F,0xC0,0x10,0x80,0x69,0x00,
0x06,0x00,0x0C,0x00,0xF0,0x00,0x00,0x00,
};
char si12S[]={
/* 以下是 '斯' 的 12点阵宋体 字模,24 byte */
0x48,0x60,0xFD,0x80,0x49,0x00,0x79,0x00,
0x49,0xE0,0x79,0x40,0x49,0x40,0xFD,0x40,
0x2A,0x40,0x4A,0x40,0x84,0x40,0x00,0x00,
};
char fang12S[]={
/* 以下是 '方' 的 12点阵宋体 字模,24 byte */
0x04,0x00,0x02,0x40,0xFF,0xE0,0x08,0x00,
0x0F,0x80,0x08,0x80,0x08,0x80,0x10,0x80,
0x10,0x80,0x20,0x80,0xC7,0x00,0x00,0x00,
};
char kuai12S[]={
/* 以下是 '块' 的 12点阵宋体 字模,24 byte */
0x42,0x00,0x42,0x00,0x4F,0xC0,0xF2,0x40,
0x42,0x40,0x42,0x40,0x5F,0xE0,0x42,0x00,
0xF5,0x00,0x88,0x80,0x30,0x60,0x00,0x00,
};
char zuo12S[]={
/* 以下是 '作' 的 12点阵宋体 字模,24 byte */
0x12,0x00,0x12,0x00,0x27,0xE0,0x6A,0x00,
0xB2,0x00,0x23,0xC0,0x22,0x00,0x22,0x00,
0x23,0xE0,0x22,0x00,0x22,0x00,0x00,0x00,
};
char zhe12S[]={
/* 以下是 '者' 的 12点阵宋体 字模,24 byte */
0x08,0x40,0x7F,0x80,0x09,0x00,0x0A,0x40,
0xFF,0xE0,0x08,0x00,0x1F,0x80,0x30,0x80,
0xDF,0x80,0x10,0x80,0x1F,0x80,0x00,0x00,
};
char zhi12S[]={
/* 以下是 '志' 的 12点阵宋体 字模,24 byte */
0x04,0x00,0x04,0x40,0xFF,0xE0,0x04,0x00,
0x04,0x00,0x7F,0xC0,0x00,0x00,0x54,0x40,
0x52,0x20,0x90,0xA0,0x1F,0x80,0x00,0x00,
};
char ping12S[]={
/* 以下是 '平' 的 12点阵宋体 字模,24 byte */
0x7F,0xC0,0x04,0x00,0x24,0x80,0x14,0x80,
0x15,0x00,0xFF,0xE0,0x04,0x00,0x04,0x00,
0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00,
};
Hline(1,161,1,45);
Hline(161,319,1,45);
Hline(1,319,181,45);
Hline(162,279,45,45);
Hline(162,279,90,45);
Hline(162,279,135,45);
Sline(1,181,1,45);
Sline(1,181,161,45);
Sline(1,181,280,45);
Sline(1,181,319,45);
drawmat(e12S,12,292,5,48);
drawmat(luo12S,12,292,20,48);
drawmat(si12S,12,292,35,48);
drawmat(fang12S,12,292,50,48);
drawmat(kuai12S,12,292,65,48);
drawmat(zuo12S,12,292,100,48);
drawmat(zhe12S,12,292,115,48);
drawmat(zhi12S,12,292,145,48);
drawmat(ping12S,12,292,160,48);
String_Show(180,15,48,"Level:");
String_Show(180,61,48,"Next:");
String_Show(180,151,48,"Score:");
}/*画界面*/
int Color(int x,int y)
{if(y<0)return 0;
else {color= video_buffer[((y)<<6)+((y)<<8)+(x)];return color;}
}
void Draw_Cube(int x[],int y[],char color)
{fillrectangle(x[0],y[0],x[0]+8,y[0]+8,color);
fillrectangle(x[1],y[1],x[1]+8,y[1]+8,color);
fillrectangle(x[2],y[2],x[2]+8,y[2]+8,color);
fillrectangle(x[3],y[3],x[3]+8,y[3]+8,color);
}/*画方块*/
void Clean_Cube(int x[],int y[])
{fillrectangle(x[0],y[0],x[0]+8,y[0]+8,0);
fillrectangle(x[1],y[1],x[1]+8,y[1]+8,0);
fillrectangle(x[2],y[2],x[2]+8,y[2]+8,0);
fillrectangle(x[3],y[3],x[3]+8,y[3]+8,0);
}/*清除方块*/
void Preview(color)
{fillrectangle(cube2.x[0]+138,cube2.y[0]+130,cube2.x[0]+8+138,cube2.y[0]+8+130,color);
fillrectangle(cube2.x[1]+138,cube2.y[1]+130,cube2.x[1]+8+138,cube2.y[1]+8+130,color);
fillrectangle(cube2.x[2]+138,cube2.y[2]+130,cube2.x[2]+8+138,cube2.y[2]+8+130,color);
fillrectangle(cube2.x[3]+138,cube2.y[3]+130,cube2.x[3]+8+138,cube2.y[3]+8+130,color);
}/*方块下一个显示*/
void Cube_Value(struct CUBE *cube)
{ switch(cube->kind)
{case 0:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=92;
cube->y[0]=-8;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=15;break;
case 1 :
cube->x[0]=72;cube->x[1]=72;cube->x[2]=72;cube->x[3]=72;
cube->y[0]=-38;cube->y[1]=-28;cube->y[2]=-18;cube->y[3]=-8;
cube->color=15;break;
case 2:
cube->x[0]=72;cube->x[1]=62;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=2;break;
case 3:
cube->x[0]=82;cube->x[1]=72;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=2;break;
case 4:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=72;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=2;break;
case 5:
cube->x[0]=72;cube->x[1]=72;cube->x[2]=82;cube->x[3]=72;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=2;break;
case 6:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=4;break;
case 7:
cube->x[0]=72;cube->x[1]=72;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=11;break;
case 8:
cube->x[0]=82;cube->x[1]=62;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=11;break;
case 9:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-28;cube->y[2]=-18;cube->y[3]=-8;
cube->color=11;break;
case 10:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=62;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=11;break;
case 11:
cube->x[0]=82;cube->x[1]=82;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=14;break;
case 12:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=14;break;
case 13:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=72;cube->x[3]=72;
cube->y[0]=-28;cube->y[1]=-28;cube->y[2]=-18;cube->y[3]=-8;
cube->color=14;break;
case 14:
cube->x[0]=62;cube->x[1]=62;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-8;cube->y[2]=-8;cube->y[3]=-8;
cube->color=14;break;
case 15:
cube->x[0]=82;cube->x[1]=72;cube->x[2]=82;cube->x[3]=72;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=15;break;
case 16:
cube->x[0]=62;cube->x[1]=72;cube->x[2]=72;cube->x[3]=82;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=15;break;
case 17:
cube->x[0]=72;cube->x[1]=72;cube->x[2]=82;cube->x[3]=82;
cube->y[0]=-28;cube->y[1]=-18;cube->y[2]=-18;cube->y[3]=-8;
cube->color=48;break;
case 18:
cube->x[0]=72;cube->x[1]=82;cube->x[2]=62;cube->x[3]=72;
cube->y[0]=-18;cube->y[1]=-18;cube->y[2]=-8;cube->y[3]=-8;
cube->color=48;break;
}
}/*方块值初始化*/
void Cube_Judge()
{int x,y,i,n=0;
for(y=cube.y[3];y>=cube.y[0];y-=10)
{for(x=2;x<=152;x+=10)
if(Color(x,y)==0)break;
if(x>152)
{n++;
fillrectangle(2,y,160,y+8,0);
}
else if(x<=152&&n!=0)
{for(i=2;i<=152;i+=10)
if(Color(i,y)!=0)fillrectangle(i,y+n*10,i+8,y+n*10+8,color);
fillrectangle(2,y,160,y+8,0);
}
}
if(n!=0)
{for(y=cube.y[0]-10;y>=miny;y-=10)
for(x=2;x<=152;x+=10)
if(Color(x,y)!=0)
{fillrectangle(x,y+n*10,x+8,y+n*10+8,color);
fillrectangle(x,y,x+8,y+8,0);
}
miny=miny+n*10;
Score+=10*n;
Level=Score/200;
sprintf(score,"%d",Score);
sprintf(level,"%d",Level);
String_Show(240,15,48,level);
String_Show(240,151,48,score);
}
}/*方块下落及消除*/
void Game_Over()
{String_Show(40,90,40,"Game Over!");
getch();
exit(0);
}/*游戏结束!*/
void Symbol_Value()
{int i;
for(i=0;i<4;i++)
point(cube.x[i],cube.y[i],cube.color);
if(miny>cube.y[0])miny=cube.y[0];
elsfk='n';
Cube_Judge();
if(miny<=0)
Game_Over();
}/*符号赋值*/
void Cube_Change(int kind)
{ if(kind==0&&cube.y[0]+30<=172&&Color(cube.x[1],cube.y[0]+10)==0&&Color(cube.x[1],cube.y[0]+20)==0&&Color(cube.x[1],cube.y[1]+30)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]=cube.x[1];cube.x[2]=cube.x[0];cube.x[3]=cube.x[0];
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1]+10;cube.y[3]=cube.y[2]+10;
cube.kind=1;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==1&&cube.x[0]-10>=2&&cube.x[3]+20<=152&&Color(cube.x[0]-10,cube.y[0])==0&&Color(cube.x[3]+20,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]-=10;cube.x[2]+=10;cube.x[3]+=20;cube.y[1]=cube.y[0];cube.y[2]=cube.y[0];cube.y[3]=cube.y[0];
cube.kind=0;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==2&&Color(cube.x[0],cube.y[2]+10)==0&&cube.y[2]+10<=172)
{Clean_Cube(cube.x,cube.y);
cube.x[3]=cube.x[2];
cube.y[3]=cube.y[2]+10;
cube.kind=3;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==3&&Color(cube.x[0]-10,cube.y[0])==0&&cube.x[0]+10<=152&&Color(cube.x[0]+10,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]-=10;cube.x[1]+=10;cube.x[2]+=10;cube.y[1]-=10;cube.y[2]-=10;cube.y[3]-=10;
cube.kind=4;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==4&&Color(cube.x[3]+10,cube.y[3])==0&&Color(cube.x[3],cube.y[3]+10)==0&&cube.y[3]+10<=172)
{Clean_Cube(cube.x,cube.y);
cube.x[0]+=10;cube.y[1]+=10;cube.y[2]+=10;cube.y[3]+=10;
cube.kind=5;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==5&&cube.x[0]-10>=2&&Color(cube.x[0]-10,cube.y[2])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[3]=cube.x[2];cube.y[3]=cube.y[2];cube.x[1]-=10;cube.x[2]-=10;
cube.kind=2;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==7&&cube.x[0]-20>=2&&Color(cube.x[0]-20,cube.y[1])==0&&Color(cube.x[0]-10,cube.y[1])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]-20;cube.x[2]=cube.x[0]-10;cube.x[3]=cube.x[0];
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1];cube.y[3]=cube.y[2];
cube.kind=8;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==8&&cube.y[1]+10<=172&&cube.x[0]+10<=152&&Color(cube.x[0]+10,cube.y[0])==0&&Color(cube.x[0]+10,cube.y[1])==0&&Color(cube.x[0],cube.y[1]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]+10;cube.x[2]=cube.x[1];cube.x[3]=cube.x[1];
cube.y[1]=cube.y[0];cube.y[2]=cube.y[0]+10;cube.y[3]=cube.y[2]+10;
cube.kind=9;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==9&&cube.x[1]+10<=152&&Color(cube.x[0],cube.y[2])==0&&Color(cube.x[1]+10,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]+10;cube.x[2]=cube.x[1]+10;cube.x[3]=cube.x[0];
cube.y[1]=cube.y[0];cube.y[2]=cube.y[0];cube.y[3]=cube.y[0]+10;
cube.kind=10;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==10&&cube.y[3]+10<=172&&Color(cube.x[0],cube.y[3]+10)==0&&Color(cube.x[1],cube.y[3]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0];cube.x[2]=cube.x[0];cube.x[3]=cube.x[0]+10;
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1]+10;cube.y[3]=cube.y[2];
cube.kind=7;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==11&&cube.x[0]+10<=152&&Color(cube.x[3],cube.y[0])==0&&Color(cube.x[0]+10,cube.y[0])==0&&Color(cube.x[0]+10,cube.y[1])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0];cube.x[0]-=10;cube.x[2]=cube.x[1]+10;cube.x[3]=cube.x[2];cube.y[1]=cube.y[0];cube.y[2]=cube.y[0];cube.y[3]=cube.y[0]+10;
cube.kind=12;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==12&&cube.y[3]+10<=172&&Color(cube.x[1],cube.y[3])==0&&Color(cube.x[1],cube.y[3]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]+=10;cube.x[1]+=10;cube.x[2]-=10;cube.x[3]-=10;cube.y[2]=cube.y[3];cube.y[3]+=10;
cube.kind=13;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==13&&cube.x[1]+10<=152&&Color(cube.x[1],cube.y[2])==0&&Color(cube.x[1]+10,cube.y[2])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]-=10;cube.x[2]+=10;cube.x[3]+=20;cube.y[1]+=10;cube.y[3]=cube.y[2];
cube.kind=14;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==14&&cube.x[0]-10>=2&&cube.y[1]+10<=172&&Color(cube.x[0]-10,cube.y[1]+10)==0&&Color(cube.x[0],cube.y[1]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[2]-=20;cube.x[3]-=20;cube.y[2]+=10;cube.y[3]+=10;
cube.kind=11;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==15&&cube.x[0]+10<=152&&Color(cube.x[0]-10,cube.y[0])==0&&Color(cube.x[0]+10,cube.y[2])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]-=10;cube.x[1]+=10;cube.x[3]+=20;cube.y[1]-=10;cube.y[3]-=10;
cube.kind=16;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==16&&cube.y[3]+10<=172&&Color(cube.x[0],cube.y[2])==0&&Color(cube.x[0],cube.y[2]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[0]+=10;cube.x[1]-=10;cube.x[3]=cube.x[1];cube.y[1]+=10;cube.y[3]+=10;
cube.kind=15;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==17&&cube.x[0]-10>=2&&Color(cube.x[0]-10,cube.y[1])==0&&Color(cube.x[0]+10,cube.y[0])==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0]+10;cube.x[2]=cube.x[0];cube.x[3]=cube.x[0]-10;
cube.y[1]=cube.y[0];cube.y[2]=cube.y[0]+10;cube.y[3]=cube.y[2];
cube.kind=18;
Draw_Cube(cube.x,cube.y,cube.color);
}
if(kind==18&&cube.y[3]+10<=172&&Color(cube.x[1],cube.y[2])==0&&Color(cube.x[1],cube.y[2]+10)==0)
{Clean_Cube(cube.x,cube.y);
cube.x[1]=cube.x[0];cube.x[2]=cube.x[0]+10;cube.x[3]=cube.x[2];
cube.y[1]=cube.y[0]+10;cube.y[2]=cube.y[1];cube.y[3]=cube.y[2]+10;
cube.kind=17;
Draw_Cube(cube.x,cube.y,cube.color);
}
} /*方块变形*/
void Game_Play()
{int key,i,n=0;
struct CUBE * AA;
sprintf(score,"%d",Score);
sprintf(level,"%d",Level);
String_Show(240,15,48,level);
String_Show(240,151,48,score);
randomize();
cube.kind=rand()%19;AA=&cube;
Cube_Value(AA);
while(1)
{ while(!kbhit())
{Draw_Cube(cube.x,cube.y,cube.color);/*画出方块*/
if(elsfk=='n')
{randomize();
cube2.kind=rand()%19;
AA=&cube2;
Cube_Value(AA);/*方块赋值*/
Preview(cube2.color);
elsfk='y';
}
Delay(1);/*延时*/
n++;
if(n==11-Level)
{if(Color(cube.x[0],cube.y[0]+10)==0&&Color(cube.x[1],cube.y[1]+10)==0&&Color(cube.x[2],cube.y[2]+10)==0&&Color(cube.x[3],cube.y[3]+10)==0&&cube.y[3]<172)
{Clean_Cube(cube.x,cube.y);/*清除方块*/
for(i=0;i<4;i++)
cube.y[i]+=10;/*方块下落*/
}
else {Symbol_Value();Preview(0);cube=cube2;}
n=0;
}
}
key=bioskey(0);/*接收按键信息*/
if(elsfk=='y')
{Draw_Cube(cube.x,cube.y,cube.color);/*画出方块*/
if(key==UP||key==SPACE)
{Cube_Change(cube.kind);
}
/*上键或空格键变形*/
if(key==DOWN)
{if(Color(cube.x[0],cube.y[0]+10)==0&&Color(cube.x[1],cube.y[1]+10)==0&&Color(cube.x[2],cube.y[2]+10)==0&&Color(cube.x[3],cube.y[3]+10)==0&&cube.y[3]<172)
{Clean_Cube(cube.x,cube.y);/*清除方块*/
for(i=0;i<4;i++)
cube.y[i]+=10;/*方块下落*/
Draw_Cube(cube.x,cube.y,cube.color);/*画出方块*/
}
else {Symbol_Value();Preview(0);cube=cube2;}
}/*下键加速下落*/
if(key==LEFT)
{if(cube.x[0]-10>=2&&cube.x[1]-10>=2&&cube.x[2]-10>=2&&cube.x[3]-10>=2)
if(Color(cube.x[0]-10,cube.y[0])==0&&Color(cube.x[1]-10,cube.y[1])==0&&Color(cube.x[2]-10,cube.y[2])==0&&Color(cube.x[3]-10,cube.y[3])==0)
{Clean_Cube(cube.x,cube.y);
for(i=0;i<4;i++)
cube.x[i]-=10;
Draw_Cube(cube.x,cube.y,cube.color);
}
}/*左键方块左移*/
if(key==RIGHT)
{if(cube.x[0]+10<=152&&cube.x[1]+10<=152&&cube.x[2]+10<=152&&cube.x[3]+10<=152)
if(Color(cube.x[0]+10,cube.y[0])==0&&Color(cube.x[1]+10,cube.y[1])==0&&Color(cube.x[2]+10,cube.y[2])==0&&Color(cube.x[3]+10,cube.y[3])==0)
{Clean_Cube(cube.x,cube.y);
for(i=0;i<4;i++)
cube.x[i]+=10;
Draw_Cube(cube.x,cube.y,cube.color);
}
}/*右键方块右移*/
}
if(key==ESC)
{Set_Video_Mode(0x03);
exit(0);
}/*Esc结束游戏*/
if(key==F5)getch();/*F5暂停游戏*/
}/*游戏进行中*/
}
void main()
{Set_Video_Mode(0x13);
Game_Draw();
Game_Play();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
#define MINBOXSIZE 15 /* 最小方块的尺寸 */
#define BGCOLOR 7 /* 背景着色 */
#define GX 200
#define GY 10
#define SJNUM 10000 /* 每当玩家打到一万分等级加一级*/
/* 按键码*/
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_HOME 0x4700
#define VK_END 0x4f00
#define VK_SPACE 0x3920
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
/* 定义俄罗斯方块的方向(我定义他为4种)*/
#define F_DONG 0
#define F_NAN 1
#define F_XI 2
#define F_BEI 3
#define NEXTCOL 20 /* 要出的下一个方块的纵坐标*/
#define NEXTROW 12 /* 要出的下一个方块的横从标*/
#define MAXROW 14 /* 游戏屏幕大小*/
#define MAXCOL 20
#define SCCOL 100 /*游戏屏幕大显示器上的相对位置*/
#define SCROW 60
int gril[22][16]; /* 游戏屏幕坐标*/
int col=1,row=7; /* 当前方块的横纵坐标*/
int boxfx=0,boxgs=0; /* 当前寺块的形壮和方向*/
int nextboxfx=0,nextboxgs=0,maxcol=22;/*下一个方块的形壮和方向*/
int minboxcolor=6,nextminboxcolor=6;
int num=0; /*游戏分*/
int dj=0,gamedj[10]={18,16,14,12,10,8,6,4,2,1};/* 游戏等级*/
/* 以下我用了一个3维数组来纪录方块的最初形状和方向*/
int boxstr[7][4][16]={{
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,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,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},
{
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}},
{
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
{1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{
{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}},
{
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}},
{
{1,1,0,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},
{1,1,0,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}},
{
{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
{0,1,0,0,1,1,1,0,0,0,0,0.0,0,0,0},
{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}}
};
/* 随机得到当前方块和下一个方块的形状和方向*/
void boxrad(){
minboxcolor=nextminboxcolor;
boxgs=nextboxgs;
boxfx=nextboxfx;
nextminboxcolor=random(14)+1;
if(nextminboxcolor==4||nextminboxcolor==7||nextminboxcolor==8)
nextminboxcolor=9;
nextboxfx=F_DONG;
nextboxgs=random(7);
}
/*初始化图形模试*/
void init(int gdrive,int gmode){
int errorcode;
initgraph(&gdrive,&gmode,"D:\\tc\\");
errorcode=graphresult();
if(errorcode!=grOk){
printf("error of: %s",grapherrormsg(errorcode));
exit(1);
}
}
/* 在图形模式下的清屏 */
void cls()
{
setfillstyle(SOLID_FILL,0);
setcolor(0);
bar(0,0,640,480);
}
/*在图形模式下的高级清屏*/
void clscr(int a,int b,int c,int d,int color){
setfillstyle(SOLID_FILL,color);
setcolor(color);
bar(a,b,c,d);
}
/*最小方块的绘制*/
void minbox(int asc,int bsc,int color,int bdcolor){
int a=0,b=0;
a=SCCOL+asc;
b=SCROW+bsc;
clscr(a+1,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE,color);
if(color!=BGCOLOR){
setcolor(bdcolor);
line(a+1,b+1,a-1+MINBOXSIZE,b+1);
line(a+1,b+1,a+1,b-1+MINBOXSIZE);
line(a-1+MINBOXSIZE,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE);
line(a+1,b-1+MINBOXSIZE,a-1+MINBOXSIZE,b-1+MINBOXSIZE);
}
}
/*游戏中出现的文字*/
void txt(int a,int b,char *txt,int font,int color){
setcolor(color);
settextstyle(0,0,font);
outtextxy(a,b,txt);
}
/*windows 绘制*/
void win(int a,int b,int c,int d,int bgcolor,int bordercolor){
clscr(a,b,c,d,bgcolor);
setcolor(bordercolor);
line(a,b,c,b);
line(a,b,a,d);
line(a,d,c,d);
line(c,b,c,d);
}
/* 当前方块的绘制*/
void funbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(i=0;i<16;i++)
boxz[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(boxz[i][j]==1)
minbox((j+row+a)*MINBOXSIZE,(i+col+b)*MINBOXSIZE,color,bdcolor);
}
/*下一个方块的绘制*/
void nextfunbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(i=0;i<16;i++)
boxz[i/4][i%4]=boxstr[nextboxgs][nextboxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(boxz[i][j]==1)
minbox((j+a)*MINBOXSIZE,(i+b)*MINBOXSIZE,color,bdcolor);
}
/*时间中断定义*/
#define TIMER 0x1c
int TimerCounter=0;
void interrupt ( *oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS){
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS)){
oldhandler=getvect(TIMER);
disable();
setvect(TIMER,IntProc);
enable();
}
/*由于游戏的规则,消掉都有最小方块的一行*/
void delcol(int a){
int i,j;
for(i=a;i>1;i--)
for(j=1;j<15;j++){
minbox(j*MINBOXSIZE,i*MINBOXSIZE,BGCOLOR,BGCOLOR);
gril[i][j]=gril[i-1][j];
if(gril[i][j]==1)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,minboxcolor,0);
}
}
/*消掉所有都有最小方块的行*/
void delete(){
int i,j,zero,delgx=0;
char *nm="00000";
for(i=1;i<21;i++){
zero=0;
for(j=1;j<15;j++)
if(gril[i][j]==0)
zero=1;
if(zero==0){
delcol(i);
delgx++;
}
}
num=num+delgx*delgx*10;
dj=num/10000;
sprintf(nm,"%d",num);
clscr(456,173,500,200,4);
txt(456,173,"Number:",1,15);
txt(456,193,nm,1,15);
}
/*时间中断结束*/
void KillTimer(){
disable();
setvect(TIMER,oldhandler);
enable();
}
/* 测试当前方块是否可以向下落*/
int downok(){
int i,j,k=1,a[4][4];
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j] && gril[col+i+1][row+j])
k=0;
return(k);
}
/* 测试当前方块是否可以向左行*/
int leftok(){
int i,j,k=1,a[4][4];
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j] && gril[col+i][row+j-1])
k=0;
return(k);
}
/* 测试当前方块是否可以向右行*/
int rightok(){
int i,j,k=1,a[4][4];
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j] && gril[col+i][row+j+1])
k=0;
return(k);
}
/* 测试当前方块是否可以变形*/
int upok(){
int i,j,k=1,a[4][4];
for(i=0;i<4;i++)
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx+1][i];
for(i=3;i>=0;i--)
for(j=3;j>=0;j--)
if(a[i][j] && gril[col+i][row+j])
k=0;
return(k);
}
/*当前方块落下之后,给屏幕坐标作标记*/
void setgril(){
int i,j,a[4][4];
funbox(0,0,minboxcolor,0);
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j])
gril[col+i][row+j]=1;
col=1;row=7;
}
/*游戏结束*/
void gameover(){
int i,j;
for(i=20;i>0;i--)
for(j=1;j<15;j++)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,2,0);
txt(103,203,"Game Over",3,10);
}
/*按键的设置*/
void call_key(int keyx){
switch(keyx){
case VK_DOWN: { /*下方向键,横坐标加一。*/
if(downok()){
col++;
funbox(0,0,minboxcolor,0);}
else{
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
}
break;
}
case VK_UP: { /*上方向键,方向形状旋转90度*/
if(upok())
boxfx++;
if(boxfx>3)
boxfx=0;
funbox(0,0,minboxcolor,0);
break;
}
case VK_LEFT:{ /*左方向键,纵坐标减一*/
if(leftok())
row--;
funbox(0,0,minboxcolor,0);
break;
}
case VK_RIGHT:{ /*右方向键,纵坐标加一*/
if(rightok())
row++;
funbox(0,0,minboxcolor,0);
break;
}
case VK_SPACE: /*空格键,直接落到最后可以落到的们置*/
while(downok())
col++;
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
break;
default:
{
txt(423,53,"worng key!",1,4);
txt(428,80,"Plese Enter Anly Key AG!",1,4);
getch();
clscr(420,50,622,97,BGCOLOR);
}
}
}
/*时间中断开始*/
void timezd(void){
int key;
SetTimer(newhandler);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
for(;;){
if(bioskey(1)){
key=bioskey(0);
funbox(0,0,BGCOLOR,BGCOLOR);
if(key==VK_ESC)
break;
call_key(key);
}
if(TimerCounter>gamedj[dj]){
TimerCounter=0;
if(downok()){
funbox(0,0,BGCOLOR,BGCOLOR);
col++;
funbox(0,0,minboxcolor,0);
}
else {
if(col==1){
gameover();
getch();
break;
}
setgril();
delete();
funbox(0,0,minboxcolor,0);
col=1;row=7;
funbox(0,0,BGCOLOR,BGCOLOR);
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
}
}
}
}
/*主程序开始*/
void main(void){
int i,j;
char *nm="00000";
init(VGA,VGAHI);
cls();
/*屏幕坐标初始化*/
for(i=0;i<=MAXCOL+1;i++)
for(j=0;j<=MAXROW+1;j++)
gril[i][j]=0;
for(i=0;i<=MAXCOL+1;i++) {
gril[i][0]=1;
gril[i][15]=1;
}
for(j=1;j<=MAXROW;j++){
gril[0][j]=1;
gril[21][j]=1;
}
clscr(0,0,640,480,15);
win(1,1,639,479,4,15);
win(SCCOL+MINBOXSIZE-2,SCROW+MINBOXSIZE-2,SCCOL+15*MINBOXSIZE+2,SCROW+21*MINBOXSIZE+2,BGCOLOR,0);
nextboxgs=random(8);
nextboxfx=random(4);
sprintf(nm,"%d",num);
txt(456,173,"Number:",1,15);
txt(456,193,nm,1,15);
txt(456,243,"Next Box:",1,15);
timezd();
KillTimer();
closegraph();
}
#include <dos.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
#define MINBOXSIZE 15 /* 最小方块的尺寸 */
#define BGCOLOR 7 /* 背景着色 */
#define GX 200
#define GY 10
#define SJNUM 10000 /* 每当玩家打到一万分等级加一级*/
/* 按键码*/
#define VK_LEFT 0x4b00
#define VK_RIGHT 0x4d00
#define VK_DOWN 0x5000
#define VK_UP 0x4800
#define VK_HOME 0x4700
#define VK_END 0x4f00
#define VK_SPACE 0x3920
#define VK_ESC 0x011b
#define VK_ENTER 0x1c0d
/* 定义俄罗斯方块的方向(我定义他为4种)*/
#define F_DONG 0
#define F_NAN 1
#define F_XI 2
#define F_BEI 3
#define NEXTCOL 20 /* 要出的下一个方块的纵坐标*/
#define NEXTROW 12 /* 要出的下一个方块的横从标*/
#define MAXROW 14 /* 游戏屏幕大小*/
#define MAXCOL 20
#define SCCOL 100 /*游戏屏幕大显示器上的相对位置*/
#define SCROW 60
int gril[22][16]; /* 游戏屏幕坐标*/
int col=1,row=7; /* 当前方块的横纵坐标*/
int boxfx=0,boxgs=0; /* 当前寺块的形壮和方向*/
int nextboxfx=0,nextboxgs=0,maxcol=22;/*下一个方块的形壮和方向*/
int minboxcolor=6,nextminboxcolor=6;
int num=0; /*游戏分*/
int dj=0,gamedj[10]={18,16,14,12,10,8,6,4,2,1};/* 游戏等级*/
/* 以下我用了一个3维数组来纪录方块的最初形状和方向*/
int boxstr[7][4][16]={{
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,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,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},
{
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}},
{
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
{1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{
{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}},
{
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0}},
{
{1,1,0,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},
{1,1,0,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}},
{
{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
{0,1,0,0,1,1,1,0,0,0,0,0.0,0,0,0},
{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}}
};
/* 随机得到当前方块和下一个方块的形状和方向*/
void boxrad(){
minboxcolor=nextminboxcolor;
boxgs=nextboxgs;
boxfx=nextboxfx;
nextminboxcolor=random(14)+1;
if(nextminboxcolor==4||nextminboxcolor==7||nextminboxcolor==8)
nextminboxcolor=9;
nextboxfx=F_DONG;
nextboxgs=random(7);
}
/*初始化图形模试*/
void init(int gdrive,int gmode){
int errorcode;
initgraph(&gdrive,&gmode,"D:\\tc\\");
errorcode=graphresult();
if(errorcode!=grOk){
printf("error of: %s",grapherrormsg(errorcode));
exit(1);
}
}
/* 在图形模式下的清屏 */
void cls()
{
setfillstyle(SOLID_FILL,0);
setcolor(0);
bar(0,0,640,480);
}
/*在图形模式下的高级清屏*/
void clscr(int a,int b,int c,int d,int color){
setfillstyle(SOLID_FILL,color);
setcolor(color);
bar(a,b,c,d);
}
/*最小方块的绘制*/
void minbox(int asc,int bsc,int color,int bdcolor){
int a=0,b=0;
a=SCCOL+asc;
b=SCROW+bsc;
clscr(a+1,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE,color);
if(color!=BGCOLOR){
setcolor(bdcolor);
line(a+1,b+1,a-1+MINBOXSIZE,b+1);
line(a+1,b+1,a+1,b-1+MINBOXSIZE);
line(a-1+MINBOXSIZE,b+1,a-1+MINBOXSIZE,b-1+MINBOXSIZE);
line(a+1,b-1+MINBOXSIZE,a-1+MINBOXSIZE,b-1+MINBOXSIZE);
}
}
/*游戏中出现的文字*/
void txt(int a,int b,char *txt,int font,int color){
setcolor(color);
settextstyle(0,0,font);
outtextxy(a,b,txt);
}
/*windows 绘制*/
void win(int a,int b,int c,int d,int bgcolor,int bordercolor){
clscr(a,b,c,d,bgcolor);
setcolor(bordercolor);
line(a,b,c,b);
line(a,b,a,d);
line(a,d,c,d);
line(c,b,c,d);
}
/* 当前方块的绘制*/
void funbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(i=0;i<16;i++)
boxz[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(boxz[i][j]==1)
minbox((j+row+a)*MINBOXSIZE,(i+col+b)*MINBOXSIZE,color,bdcolor);
}
/*下一个方块的绘制*/
void nextfunbox(int a,int b,int color,int bdcolor){
int i,j;
int boxz[4][4];
for(i=0;i<16;i++)
boxz[i/4][i%4]=boxstr[nextboxgs][nextboxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(boxz[i][j]==1)
minbox((j+a)*MINBOXSIZE,(i+b)*MINBOXSIZE,color,bdcolor);
}
/*时间中断定义*/
#define TIMER 0x1c
int TimerCounter=0;
void interrupt ( *oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS){
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS)){
oldhandler=getvect(TIMER);
disable();
setvect(TIMER,IntProc);
enable();
}
/*由于游戏的规则,消掉都有最小方块的一行*/
void delcol(int a){
int i,j;
for(i=a;i>1;i--)
for(j=1;j<15;j++){
minbox(j*MINBOXSIZE,i*MINBOXSIZE,BGCOLOR,BGCOLOR);
gril[i][j]=gril[i-1][j];
if(gril[i][j]==1)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,minboxcolor,0);
}
}
/*消掉所有都有最小方块的行*/
void delete(){
int i,j,zero,delgx=0;
char *nm="00000";
for(i=1;i<21;i++){
zero=0;
for(j=1;j<15;j++)
if(gril[i][j]==0)
zero=1;
if(zero==0){
delcol(i);
delgx++;
}
}
num=num+delgx*delgx*10;
dj=num/10000;
sprintf(nm,"%d",num);
clscr(456,173,500,200,4);
txt(456,173,"Number:",1,15);
txt(456,193,nm,1,15);
}
/*时间中断结束*/
void KillTimer(){
disable();
setvect(TIMER,oldhandler);
enable();
}
/* 测试当前方块是否可以向下落*/
int downok(){
int i,j,k=1,a[4][4];
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j] && gril[col+i+1][row+j])
k=0;
return(k);
}
/* 测试当前方块是否可以向左行*/
int leftok(){
int i,j,k=1,a[4][4];
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j] && gril[col+i][row+j-1])
k=0;
return(k);
}
/* 测试当前方块是否可以向右行*/
int rightok(){
int i,j,k=1,a[4][4];
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j] && gril[col+i][row+j+1])
k=0;
return(k);
}
/* 测试当前方块是否可以变形*/
int upok(){
int i,j,k=1,a[4][4];
for(i=0;i<4;i++)
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx+1][i];
for(i=3;i>=0;i--)
for(j=3;j>=0;j--)
if(a[i][j] && gril[col+i][row+j])
k=0;
return(k);
}
/*当前方块落下之后,给屏幕坐标作标记*/
void setgril(){
int i,j,a[4][4];
funbox(0,0,minboxcolor,0);
for(i=0;i<16;i++)
a[i/4][i%4]=boxstr[boxgs][boxfx][i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i][j])
gril[col+i][row+j]=1;
col=1;row=7;
}
/*游戏结束*/
void gameover(){
int i,j;
for(i=20;i>0;i--)
for(j=1;j<15;j++)
minbox(j*MINBOXSIZE,i*MINBOXSIZE,2,0);
txt(103,203,"Game Over",3,10);
}
/*按键的设置*/
void call_key(int keyx){
switch(keyx){
case VK_DOWN: { /*下方向键,横坐标加一。*/
if(downok()){
col++;
funbox(0,0,minboxcolor,0);}
else{
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
}
break;
}
case VK_UP: { /*上方向键,方向形状旋转90度*/
if(upok())
boxfx++;
if(boxfx>3)
boxfx=0;
funbox(0,0,minboxcolor,0);
break;
}
case VK_LEFT:{ /*左方向键,纵坐标减一*/
if(leftok())
row--;
funbox(0,0,minboxcolor,0);
break;
}
case VK_RIGHT:{ /*右方向键,纵坐标加一*/
if(rightok())
row++;
funbox(0,0,minboxcolor,0);
break;
}
case VK_SPACE: /*空格键,直接落到最后可以落到的们置*/
while(downok())
col++;
funbox(0,0,minboxcolor,0);
setgril();
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
delete();
break;
default:
{
txt(423,53,"worng key!",1,4);
txt(428,80,"Plese Enter Anly Key AG!",1,4);
getch();
clscr(420,50,622,97,BGCOLOR);
}
}
}
/*时间中断开始*/
void timezd(void){
int key;
SetTimer(newhandler);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
for(;;){
if(bioskey(1)){
key=bioskey(0);
funbox(0,0,BGCOLOR,BGCOLOR);
if(key==VK_ESC)
break;
call_key(key);
}
if(TimerCounter>gamedj[dj]){
TimerCounter=0;
if(downok()){
funbox(0,0,BGCOLOR,BGCOLOR);
col++;
funbox(0,0,minboxcolor,0);
}
else {
if(col==1){
gameover();
getch();
break;
}
setgril();
delete();
funbox(0,0,minboxcolor,0);
col=1;row=7;
funbox(0,0,BGCOLOR,BGCOLOR);
nextfunbox(NEXTCOL,NEXTROW,4,4);
boxrad();
nextfunbox(NEXTCOL,NEXTROW,nextminboxcolor,0);
}
}
}
}
/*主程序开始*/
void main(void){
int i,j;
char *nm="00000";
init(VGA,VGAHI);
cls();
/*屏幕坐标初始化*/
for(i=0;i<=MAXCOL+1;i++)
for(j=0;j<=MAXROW+1;j++)
gril[i][j]=0;
for(i=0;i<=MAXCOL+1;i++) {
gril[i][0]=1;
gril[i][15]=1;
}
for(j=1;j<=MAXROW;j++){
gril[0][j]=1;
gril[21][j]=1;
}
clscr(0,0,640,480,15);
win(1,1,639,479,4,15);
win(SCCOL+MINBOXSIZE-2,SCROW+MINBOXSIZE-2,SCCOL+15*MINBOXSIZE+2,SCROW+21*MINBOXSIZE+2,BGCOLOR,0);
nextboxgs=random(8);
nextboxfx=random(4);
sprintf(nm,"%d",num);
txt(456,173,"Number:",1,15);
txt(456,193,nm,1,15);
txt(456,243,"Next Box:",1,15);
timezd();
KillTimer();
closegraph();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询