俄罗斯方块.docx
《俄罗斯方块.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块.docx(13页珍藏版)》请在冰豆网上搜索。
俄罗斯方块
#ifndefblock_h
#defineblock_h
intshape[7][4][4]=
{
{
{0,1,5,9},
{2,4,5,6},
{0,4,8,9},
{0,1,2,4}
},
{
{0,1,4,8},
{0,1,2,6},
{1,5,8,9},
{0,4,5,6}
},
{
{0,1,5,6},
{1,4,5,8},
{0,1,5,6},
{1,4,5,8}
},
{
{1,2,4,5},
{0,4,5,9},
{1,2,4,5},
{0,4,5,9}
},
{
{1,4,5,6},
{0,4,5,8},
{0,1,2,5},
{1,4,5,9}
},
{
{0,1,4,5},
{0,1,4,5},
{0,1,4,5},
{0,1,4,5}
},
{
{0,1,2,3},
{0,4,8,12},
{0,1,2,3},
{0,4,8,12}
}
};
#endif
函数声明:
#ifndeffunction_h
#definefunction_h
LRESULTCALLBACKWndProc(HWNDhwnd,UINTmesage,WPARAMwParam,LPARAMlParam);
voidDraw_shape(HWNDhwnd,intshape_num,intshape_direction,POINTposition,COLORREFcolor);
voidDraw_block(HWNDhwnd,POINTblock,COLORREFcolor);
voidInit_map();
voidNew_shape(HWNDhwnd);
voidDown_shape(HWNDhwnd);
voidRight_shape(HWNDhwnd);
voidLeft_shape(HWNDhwnd);
voidChange_direction(HWNDhwnd);
boolDownable();
boolRightable();
boolLeftable();
boolChangeable();
voidUpdate_map();
intFull_row();
voidClean_full_row(HWNDhwnd,introw);
voidPause();
voidDraw_edge(HWNDhwnd);
voidPre_to_cur(HWNDhwnd);
boolOver();
voidShow_over(HWNDhwnd);
voidInit_frame(HWNDhwnd);
voidShow_score(HWNDhwnd);
voidShow_level(HWNDhwnd);
voidChange_level(HWNDhwnd);
voidShow_preview(HWNDhwnd);
#endif
主程序:
#include
#include
#include"block.h"
#include"function.h"
intblock_size=15;//小方块边长
POINTcur_pos={8,1};//图形当前坐标
intcur_shape_num=0;//图形当前号码
intcur_shape_direction=0;//图形当前方向方向表示同种图形内部的变形变化
intpre_shape_num=0;
intpre_shape_direction=0;
#defineX_max20//画图区域宽度
#defineY_max30//画图区域高度
intmap[Y_max][X_max];//画图区域坐标系
COLORREFshape_color=RGB(0,200,200);//图形颜色
COLORREFbg_color=RGB(150,20,20);//背景颜色
COLORREFedge_color=RGB(0,100,100);
intscore=0;
intlevel=1;
intWINAPIWinMain(HINSTANCEhInstance,
HINSTANCEhPreInstance,
PSTRszCmdLine,
intiCmdShow)
{
constintcaption_height=GetSystemMetrics(SM_CYCAPTION);//标题栏高度
constintframe_height=GetSystemMetrics(SM_CYFRAME);//窗口边框高度
constintframe_width=GetSystemMetrics(SM_CXFRAME);//窗口侧边栏宽度
staticTCHARclassname[]=TEXT("classname");
MSGmsg;
HWNDhwnd;
WNDCLASSwc;
wc.style =CS_HREDRAW|CS_VREDRAW;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInstance;
wc.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground =CreateSolidBrush(bg_color);;
wc.lpszMenuName =NULL;
wc.lpszClassName =classname;
if(!
RegisterClass(&wc))
{
MessageBox(NULL,TEXT("需要windowsNT"),TEXT("出错啦!
"),MB_OK|MB_ICONERROR);
return0;
}
hwnd=CreateWindow(
classname,
TEXT("俄罗斯方块_林语"),
WS_SYSMENU,
500,
200,
(frame_width*2)+((X_max+5)*block_size),//+5用于显示预览,得分,等级
(frame_height*2)+(caption_height)+(Y_max*block_size),
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,iCmdShow);
// UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
returnmsg.wParam;
}
LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)
{
switch(message)
{
caseWM_CREATE:
Init_map();
break;
caseWM_PAINT:
Init_frame(hwnd);
New_shape(hwnd);
Pre_to_cur(hwnd);
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);
SetTimer(hwnd,1,1000/level,NULL);
break;
caseWM_TIMER:
Down_shape(hwnd);
break;
caseWM_KEYDOWN:
switch(wParam)
{
caseVK_UP:
Change_direction(hwnd);
break;
caseVK_DOWN:
Down_shape(hwnd);
break;
caseVK_LEFT:
Left_shape(hwnd);
break;
caseVK_RIGHT:
Right_shape(hwnd);
break;
caseVK_SPACE:
Pause();
break;
}
break;
caseWM_CLOSE:
KillTimer(hwnd,1);
PostQuitMessage(0);
break;
}
returnDefWindowProc(hwnd,message,wParam,lParam);;
}
//*******************************************************************************************************************
voidDraw_shape(HWNDhwnd,intshape_num,intshape_direction,POINTposition,COLORREFcolor)//图形号、方向以及左上角坐标、颜色
{//画图形
POINTblock;
for(inti=0;i<4;++i)
{
block.x=position.x+shape[shape_num][shape_direction][i]%4;
block.y=position.y+shape[shape_num][shape_direction][i]/4;
Draw_block(hwnd,block,color);
}
}
voidDraw_block(HWNDhwnd,POINTblock,COLORREFcolor)//小方块左上角坐标及颜色
{//画小方块
RECTrc;
SetRect(&rc,
block.x*block_size+1, //作+1、-1的调整是为了使得方块之间有间隙
block.y*block_size+1,
(block.x+1)*block_size-1,
(block.y+1)*block_size-1);
HDChdc=GetDC(hwnd);
HBRUSHbrush=(HBRUSH)CreateSolidBrush(color);
FillRect(hdc,&rc,brush);
DeleteObject(brush);
ReleaseDC(hwnd,hdc);
}
voidInit_map()
{//构造一个开口朝上的半包围结构作为边界
for(inti=0;i {
for(intj=0;j {
map[i][j]=0;
}
}
//将边界初始化为1表明此处不可被覆盖
for(i=0;i {
map[i][0]=1;
map[i][X_max-1]=1;
}
for(i=0;i {
map[Y_max-1][i]=1;
}
}
voidInit_frame(HWNDhwnd)
{//初始化界面,画出方框和信息
Draw_edge(hwnd);
HDChdc=GetDC(hwnd);
HPENhpen=CreatePen(PS_SOLID,2,shape_color);
SelectObject(hdc,hpen);
TCHARprev[]="预览:
";//preview
TextOut(hdc,X_max*block_size,block_size,prev,strlen(prev));
TCHARsco[]="得分:
";//score
TextOut(hdc,X_max*block_size,10*block_size,sco,strlen(sco));
Show_score(hwnd);
TCHARlev[]="等级:
";//level
TextOut(hdc,X_max*block_size,20*block_size,lev,strlen(lev));
Show_level(hwnd);
DeleteObject(hpen);
ReleaseDC(hwnd,hdc);
}
voidNew_shape(HWNDhwnd)
{//随机生成预览图形并显示
srand((unsigned)time(NULL));
pre_shape_num=rand()%7;
pre_shape_direction=0;
Show_preview(hwnd);
}
voidPre_to_cur(HWNDhwnd)
{//将预览图形变为将要出现的图形,并生成新的预览图形
cur_shape_num=pre_shape_num;
cur_shape_direction=pre_shape_direction;
cur_pos.x=8;
cur_pos.y=1;
New_shape(hwnd);
}
voidDown_shape(HWNDhwnd)
{//处理图形下移
if(!
Downable())
{//如果不可以下移、
Update_map();//在坐标系中记录
if(Over())//判断是否结束
{
KillTimer(hwnd,1);
Show_over(hwnd);
return;
}
introw;
while(row=Full_row())//是否有满行
{
Clean_full_row(hwnd,row);
}
Pre_to_cur(hwnd);//产生新的图形
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);
return;
}
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,bg_color);//消除原来的痕迹
cur_pos.y+=1;//图形的纵坐标+1
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);//画出下降后的图形
}
voidRight_shape(HWNDhwnd)
{//处理右移
if(!
Rightable())
{
return;
}
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,bg_color);
cur_pos.x+=1;
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);
}
voidLeft_shape(HWNDhwnd)
{//处理左移
if(!
Leftable())
{
return;
}
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,bg_color);
cur_pos.x-=1;
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);
}
voidChange_direction(HWNDhwnd)
{//变形处理
if(!
Changeable())
{
return;
}
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,bg_color);
cur_shape_direction+=1;
cur_shape_direction%=4;
Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);
}
boolDownable()
{//判断是否可以向下
POINTdown_pos,block_pos;
down_pos.x=cur_pos.x;
down_pos.y=cur_pos.y+1;
for(inti=0;i<4;i++)
{//分别检查图形中的4个小块是否会与下方重叠
block_pos.x=down_pos.x+shape[cur_shape_num][cur_shape_direction][i]%4;
block_pos.y=down_pos.y+shape[cur_shape_num][cur_shape_direction][i]/4;
if(map[block_pos.y][block_pos.x]==1)
{
returnfalse;
}
}
returntrue;
}
boolRightable()
{//判断是否可以向右
POINTdown_pos,block_pos;
down_pos.x=cur_pos.x+1;
down_pos.y=cur_pos.y;
for(inti=0;i<4;i++)
{//分别检查图形中的4个小块是否会与右方重叠
block_pos.x=down_pos.x+shape[cur_shape_num][cur_shape_direction][i]%4;
block_pos.y=down_pos.y+shape[cur_shape_num][cur_shape_direction][i]/4;
if(map[block_pos.y][block_pos.x]==1)
{
returnfalse;
}
}
returntrue;
}
boolLeftable()
{//判断是否可以向左
POINTdown_pos,block_pos;
down_pos.x=cur_pos.x-1;
down_pos.y=cur_pos.y;
for(inti=0;i<4;i++)
{//分别检查图形中的4个小块是否会与左方重叠
block_pos.x=down_pos.x+shape[cur_shape_num][cur_s