我设想的俄罗斯方块如下图:我们先弹出一个窗口。首先,新建一个windows应用工程,如下图:然后删除工程里预写好的代码,添加如下代码:#includewindows.h#includestdio.hHWNDhwnd;//窗口句柄LRESULTCALLBACKWindowProcedure(HWND,UINT,WPARAM,LPARAM);//声明回调函数charmyClassName[]=myClass;intWINAPIWinMain(HINSTANCEhThisInstance,HINSTANCEhPrevInstance,LPSTRlpszArgument,intnFunsterStil){MSGmessages;WNDCLASSEXwincl;wincl.hInstance=hThisInstance;wincl.lpszClassName=myClassName;//窗口类名wincl.lpfnWndProc=WindowProcedure;//回调函数wincl.style=CS_DBLCLKS;wincl.cbSize=sizeof(WNDCLASSEX);wincl.hIcon=LoadIcon(NULL,IDI_APPLICATION);wincl.hIconSm=LoadIcon(NULL,IDI_APPLICATION);wincl.hCursor=LoadCursor(NULL,IDC_ARROW);wincl.lpszMenuName=NULL;wincl.cbClsExtra=0;wincl.cbWndExtra=0;wincl.hbrBackground=(HBRUSH)COLOR_BACKGROUND;if(!RegisterClassEx(&wincl))//注册窗口类return0;hwnd=CreateWindowEx(0,myClassName,俄罗斯方块,WS_OVERLAPPEDWINDOW|WS_VISIBLE,CW_USEDEFAULT,//默认横坐标CW_USEDEFAULT,//默认纵坐标430,615,//长宽HWND_DESKTOP,NULL,hThisInstance,NULL);while(GetMessage(&messages,NULL,0,0))//进入消息循环{TranslateMessage(&messages);DispatchMessage(&messages);}returnmessages.wParam;}LRESULTCALLBACKWindowProcedure(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam){switch(message){caseWM_DESTROY:PostQuitMessage(0);break;default:returnDefWindowProc(hwnd,message,wParam,lParam);}return0;}然后再点击“编译并运行”按钮,弹出窗口:这样,我们的第一步,就算完成了,呵。以后的我们的所有代码都是在上边代码的基础上添加而成的。如图:上图是一种方块变形的四种情况,为表示以上所有的四的情况,我们需要建立一个三维数组:m_olshapePiece[4][5][5]={0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0};上边建立的三维数组应该不难理解吧,m_olshapePiece[0][x][y]就表示第一种形状,m_olshapePiece[1][x][y]表示第二种形状,m_olshapePiece[2[x][y]表示第三种形状,m_olshapePiece[3][x][y]表示第四种形状。我们按照同样的方法,把其它几种形状的矩阵弄好,并且把它们设为全局变量,如下:boolm_longPiece[4][5][5]={0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0};boolm_lshapePiece[4][5][5]={0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,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,0,0};boolm_olshapePiece[4][5][5]={0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0};boolm_zshapePiece[4][5][5]={0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0};boolm_ozshapePiece[4][5][5]={0,1,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,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,1,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,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};boolm_squarePiece[4][5][5]={0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,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,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,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,0,0};boolm_pshapePiece[4][5][5]={0,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0};接下来,让我们来看仔细看看我们游戏的布局:如上图所示,我们的游戏由五大块组成:一、按钮:这个就是完成一些特定功能的,比如开始、停止、暂停等等,第一个按钮在窗口上的坐标是(12,16)。二、BigMap:它是我们游戏的主要区域,它由二维数组BigMap[12][21]来储存该格子是已有方块,BigMap由DrawBigMap()函数完成在窗口上的绘制。它在窗口上的坐标是(12,66)。三、CurPiece:它是当前正在掉落的方块,它由三维数组CurPiece[4][5][5]来储存,CupPiece是一个5*5的二维平面,但因为它有4种变形,所以我们需要准备4个二维平面。CurPiece由DrawCurPiece()函数完成在窗口上的绘制。它在窗口上的坐标由变量xPos、yPos储存。四、NextPiece:它表示下一个要掉落的方块,它由三维数组NextPiece[4][5][5]来储存,由DrawNextPiece()函数完成在窗口上的绘制。它在窗口上的坐标是(277,66)。五、Score:它表示当前得分和游戏等级,由变量score和level储存(为了方便,我们还需要同时字符串scoreString[20]和levelString[20]储存),由DrawScore()函数完成在窗口上的绘制。它在窗口上的坐标分别是(300,220)和(300,270)。或许你对上边的叙述还不太清楚,没关系,随着我们的继续,你会很快明白的。由以上部分可知,我们需要定义的全局变量有:boolBigMap[12][21];//BigMapboolCurPiece[4][5][5];//CurPieceboolNextPiece[4][5][5];//NextPieceintxPos,yPos;//表示当前下落方块在BigMap中的位置坐标intscore;//储存分数charscoreString[20];//储存分数的字符串intlevel;//储存过关数charlevelString[20];//储存过关数的字符串我们需要定义的函数有:voidDrawBigMap();//绘制BigMapvoidDrawCurMap();//绘制CurPiecevoidDrawNextMap();//绘制NextPiecevoidDrawScore();//绘制Score另外,我们还需要定义的其它全局变量有:HDChdc;//储存设备句柄HPENhPen;//储存画笔句柄HBRUSHhBrush;//储存笔刷句柄intpm=25;//储存一个方块的大小intdirection;//表示方块当前的方向前三个变量句于绘制。我们说过,正掉落的方块有四种变形,这里我们称之的方块的四种方向,我们用direction储存。pm表示游戏中最小格子的大小,也就是25像素,如下图:我们还需要定义以下几个函数:voidinit();//初始化变量voidBegin();//按下开始开始按钮时,执行此函数接下来,我们就开定义以上每个函数,首先是DrawBigMap()函数。BigMap是一个12*21的矩阵,其中有些是用来表示边界的,并不被绘制出来,如下图:其中的灰色部分是边界。其它是0的地方表示该格子没有方块,是1的地方表示该格子有方块,于是DrawBigMap()函数的定义如下:voidDrawPiece(intx,inty){//在(x,y)绘制一个大小为pm(25像素)的方块SelectObject(hdc,GetStockObject(WHITE_PEN));//选择白色画笔SelectObject(hdc,GetStockObject(BLACK_BRUSH));//选择黑色笔刷Rectangle(hdc,x,y,x+pm,y+pm);}voidDrawBigMap(){//绘制BigMapSelectObject(