这里给出源代码:算法功能单元
#define Screen_Width 8
#define Screen_Heighth 16
#define Tetris_Width 4
#define Tetris_Heighth 4
#define TetrisTypeNum 7
//#define TETRIS_TEST
/*
0: --------------------------
.
.
.
n: --------------------------
*/
/*
--------->X
|
|
|
Y
*/
uchar TetrisLayout[Screen_Heighth*Screen_Width];
uchar TetrisImage[Screen_Heighth*Screen_Width];
uchar TempImage[Screen_Heighth*Screen_Width];
uchar leftcol[Tetris_Heighth];
uchar rightcol[Tetris_Heighth];
uchar buttomline[Tetris_Width];
bool CanL=false,CanR=false,CanD=false;
bool LbuttonDown=false,LbuttonUp=false;
bool RbuttonDown=false,RbuttonUp=false;
bool DbuttonDown=false,DbuttonUp=false;
typedef struct TetrisEntity
{
uchar type;
uchar revolve;
uchar posX;
uchar posY;
} TetrisEntity,*pTetrisEntity;
TetrisEntity CurrentTetris;
pTetrisEntity pCurrentTetris= null;
bool IsProducing = false;
enum TetrisType
{
I = 0,
O = 1,
J = 2,
L = 3,
Z = 4,
S = 5,
T = 6
};
uchar TetrisData_Backup_I[Tetris_Width*Tetris_Heighth*4] =
{
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,0,1,0,
0,0,1,0,
0,0,1,0,
0,0,1,0,
0,0,0,0,
0,0,0,0,
1,1,1,1,
0,0,0,0
};
uchar TetrisData_I[Tetris_Width*Tetris_Heighth*4];
uchar TetrisData_Backup_O[Tetris_Width*Tetris_Heighth*4] =
{
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
};
uchar TetrisData_O[Tetris_Width*Tetris_Heighth*4];
uchar TetrisData_Backup_J[Tetris_Width*Tetris_Heighth*4] =
{
0,0,0,0,
0,0,1,0,
0,0,1,0,
0,1,1,0,
0,0,0,0,
1,0,0,0,
1,1,1,0,
0,0,0,0,
0,1,1,0,
0,1,0,0,
0,1,0,0,
0,0,0,0,
0,0,0,0,
0,1,1,1,
0,0,0,1,
0,0,0,0
};
uchar TetrisData_J[Tetris_Width*Tetris_Heighth*4];
uchar TetrisData_Backup_L[Tetris_Width*Tetris_Heighth*4] =
{
0,0,0,0,
0,1,0,0,
0,1,0,0,
0,1,1,0,
0,0,0,0,
1,1,1,0,
1,0,0,0,
0,0,0,0,
0,1,1,0,
0,0,1,0,
0,0,1,0,
0,0,0,0,
0,0,0,0,
0,0,0,1,
0,1,1,1,
0,0,0,0
};
uchar TetrisData_L[Tetris_Width*Tetris_Heighth*4];
uchar TetrisData_Backup_Z[Tetris_Width*Tetris_Heighth*4] =
{
0,0,0,0,
1,1,0,0,
0,1,1,0,
0,0,0,0,
0,0,1,0,
0,1,1,0,
0,1,0,0,
0,0,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
};
uchar TetrisData_Z[Tetris_Width*Tetris_Heighth*4];
uchar TetrisData_Backup_S[Tetris_Width*Tetris_Heighth*4] =
{
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,
1,1,0,0,
0,0,0,0,
0,1,0,0,
0,1,1,0,
0,0,1,0,
0,0,0,0
};
uchar TetrisData_S[Tetris_Width*Tetris_Heighth*4];
uchar TetrisData_Backup_T[Tetris_Width*Tetris_Heighth*4] =
{
1,1,1,0,
0,1,0,0,
0,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,0,0,
0,0,0,0,
0,0,1,0,
0,1,1,1,
0,0,0,0,
1,0,0,0,
1,1,0,0,
1,0,0,0
};
uchar TetrisData_T[Tetris_Width*Tetris_Heighth*4];
uchar* pTetrisData_Backup[TetrisTypeNum] = {
TetrisData_Backup_I,TetrisData_Backup_O,
TetrisData_Backup_J,TetrisData_Backup_L,
TetrisData_Backup_Z,TetrisData_Backup_S,
TetrisData_Backup_T
};
uchar* pTetrisData[TetrisTypeNum] = {
TetrisData_I,TetrisData_O,
TetrisData_J,TetrisData_L,
TetrisData_Z,TetrisData_S,
TetrisData_T
};
void RestoreData(uchar* src,uchar* dst)
{
for(int i=0;i<Tetris_Width*Tetris_Heighth*4;i++)
{
dst[i]=src[i];
}
}
void RestoreDataAll()
{
for(int j=0;j<TetrisTypeNum;j++)
{
RestoreData(pTetrisData_Backup[j],pTetrisData[j]);
}
}
void CopyLayout(uchar* src,uchar* dst)
{
for(int i=0;i<Screen_Heighth*Screen_Width;i++)
{
dst[i]=src[i];
}
}
void ClearLayout(uchar* image)
{
for(int i=0;i<Screen_Heighth*Screen_Width;i++)
{
image[i]=0;
}
}
/*
void PrintLayout(const uchar* image)
{
for(int i=0;i<Screen_Heighth*Screen_Width;i++)
{
printf("%c ",image[i]+48);
if(i%Screen_Width==(Screen_Width-1))
{
printf("\n\n");
}
}
}
*/
void FillLayout(uchar* image,const uchar* curTetrisData,uchar posX,uchar posY)
{
int startImageline = 0;
int startTetrisline = 0;
int fillnums = Tetris_Heighth;
if(posY>=(Tetris_Heighth-1))
{
startImageline=posY-(Tetris_Heighth-1);
}
else
{
startTetrisline=(Tetris_Heighth-1)-posY;
fillnums=Tetris_Heighth-startTetrisline;
}
for(int i=0;i<fillnums;i++)
{
for(int j=0;j<Tetris_Width;j++)
{
if(curTetrisData[(startTetrisline+i)*Tetris_Width+j])
{
image[(startImageline+i)*Screen_Width+posX+j]=curTetrisData[(startTetrisline+i)*Tetris_Width+j];
}
}
}
}
bool VerifyMoveL(const uchar* curTetrisData,uchar posX,uchar posY)
{
int i=0;int j=0;
while(i<Tetris_Width)
{
for(j=0;j<Tetris_Heighth;j++)
{
if(curTetrisData[j*Tetris_Width+i]!=0)
break;
}
if(j<Tetris_Heighth)
{
for(j=0;j<Tetris_Heighth;j++)
{
leftcol[j]=curTetrisData[j*Tetris_Width+i];
}
break;
}
i++;
}
if((posX+i)==0)
{
return false;
}
uchar tempdata[Tetris_Heighth*Tetris_Width];
if(posX==0)
{
for(int tempx=0;tempx<Tetris_Heighth;tempx++)
{
for(int tempy=0;tempy<Tetris_Width;tempy++)
{
if(tempy!=(Tetris_Width-1))
tempdata[tempx*Tetris_Width+tempy]=curTetrisData[tempx*Tetris_Width+tempy+1];
else
tempdata[tempx*Tetris_Width+tempy]=0;
}
}
ClearLayout(TempImage);
FillLayout(TempImage,tempdata,0,posY);
return !VerifyMix(TempImage,TetrisLayout);
}
else
{
ClearLayout(TempImage);
FillLayout(TempImage,curTetrisData,posX-1,posY);
return !VerifyMix(TempImage,TetrisLayout);
}
}
bool VerifyMoveR(const uchar* curTetrisData,uchar posX,uchar posY)
{
int i=Tetris_Width;int j=0;
while(i!=0)
{
i--;
for(j=0;j<Tetris_Heighth;j++)
{
if(curTetrisData[j*Tetris_Width+i]!=0)
break;
}
if(j<Tetris_Heighth)
{
for(j=0;j<Tetris_Heighth;j++)
{
rightcol[j]=curTetrisData[j*Tetris_Width+i];
}
break;
}
}
if((posX+i)==(Screen_Width-1))
{
return false;
}
//if(posX==(Screen_Width-1)
uchar tempdata[Tetris_Heighth*Tetris_Width];
if(posX==(Screen_Width-1-(Tetris_Width-1)))
{
for(int tempx=0;tempx<Tetris_Heighth;tempx++)
{
for(int tempy=0;tempy<Tetris_Width;tempy++)
{
if(tempy!=0)
tempdata[tempx*Tetris_Width+tempy]=curTetrisData[tempx*Tetris_Width+tempy-1];
else
tempdata[tempx*Tetris_Width+tempy]=0;
}
}
ClearLayout(TempImage);
FillLayout(TempImage,tempdata,posX,posY);
return !VerifyMix(TempImage,TetrisLayout);
}
else
{
ClearLayout(TempImage);
FillLayout(TempImage,curTetrisData,posX+1,posY);
return !VerifyMix(TempImage,TetrisLayout);
}
}
bool VerifyMoveD(const uchar* curTetrisData,uchar posX,uchar posY)
{
int i=0;int j=Tetris_Heighth;
while(j!=0)
{
j--;
for(i=0;i<Tetris_Width;i++)
{
if(curTetrisData[j*Tetris_Width+i]!=0)
break;
}
if(i<Tetris_Width)
{
for(i=0;i<Tetris_Width;i++)
{
buttomline[i]=curTetrisData[j*Tetris_Width+i];
}
break;
}
}
if((posY-(Tetris_Heighth-1-j))==(Screen_Heighth-1))
{
return false;
}
uchar tempdata[Tetris_Heighth*Tetris_Width];
if(posY==(Screen_Heighth-1))
{
for(int tempx=0;tempx<Tetris_Heighth;tempx++)
{
for(int tempy=0;tempy<Tetris_Width;tempy++)
{
if(tempx!=0)
tempdata[tempx*Tetris_Width+tempy]=curTetrisData[(tempx-1)*Tetris_Width+tempy];
else
tempdata[tempx*Tetris_Width+tempy]=0;
}
}
ClearLayout(TempImage);
FillLayout(TempImage,tempdata,posX,posY);
return !VerifyMix(TempImage,TetrisLayout);
}
else
{
ClearLayout(TempImage);
FillLayout(TempImage,curTetrisData,posX,posY+1);
return !VerifyMix(TempImage,TetrisLayout);
}
}