1.         方块的大小和游戏区的坐标
const int SquareSize=20;
const int GameSizeX=300;
const int GameSizeY=50;

const int GameRegionWidth=10;
const int GameRegionHeight=20;
struct GameMapStates
{
int states;
int shapes;
}GameMapStatesInfo[10][20];

2.         建立游戏区

int LeftX=GameSizeX;//游戏区左上角横坐标
int LeftY=GameSizeY;//游戏区左上角纵坐标
int RightX=LeftX+GameRegionWidth*SquareSize;//游戏区右下角横坐标
int RightY=LeftY+GameRegionHeight*SquareSize;//游戏区右下角纵坐标

void DrawGameRegion(HDC hdc)
{
HBRUSH brush;
int LeftX,LeftY,RightX,RightY;
LeftX=GameSizeX;
LeftY=GameSizeY;
RightX=LeftX+GameRegionWidth*SquareSize;
RightY=LeftY+GameRegionHeight*SquareSize;
brush=CreateSolidBrush(RGB(165,180,255));
SelectObject(hdc,brush);
Rectangle(hdc,LeftX,LeftY,RightX,RightY);
DeleteObject(brush);
}

3.         初始化俄罗斯方块的7种图形
const POINT Terics[7][4][4] =
{
{

0,0,1,0,0,1,-1,1,

0,0,0,1,1,1,1,2,

0,0,1,0,0,1,-1,1,

0,0,0,1,1,1,1,2

},
{

0,0,1,0,1,1,2,1,

0,0,0,1,-1,1,-1,2,

0,0,1,0,1,1,2,1,

0,0,0,1,-1,1,-1,2

},
{

0,0,0,1,0,2,1,2,

0,0,0,1,-1,1,-2,1,

0,0,1,0,1,1,1,2,

0,0,0,1,1,0,2,0

},
{

0,0,0,1,0,2,-1,2,

0,0,1,0,2,0,2,1,

0,0,1,0,0,1,0,2,

0,0,0,1,1,1,2,1

},
{

0,0,0,1,0,2,0,3,

0,0,1,0,2,0,3,0,

0,0,0,1,0,2,0,3,

0,0,1,0,2,0,3,0

},
{

0,0,1,0,0,1,1,1,

0,0,1,0,0,1,1,1,

0,0,1,0,0,1,1,1,

0,0,1,0,0,1,1,1

},
{

0,0,1,0,2,0,1,1,

0,0,0,1,0,2,1,1,

0,0,0,1,-1,1,1,1,

0,0,0,1,0,2,-1,1

}
};

4.         画小方块

void DrawRectangle(HDC hdc,int shape,int x,int y)
{
HBRUSH brush;
int leftx=GameSizeX+SquareSize*x;
int lefty=GameSizeY+SquareSize*y;
int rightx=leftx+SquareSize;
int righty=lefty+SquareSize;
brush=CreateSolidBrush(color[shape]);
SelectObject(hdc,brush);
Rectangle(hdc,leftx,lefty,rightx,righty);
DeleteObject(brush);
}

5.         画俄罗斯方块
void DrawTerics(HDC hdc,int shape,int dir,int x,int y)
{
int index;
RECT rect;
for(index=0;index<4;index++)
{
nx[index]=Terics[shape][dir][index].x+x;
ny[index]=Terics[shape][dir][index].y+y;
rect.left=nx[index];
rect.top=ny[index];
rect.right=nx[index]+SquareSize;
rect.bottom=ny[index]+SquareSize;
DrawRectangle(hdc,shape,nx[index],ny[index]);
}
}

6.         判断范围
BOOL IsOutOfRegion(int shape,int dir,int x,int y)
{
int index;
int nx,ny;
for(index=0;index<4;index++)
{
nx=Terics[shape][dir][index].x+x;
ny=Terics[shape][dir][index].y+y;
if(nx<0||nx>=GameRegionWidth||ny<0||ny>=GameRegionHeight||
GameMapStatesInfo[nx][ny].states!=0)
{
return TRUE;
}
}
return FALSE;

}

7.         保存游戏区的状态及消去一行
void SaveStateOfTerics(int shape, int dir, int x, int y)
{
int index;
int nx,ny;
int indexX,indexY;
BOOL flag;
int m,n;
for ( index = 0; index < 4; ++index)
{
nx=Terics[shape][dir][index].x+x;
ny=Terics[shape][dir][index].y+y;
GameMapStatesInfo[nx][ny].states = 1;
GameMapStatesInfo[nx][ny].shapes=shape;
}
for(indexX=0;indexX<GameRegionHeight;indexX++)
{
flag=TRUE;
for(indexY=0;indexY<GameRegionWidth;indexY++)
{
if(GameMapStatesInfo[indexY][indexX].states!=1)
{
flag=FALSE;
}
}
if(flag)
{
for(m=indexX;m>=1;--m)
{
for(n=0;n<GameRegionWidth;++n)
{
GameMapStatesInfo[n][m].states=GameMapStatesInfo[n][m-1].states;
GameMapStatesInfo[n][m].shapes=GameMapStatesInfo[n][m-1].shapes;
}
}
++count;
}
}
}

8.         显示图形

void EraseGameRegion(HDC hdc)
{
RECT rect;
HBRUSH brush;
rect.left=GameSizeX;
rect.top=GameSizeY;
rect.right=GameSizeX+GameRegionWidth*SquareSize;
rect.bottom=GameSizeY+GameRegionHeight*SquareSize;
brush=CreateSolidBrush(RGB(255,255,255));
FillRect(hdc,&rect,brush);
DeleteObject(brush);
}

{
int indexX,indexY;
for(indexX=0;indexX<GameRegionWidth;indexX++)
{
for(indexY=0;indexY<GameRegionHeight;indexY++)
{
if(GameMapStatesInfo[indexX][indexY].states==1)
{
DrawRectangle(hdc,GameMapStatesInfo[indexX][indexY].shapes,indexX,indexY);
}
}
}
}

void PrintInfo(HDC hdc,int *shape)
{
char szscore[200]="score:";
char strscore[20];
score=count*10;
itoa(score,strscore,10);
strcat(szscore,strscore);
SetBkColor(hdc,RGB(165,180,255));
TextOut(hdc,600,100,szscore,strlen(szscore));
DrawTerics(hdc,*shape,0,15,5);
}
9.         初始化问题
俄罗斯方块中的方块每次都是从固定的地方出来的，因此我定义了一个初始化函数，让方块每次从固定的地方出来，且每次落下的方块形状是随机的。

struct CurrentTerics
{
int shape,dir,x,y;
}CurrentTericsInfo;
void InitTericsInfo()
{
int *p=szshape;
srand((unsigned)time(NULL));
CurrentTericsInfo.shape=rand()%7;
CurrentTericsInfo.dir=0;
CurrentTericsInfo.x=4;
CurrentTericsInfo.y=0;
*p++=CurrentTericsInfo.shape;
}

10.         颜色问题

const COLORREF color[7] =
{
RGB(255,0,0),
RGB(240,100,5),
RGB(150,250,60),
RGB(27,229,50),
RGB(10,125,145),
RGB(20,12,184),
RGB(116,34,156)

};

11.         游戏结束
BOOL IsGameOver(int shape,int dir,int x,int y)
{
int index;
int nx,ny;
for(index=0;index<4;index++)
{
nx=Terics[shape][dir][index].x+x;
ny=Terics[shape][dir][index].y+y;
if(ny==0 && GameMapStatesInfo[nx][ny].states==1)
{
return TRUE;
}
}
return FALSE;

}