俄罗斯方块代码.docx
《俄罗斯方块代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块代码.docx(22页珍藏版)》请在冰点文库上搜索。
![俄罗斯方块代码.docx](https://file1.bingdoc.com/fileroot1/2023-6/22/cf10ea34-f2d3-4ef8-a911-4a0cea61d1d0/cf10ea34-f2d3-4ef8-a911-4a0cea61d1d01.gif)
俄罗斯方块代码
#include"stdafx.h"
#include
#include"resource.h"
#defineMAX_LOADSTRING100
#defineUNIT_SIZE15//模块单元大小
#defineSQUARE_AMOUNT17//窗体中模块单元的数目
#defineWM_NEWSQUAREWM_USER+1//自定义消息
#defineMYTIMER1//自定义时钟标示
#defineDIFLEVEL500//难易度,即时钟间隔
//GlobalVariables:
HINSTANCEhInst;//currentinstance
TCHARszTitle[MAX_LOADSTRING];//Thetitlebartext
TCHARszWindowClass[MAX_LOADSTRING];//Thetitlebartext
//Fowarddeclarationsoffunctionsincludedinthiscodemodule:
ATOMMyRegisterClass(HINSTANCEhInstance);
BOOLInitInstance(HINSTANCE,int);
LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);
LRESULTCALLBACKAbout(HWND,UINT,WPARAM,LPARAM);
typedefstruct_Square
{
POINTps[4];
HWNDhUnitWnd[4];
inttype;
}Square,*pSquare;
intAPIENTRYWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPSTRlpCmdLine,
intnCmdShow)
{
//TODO:
Placecodehere.
MSGmsg;
HACCELhAccelTable;
//Initializeglobalstrings
LoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);
LoadString(hInstance,IDC_I_GOV1,szWindowClass,MAX_LOADSTRING);
MyRegisterClass(hInstance);
//Performapplicationinitialization:
if(!
InitInstance(hInstance,nCmdShow))
{
returnFALSE;
}
hAccelTable=LoadAccelerators(hInstance,(LPCTSTR)IDC_I_GOV1);
//Mainmessageloop:
while(GetMessage(&msg,NULL,0,0))
{
if(!
TranslateAccelerator(msg.hwnd,hAccelTable,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
returnmsg.wParam;
}
ATOMMyRegisterClass(HINSTANCEhInstance)
{
WNDCLASSEXwcex;
wcex.cbSize=sizeof(WNDCLASSEX);
wcex.style=CS_HREDRAW|CS_VREDRAW;
wcex.lpfnWndProc=(WNDPROC)WndProc;
wcex.cbClsExtra=0;
wcex.cbWndExtra=0;
wcex.hInstance=hInstance;
wcex.hIcon=LoadIcon(hInstance,(LPCTSTR)IDI_I_GOV1);
wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName=(LPCSTR)IDC_I_GOV1;
wcex.lpszClassName=szWindowClass;
wcex.hIconSm=LoadIcon(wcex.hInstance,(LPCTSTR)IDI_SMALL);
returnRegisterClassEx(&wcex);
}
BOOLInitInstance(HINSTANCEhInstance,intnCmdShow)
{
HWNDhWnd;
hInst=hInstance;//Storeinstancehandleinourglobalvariable
hWnd=CreateWindow(szWindowClass,szTitle,WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!
hWnd)
{
returnFALSE;
}
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
returnTRUE;
}
LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
intwmId,wmEvent;
PAINTSTRUCTps;
HDChdc;
TCHARszHello[MAX_LOADSTRING];
LoadString(hInst,IDS_HELLO,szHello,MAX_LOADSTRING);
staticRECTrc;
staticBOOLIsFilled[SQUARE_AMOUNT][SQUARE_AMOUNT];
staticHWNDhWndSquare[SQUARE_AMOUNT][SQUARE_AMOUNT];
staticSquaresq;
inti,j;
intxFirstUnitPos=SQUARE_AMOUNT/2;
staticHMENUhMenu;
switch(message)
{
caseWM_CREATE:
rc.left=rc.top=0;
rc.right=rc.bottom=UNIT_SIZE*SQUARE_AMOUNT;
AdjustWindowRect(&rc,WS_CAPTION|WS_SYSMENU,TRUE);
SetWindowPos(hWnd,NULL,0,0,rc.right-rc.left,
rc.bottom-rc.top,SWP_NOMOVE);
break;
caseWM_TIMER:
//是否不能再往下落了
for(i=0;i<4;i++)
{
if(IsFilled[sq.ps[i].x][sq.ps[i].y]==1)
{
KillTimer(hWnd,MYTIMER);
MessageBox(hWnd,"你已失败","注意",MB_OK);
PostMessage(hWnd,WM_DESTROY,0,0);
return0;
}
if(sq.ps[i].y==SQUARE_AMOUNT-1||IsFilled[sq.ps[i].x][sq.ps[i].y+1]==TRUE)
{
for(j=0;j<4;j++)//填充方块所占的区域
{
IsFilled[sq.ps[j].x][sq.ps[j].y]=TRUE;
hWndSquare[sq.ps[j].x][sq.ps[j].y]=sq.hUnitWnd[j];
}
KillTimer(hWnd,MYTIMER);
//消去方块
//浏览,产生记录条
{//procedure
unsignedintflag=0;
for(i=SQUARE_AMOUNT-1;i>=0;i--)//yshaft
{
for(j=0;jif(!
IsFilled[j][i])
break;
if(j==SQUARE_AMOUNT)
flag|=1l<
}
if(flag)//有行要消掉
{
for(i=SQUARE_AMOUNT-1;i>=0;i--)
if(flag&1l<
for(j=0;j{
DestroyWindow(hWndSquare[j][i]);
IsFilled[j][i]=FALSE;
}
for(i=SQUARE_AMOUNT-1;i>=0;i--)
if(!
(flag&1l<
{
inttemp,offset;
temp=i+1;
offset=0;
while(temp{
if(flag&1l<offset++;
temp++;
}
for(j=0;j{
RECTrcUnit;
if(IsFilled[j][i])
{GetWindowRect(hWndSquare[j][i],&rcUnit);
POINTpt;
pt.x=rcUnit.left;
pt.y=rcUnit.top;
ScreenToClient(hWnd,&pt);
MoveWindow(hWndSquare[j][i],
pt.x,pt.y+offset*UNIT_SIZE,
UNIT_SIZE,UNIT_SIZE,TRUE);
IsFilled[j][i]=FALSE;
IsFilled[j][i+offset]=TRUE;
hWndSquare[j][i+offset]=hWndSquare[j][i];
}
}
}
}
}//procedure
SendMessage(hWnd,WM_NEWSQUARE,NULL,NULL);
return0;
}
}
//移动方块
for(i=0;i<4;i++)
{
//_beginthread();
MoveWindow(sq.hUnitWnd[i],sq.ps[i].x*UNIT_SIZE,
(++sq.ps[i].y)*UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,TRUE);
}
break;
caseWM_NEWSQUARE:
//生成方块
srand((unsigned)time(NULL));
switch(rand()%6)
{
/*@@@@*/
case0:
sq.type=0;
xFirstUnitPos=8;
for(i=0;i<4;i++)
{
sq.ps[i].x=xFirstUnitPos+i;
sq.ps[i].y=0;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
break;
/*@
@@@*/
case1:
sq.type=1;
for(i=0;i<4;i++)
{
if(0==i)
{
sq.ps[i].x=xFirstUnitPos;
sq.ps[i].y=0;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,sq.ps[i].y,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
else
{
sq.ps[i].x=xFirstUnitPos+i-1;
sq.ps[i].y=1;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,sq.ps[i].y,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
}
break;
/*@@
@@*/
case2:
sq.type=2;
for(i=0;i<4;i++)
{
if(i==0||i==1)
{
sq.ps[i].x=xFirstUnitPos+i;
sq.ps[i].y=0;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
else
{
sq.ps[i].x=xFirstUnitPos+i-2;
sq.ps[i].y=1;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
}
break;
/*@
@@@*/
case3:
sq.type=3;
for(i=0;i<4;i++)
{
if(i==0)
{
sq.ps[i].x=xFirstUnitPos+1;
sq.ps[i].y=0;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
else
{
sq.ps[i].x=xFirstUnitPos+i-1;
sq.ps[i].y=1;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
}
break;
/*@@
@@*/
case4:
sq.type=4;
for(i=0;i<4;i++)
{
if(i==0||i==1)
{
sq.ps[i].x=xFirstUnitPos+i;
sq.ps[i].y=0;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
else
{
sq.ps[i].x=xFirstUnitPos+i-1;
sq.ps[i].y=1;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
}
break;
/*@@
@@*/
case5:
sq.type=5;
for(i=0;i<4;i++)
{
if(i==0||i==1)
{
sq.ps[i].x=xFirstUnitPos+i+1;
sq.ps[i].y=0;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
else
{
sq.ps[i].x=xFirstUnitPos+i-2;
sq.ps[i].y=1;
sq.hUnitWnd[i]=CreateWindow("BUTTON",
NULL,WS_VISIBLE|WS_CHILD,sq.ps[i].x*UNIT_SIZE,0,
UNIT_SIZE,UNIT_SIZE,hWnd,NULL,hInst,NULL);
}
}
break;
}
SetTimer(hWnd,MYTIMER,DIFLEVEL,NULL);
break;
caseWM_KEYDOWN:
switch(wParam)
{
caseVK_LEFT:
for(i=0;i<4;i++)
{
if(sq.ps[i].x-1<0||IsFilled[sq.ps[i].x-1][sq.ps[i].y]==TRUE)
return0;
}
for(i=0;i<4;i++)
MoveWindow(sq.hUnitWnd[i],--sq.ps[i].x*UNIT_SIZE,
sq.ps[i].y*UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,TRUE);
break;
caseVK_RIGHT:
for(i=0;i<4;i++)
{
if(sq.ps[i].x+1>SQUARE_AMOUNT-1||
IsFilled[sq.ps[i].x+1][sq.ps[i].y]==TRUE)
return0;
}
for(i=0;i<4;i++)
MoveWindow(sq.hUnitWnd[i],++sq.ps[i].x*UNIT_SIZE,
sq.ps[i].y*UNIT_SIZE,UNIT_SIZE,UNIT_SIZE,TRUE);
break;
caseVK_DOWN:
SendMessage(hWnd,WM_TIMER,0,0);
break;
caseVK_SPACE:
//方块变形
intshaft;
switch(sq.type)//确定每个方块的中轴方向
{
/*@@@@*/
case0:
shaft=1;
break;
/*@
@@@*/
case1:
shaft=1;
break;
/*@@
@@*/
case2:
re