#include <dos.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include "funkey.h"
typedef struct Mode_tag
{
struct pos_tag
{
int h,l;
}pos[4];
}BLOCKMODE;
typedef struct shape_tag
{
struct corner_tag
{
int x,y;
}corner[4];
}SHAPE;
BLOCKMODE modeset[7][4]={
{{1,1,2,1,3,1,4,1},{1,1,1,2,1,3,1,4},{1,1,2,1,3,1,4,1},{1,1,1,2,1,3,1,4}},
{{1,1,1,2,2,1,3,1},{1,1,1,2,1,3,2,3},{1,2,2,2,3,1,3,2},{1,1,2,1,2,2,2,3}},
{{1,1,1,2,2,2,3,2},{1,3,2,1,2,2,2,3},{1,1,2,1,3,1,3,2},{1,1,1,2,1,3,2,1}},
{{1,1,1,2,2,1,2,2},{1,1,1,2,2,1,2,2},{1,1,1,2,2,1,2,2},{1,1,1,2,2,1,2,2}},
{{1,1,2,1,2,2,3,1},{1,1,1,2,1,3,2,2},{1,2,2,1,2,2,3,2},{1,2,2,1,2,2,2,3}},
{{1,1,2,1,2,2,3,2},{1,2,1,3,2,1,2,2},{1,1,2,1,2,2,3,2},{1,2,1,3,2,1,2,2}},
{{1,2,2,1,2,2,3,1},{1,1,1,2,2,2,2,3},{1,2,2,1,2,2,3,1},{1,1,1,2,2,2,2,3}}
};
char screen[23][19];
void getblock(SHAPE *blockshape)
{
int i,j,k;
i=random(7);
for(j=0;j<4;j++)
{
for(k=0;k<4;k++)
{
blockshape[j].corner[k].x=140+(modeset[i][j].pos[k].l-1)*20;
blockshape[j].corner[k].y=20+(modeset[i][j].pos[k].h-1)*20;
}
}
}
void main()
{
int gdriver = DETECT, gmode, errorcode;
SHAPE blockshape[4];
int i,j,k,key,downto;
int dir=0,level=5,d;
int del[10]={500,200,100,80,70,60,50,40,30,20};
for(i=0;i<22;i++)
for(j=0;j<18;j++)
screen[i][j]=0;
for(i=0;i<19;i++)
screen[22][i]=1;
for(i=0;i<23;i++)
screen[i][18]=1;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "f:\\blc\\bgi");
randomize();
setcolor(WHITE);
line(139,20,139,20+22*20);
line(140+18*20,20,140+18*20,20+22*20);
line(139,20+22*20+1,140+18*20,20+22*20+1);
while(1)
{
getblock(blockshape);
dir=0;
for(i=0;i<4;i++)
if(screen[(blockshape[dir].corner[i].y-20)/20][(blockshape[dir].corner[i].x-140)/20]==1)
{
setcolor(RED);
settextstyle(0,0,5);
outtextxy(140,240,"GAME OVER");
delay(2000);
closegraph();
return;
}
else
{
setcolor(WHITE);
rectangle(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
while(1)
{
if(kbhit())
{
key=bioskey(0);
if((key&0x00ff)==27)return;
downto=0;
switch(key)
{
case UP:
//add your codes down here
//add your codes up here
break;
case DOWN:
setcolor(BLACK);
for(i=0;i<4;i++)
rectangle(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
if(screen[(blockshape[dir].corner[j].y+(i+1)*20-20)/20][(blockshape[dir].corner[j].x-140)/20]==1)
break;
if(j<4)
break;
}
if(i<5)
{ //tuch the target
for(j=0;j<4;j++)
blockshape[dir].corner[j].y=(blockshape[dir].corner[j].y+i*20-20)/20*20+20;
for(i=0;i<4;i++)
{
screen[(blockshape[dir].corner[i].y-20)/20][(blockshape[dir].corner[i].x-140)/20]=1;
setcolor(WHITE);
setfillstyle(SOLID_FILL,(blockshape[dir].corner[i].y-20)/20%15+1);
bar(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
rectangle(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
downto=1;
}
else
{ //falling normally
for(i=0;i<4;i++)
for(j=0;j<4;j++)
blockshape[i].corner[j].y+=4*20;
setcolor(WHITE);
for(i=0;i<4;i++)
rectangle(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
break;
case LEFT:
// add your codes down here
//add your codes down here
break;
case RIGHT:
for(i=0;i<4;i++)
if(blockshape[dir].corner[i].x+20>=140+18*20||screen[(blockshape[dir].corner[i].y-20+19)/20][(blockshape[dir].corner[i].x-140)/20+1]==1
||screen[(blockshape[dir].corner[i].y-20)/20][(blockshape[dir].corner[i].x-140)/20+1]==1)
break;
if(i==4)
{
for(i=0;i<4;i++)
{
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
bar(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
blockshape[i].corner[j].x+=20;
for(i=0;i<4;i++)
{
setcolor(WHITE);
rectangle(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
}
break;
}//switch
if(downto==1)
break;
}
else
{
if(blockshape[dir].corner[0].y%20==0)
{
for(i=0;i<4;i++)
if(screen[(blockshape[dir].corner[i].y-20)/20+1][(blockshape[dir].corner[i].x-140)/20]==1)
break;
if(i<4)
{
for(i=0;i<4;i++)
{
screen[(blockshape[dir].corner[i].y-20)/20][(blockshape[dir].corner[i].x-140)/20]=1;
setcolor(WHITE);
setfillstyle(SOLID_FILL,(blockshape[dir].corner[i].y-20)/20%15+1);
bar(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
rectangle(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
break;
}
}
delay(del[level]);
for(i=0;i<4;i++)
{
setfillstyle(SOLID_FILL,BLACK);
bar(blockshape[dir].corner[i].x,blockshape[dir].corner[i].y,blockshape[dir].corner[i].x+20-1,blockshape[dir].corner[i].y+20-1);
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
blockshape[i].corner[j].y+=5;
if(dir==i)
{
setcolor(WHITE);
rectangle(blockshape[i].corner[j].x,blockshape[i].corner[j].y,blockshape[i].corner[j].x+20-1,blockshape[i].corner[j].y+20-1);
}
}//for j
}//else no key
} //while for falling one block
for(;;)
{
for(i=0;i<22;i++)
{
for(j=0;j<18;j++)
if(screen[i][j]==0)break;
if(j==18)
break;
}
if(i>=22)break;
else
{
for(j=i;j>0;j--)
for(k=0;k<18;k++)
screen[j][k]=screen[j-1][k];
for(i=0;i<18;i++)
screen[0][i]=0;
setcolor(WHITE);
for(i=0;i<22;i++)
for(j=0;j<18;j++)
{
if(screen[i][j]==1)
{
setcolor(WHITE);
setfillstyle(SOLID_FILL,i%15+1);
bar(140+j*20,20+i*20,140+j*20+20-1,20+i*20+20-1);
rectangle(140+j*20,20+i*20,140+j*20+20-1,20+i*20+20-1);
}//if
else
{
setfillstyle(SOLID_FILL,BLACK);
bar(140+j*20,20+i*20,140+j*20+20-1,20+i*20+20-1);
}
}
} //else
}//for(;;)
for(i=0;i<18;i++)
if(screen[0][i]==1)
{
setcolor(RED);
settextstyle(0,0,5);
outtextxy(140,240,"GAME OVER");
delay(2000);
closegraph();
return;
}
} //while
}