📄 tc
字号:
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=-1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=6;
last->next=info;
last=info;
info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=6;
last->next=info;
last=info;
info=(struct diamond *)malloc(sizeof(struct diamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=6;
last->next=info;
last=info;
last->next=first;
for (i=0;i<=random(4);i++)
{
first=first->next;
}
return first;
}
/*****************************************************************************************/
struct diamond *create_Z()
{
struct diamond *info;
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
info=(struct diamond *)malloc(sizeof(struct diamond));
first->next=info;
info->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=-1;
first->y[1]=0;
first->x[2]=0;
first->y[2]=1;
first->x[3]=1;
first->y[3]=1;
first->start_x=5;
first->start_y=2;
first->color=9;
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=9;
if(random(2)==0) {return first;}
else {return first->next;}
}
/*****************************************************************************************/
struct diamond *create_N()
{
struct diamond *info;
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
info=(struct diamond *)malloc(sizeof(struct diamond));
first->next=info;
info->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=0;
first->y[1]=1;
first->x[2]=-1;
first->y[2]=1;
first->x[3]=1;
first->y[3]=0;
first->start_x=5;
first->start_y=2;
first->color=14;
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=-1;
info->x[2]=1;
info->y[2]=0;
info->x[3]=1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=14;
if(random(2)==0) {return first;}
else {return first->next;}
}
/*****************************************************************************************/
struct diamond *create_H()
{
struct diamond *first;
first=(struct diamond *)malloc(sizeof(struct diamond));
first->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=0;
first->y[1]=1;
first->x[2]=1;
first->y[2]=0;
first->x[3]=1;
first->y[3]=1;
first->start_x=5;
first->start_y=2;
first->color=1;
return first;
}
/*****************************************************************************************/
void show_next ()
{
int nowx;/*记录当前每个格子的位置*/
int nowy;
int i;/*计数器*/
int j;
int haveit;/*当前格子是否已经显示*/
struct diamond *next;/*当前方块的翻转后的下一个状态*/
next=nowinfo->next;
if (next==NULL) {gotoxy(1,1);printf("null");}
for (i=0;i<=3;i++)/*判断是否能够翻转,若不能,就直接退出该函数*/
{
if (grid[x+next->x][y+next->y]==1)
{
return;
}
}
setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (next->x[j]==nowinfo->x&&next->y[j]==nowinfo->y) {haveit=1;break;}
}
if (haveit==0) /*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/
{
grid[x+nowinfo->x][y+nowinfo->y]=0;
if (y+nowinfo->y>=4)/*判断该格子是否到了可以显示的区域*/
floodfill(80+(nowinfo->x+x)*20+1,-20+(nowinfo->y+y)*20+1,color);
}
}
nowinfo=next;
nowx=x;
nowy=y;
setfillstyle(SOLID_FILL,nowinfo->color);/*设置填冲色为方块的颜色*/
for (i=0;i<=3;i++)
{
if (grid[x+nowinfo->x][y+nowinfo->y]!=2)/*如果该格子还没有被显示*/
{
nowx=x+nowinfo->x;
nowy=y+nowinfo->y;
if (y+nowinfo->y>=4)
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2; /*设置该格子当前有活动方块*/
}
}
return;
}
/*****************************************************************************************/
void show_left ()
{
int i;/*计数器*/
int j;
int haveit;/*当前格子是否已经显示*/
int nowx;/*记录当前每个格子的位置*/
int nowy;
for (i=0;i<=3;i++)/*判断是否可以向左移动*/
{
if (grid[x-1+nowinfo->x][y+nowinfo->y]==1)
{
return;
}
}
setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (nowinfo->x==nowinfo->x[j]-1&&nowinfo->y==nowinfo->y[j]) {haveit=1;break;}
}
if (haveit==0) /*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/
{
grid[x+nowinfo->x][y+nowinfo->y]=0;
if (y+nowinfo->y>=4)/*判断该格子是否到了可以显示的区域*/
floodfill(80+(nowinfo->x+x)*20+1,-20+(nowinfo->y+y)*20+1,color);
}
}
setfillstyle(SOLID_FILL,nowinfo->color);/*设置填冲色为方块的颜色*/
for (i=0;i<=3;i++)
{
nowx=x+nowinfo->x-1;
nowy=y+nowinfo->y;
if (grid[nowx][nowy]!=2)/*如果该格子还没有被显示*/
{
if (nowy>=4)
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2;
}
}
x=x-1;
return;
}
/*****************************************************************************************/
void show_right ()
{
int i;/*计数器*/
int j;
int haveit;/*当前格子是否已经显示*/
int nowx;/*记录当前每个格子的位置*/
int nowy;
for (i=0;i<=3;i++)
{
if (grid[x+1+nowinfo->x][y+nowinfo->y]==1)
{
return;/*判断是否可以向右移动*/
}
}
setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (nowinfo->x==nowinfo->x[j]+1&&nowinfo->y==nowinfo->y[j]) {haveit=1;break;}
}
if (haveit==0)/*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/
{
grid[x+nowinfo->x][y+nowinfo->y]=0;
if (y+nowinfo->y>=4)/*判断该格子是否到了可以显示的区域*/
floodfill(80+(nowinfo->x+x)*20+1,-20+(nowinfo->y+y)*20+1,color);
}
}
setfillstyle(SOLID_FILL,nowinfo->color);/*设置填冲色为方块的颜色*/
for (i=0;i<=3;i++)
{
nowx=x+nowinfo->x+1;
nowy=y+nowinfo->y;
if (grid[nowx][nowy]!=2)
{
if (nowy>=4)/*判断该格子是否到了可以显示的区域*/
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2;
}
}
x=x+1;
return;
}
/*****************************************************************************************/
void show_down ()
{
int i;/*计数器*/
int j;
int haveit;/*当前格子是否已经显示*/
int nowx;/*记录当前每个格子的位置*/
int nowy;
int key;
for (i=0;i<=3;i++)
{
if (grid[x+nowinfo->x][y+nowinfo->y+1]==1)/*判断方块是否能够下落*/
{
for (j=0;j<=3;j++)
{
grid[x+nowinfo->x[j]][y+nowinfo->y[j]]=1;
if (y+nowinfo->y[j]<=3)
{/*判断游戏是否已经玩完*/
install(oldtimer);
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Do you want to restart (Y/N)...");
for (;
{
key=bioskey(0);
if (key==YES)
{
startset();
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
return;
}
if (key==NO)
{
closegraph();
exit (0);}
}
}
}
delinfo(nowinfo);
scandel();/*扫描,删除*/
delay(2500);
while(bioskey(1)) bioskey(0);/*清除键盘缓冲区*/
/* clrkey();*/
nowinfo=nextinfo;/*得到新的方块*/
nextinfo=get_diamond();/*得到下一个方块*/
showsubwin(nextinfo);
x=nowinfo->start_x;/*重新设置方块位置*/
y=nowinfo->start_y;
return;
}
}
setfillstyle(SOLID_FILL,backcolor);/*设置背景色以消除不需要的格子*/
for (i=0;i<=3;i++)
{
haveit=0;
for (j=0;j<=3;j++)
{
if (nowinfo->x==nowinfo->x[j]&&nowinfo->y==nowinfo->y[j]+1) {haveit=1;break;}
}
if (haveit==0)/*判断翻转后该格子是否显示,若不显示,将该格子设为背景色*/
{
grid[x+nowinfo->x][y+nowinfo->y]=0;
if (y+nowinfo->y>=4)/*判断该格子是否到了可以显示的区域*/
floodfill(80+(nowinfo->x+x)*20+1,-20+(nowinfo->y+y)*20+1,color);
}
}
setfillstyle(SOLID_FILL,nowinfo->color);/*设置填冲色为方块的颜色*/
for (i=0;i<=3;i++)
{
nowx=x+nowinfo->x;
nowy=y+nowinfo->y+1;
if (grid[nowx][nowy]!=2)/*如果该格子还没有被显示*/
{
if (nowy>=4)
floodfill(80+nowx*20+1,-20+nowy*20+1,color);
grid[nowx][nowy]=2;
}
}
y=y+1;
return;
}
/*****************************************************************************************/
/*void print()测试用函数
{
int i;
int j;
gotoxy(1,1);
for (j=0;j<=22;j++)
{for (i=0;i<=11;i++)
{
printf ("%d",grid[j]);}
printf ("\n");
}
}*/
/*****************************************************************************************/
void scandel()
{
int i;
int k;
int j;
int num;
int scoreadd;
scoreadd=0;
for (k=21;k>=4;k--)
{
num=0;
for (i=1;i<=10;i++)
{
if (grid[k]==1) num++;
}
if (num==10)
{
scoreadd++;
for (i=1;i<=10;i++)
{
setfillstyle(SOLID_FILL,backcolor);
floodfill(80+i*20+1,-20+k*20+1,color);
}
for (j=k;j>=5;j--)
{
for (i=1;i<=10;i++)
{
setfillstyle(SOLID_FILL,getpixel(80+i*20+1,-40+j*20+1));
floodfill(80+i*20+1,-20+j*20+1,color);
grid[j]=grid[j-1];
}
}
for (i=1;i<=10;i++)
{
setfillstyle(SOLID_FILL,backcolor);
floodfill(80+i*20+1,61,color);
grid[4]=0;
}
k++;
}
}
if (scoreadd!=0) showscore(2*scoreadd-1);
}
/*****************************************************************************************/
void interrupt newtimer()/*新的定时中断调用*/
{
int leveltemp;
leveltemp=11-level;
count++;
if(count>=leveltemp)
{
addtobuffer(DOWN);
count=0;
}
(*oldtimer)();
}
/*****************************************************************************************/
void install (void interrupt (*paddr)())
{
disable();
setvect(0x1c,paddr);
enable();
}
/******************************************************************************************/
void delinfo (struct diamond *info)/*释放开辟的空间*/
{
struct diamond *now;
struct diamond *next;
now=info->next;
next=info->next;
while(next!=info)
{
next=now->next;
free(now);
now=next;
}
free(info);
}
/******************************************************************************************/
void addtobuffer(int c)/*向键盘缓冲区中增加一个DOWN*/
{
unsigned i;
i=*(unsigned far *)0x0040001CL;
*(unsigned far *)(0x00400000L | i)=c;
i+=2;
if (i>=*(unsigned far *)0x00400082L) i=*(unsigned far *)0x00400080L;
*(unsigned far *)0x0040001CL=i;
}
/******************************************************************************************/
/*void clrkey()调用dos中断清空键盘缓冲区,未使用此方法.
{
union REGS in ;
in.h.ah = 0x0c ;
in.h.al = 0x00 ;
intdos(&in , &in) ;
}
*/
/******************************************************************************************/
void showsubwin(struct diamond *next)
{
int i;/*计数器*/
int j;
setfillstyle (SOLID_FILL,backcolor);
for (i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
floodfill(361+20*i,61+20*j,color);
}
}
setfillstyle (SOLID_FILL,next->color);
for (i=0;i<=3;i++)
{
floodfill(381+next->x*20,81+next->y*20,color);
}
}
/******************************************************************************************/
void showscore(int scoreget)
{
char sscore;
char slevel[2];
score+=scoreget;
if (score<1000) level=score/100+1;
sprintf(sscore,"%d",score);
sprintf(slevel,"%d",level);
setfillstyle(SOLID_FILL,9);
bar(406,179,490,203);
setcolor(14);
outtextxy (408,180,slevel);
outtextxy (408,195,sscore);
}
/*******************************************************************************************/
void startset()
{
int i;
int j;
setfillstyle(SOLID_FILL,backcolor);
for(i=0;i<=21;i++)/*重新初始化*/
{
for(j=1;j<=10;j++)
if (grid[j]!=0)
{
grid[j]=0;
if (i>=4) floodfill(80+j*20+1,-20+i*20+1,color);
}
}
score=0;
showscore(0);
nowinfo=get_diamond ();/*得到一个当前方块*/
x=nowinfo->start_x;/*初始化方块位置*/
y=nowinfo->start_y;
nextinfo=get_diamond ();/*得到下一个方块*/
showsubwin(nextinfo);
install(newtimer);
}
--------------------------------------------------------------------------------
----------------南京程序员俱乐部-------------------
网址:Http://WWW.NjPro.CN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -