1、俄罗斯方块#ifndef block_h#define block_hint shape744=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函数声明: #ifndef functi
2、on_h#define function_hLRESULT CALLBACK WndProc(HWND hwnd,UINT mesage,WPARAM wParam, LPARAM lParam);void Draw_shape(HWND hwnd,int shape_num,int shape_direction,POINT position,COLORREF color);void Draw_block(HWND hwnd,POINT block,COLORREF color);void Init_map();void New_shape(HWND hwnd);void Down_shap
3、e(HWND hwnd);void Right_shape(HWND hwnd);void Left_shape(HWND hwnd);void Change_direction(HWND hwnd);bool Downable();bool Rightable();bool Leftable();bool Changeable();void Update_map();int Full_row();void Clean_full_row(HWND hwnd,int row);void Pause();void Draw_edge(HWND hwnd);void Pre_to_cur(HWND
4、hwnd);bool Over();void Show_over(HWND hwnd);void Init_frame(HWND hwnd);void Show_score(HWND hwnd);void Show_level(HWND hwnd);void Change_level(HWND hwnd);void Show_preview(HWND hwnd);#endif主程序: # include# include# includeblock.h# includefunction.hint block_size = 15;/小方块边长POINT cur_pos = 8,1;/图形当前坐标
5、int cur_shape_num = 0;/图形当前号码int cur_shape_direction = 0;/图形当前方向 方向表示同种图形内部的变形变化int pre_shape_num = 0;int pre_shape_direction = 0;#define X_max 20/画图区域宽度#define Y_max 30/画图区域高度int mapY_maxX_max;/画图区域坐标系COLORREF shape_color = RGB(0,200,200);/图形颜色COLORREF bg_color = RGB(150,20,20);/背景颜色COLORREF edge_c
6、olor = RGB(0,100,100);int score = 0;int level = 1;int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int iCmdShow)const int caption_height=GetSystemMetrics(SM_CYCAPTION);/标题栏高度const int frame_height=GetSystemMetrics(SM_CYFRAME);/窗口边框高度const int frame_width=GetSystemMetri
7、cs(SM_CXFRAME);/窗口侧边栏宽度static TCHAR classname=TEXT(classname);MSG msg;HWND hwnd;WNDCLASS wc;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.hbrBackgr
8、ound= CreateSolidBrush(bg_color); ;wc.lpszMenuName= NULL;wc.lpszClassName= classname;if(!RegisterClass(&wc)MessageBox(NULL,TEXT(需要windowsNT),TEXT(出错啦!),MB_OK|MB_ICONERROR);return 0;hwnd = CreateWindow(classname,TEXT(俄罗斯方块_林语),WS_SYSMENU,500,200,(frame_width*2)+(X_max+5)*block_size),/+5用于显示预览,得分,等级(f
9、rame_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);return msg.wParam;LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)switch
10、(message)case WM_CREATE:Init_map();break;case WM_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;case WM_TIMER:Down_shape(hwnd);break;case WM_KEYDOWN:switch(wParam)case VK_UP:Change_
11、direction(hwnd);break;case VK_DOWN:Down_shape(hwnd);break;case VK_LEFT:Left_shape(hwnd);break;case VK_RIGHT:Right_shape(hwnd);break;case VK_SPACE:Pause();break;break;case WM_CLOSE:KillTimer(hwnd,1);PostQuitMessage(0);break;return DefWindowProc(hwnd,message,wParam,lParam);/*void Draw_shape(HWND hwnd,
12、int shape_num,int shape_direction,POINT position,COLORREF color)/图形号、方向以及左上角坐标、颜色/画图形POINT block;for(int i=0;i4;+i)block.x = position.x + shapeshape_numshape_directioni % 4;block.y = position.y + shapeshape_numshape_directioni / 4;Draw_block(hwnd,block,color);void Draw_block(HWND hwnd,POINT block,CO
13、LORREF color)/小方块左上角坐标及颜色/画小方块RECT rc;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);HDC hdc = GetDC(hwnd);HBRUSH brush = (HBRUSH)CreateSolidBrush(color);FillRect(hdc,&rc,brush);DeleteObject(brush);R
14、eleaseDC(hwnd,hdc);void Init_map()/构造一个开口朝上的半包围结构作为边界for(int i = 0;i Y_max;i+)for(int j = 0;j X_max;j+)mapij = 0;/将边界初始化为1表明此处不可被覆盖for(i = 0;i Y_max;i+)mapi0 = 1;mapiX_max-1 = 1;for(i = 0;i X_max;i+)mapY_max-1i = 1;void Init_frame(HWND hwnd)/初始化界面,画出方框和信息Draw_edge(hwnd);HDC hdc = GetDC(hwnd);HPEN hp
15、en = CreatePen(PS_SOLID,2,shape_color);SelectObject(hdc,hpen);TCHAR prev = 预览:;/previewTextOut(hdc,X_max*block_size,block_size,prev,strlen(prev);TCHAR sco = 得分:;/scoreTextOut(hdc,X_max*block_size,10*block_size,sco,strlen(sco);Show_score(hwnd);TCHAR lev = 等级:;/levelTextOut(hdc,X_max*block_size,20*blo
16、ck_size,lev,strlen(lev);Show_level(hwnd);DeleteObject(hpen);ReleaseDC(hwnd,hdc);void New_shape(HWND hwnd)/随机生成预览图形并显示srand(unsigned)time(NULL);pre_shape_num = rand() % 7;pre_shape_direction = 0;Show_preview(hwnd);void Pre_to_cur(HWND hwnd)/将预览图形变为将要出现的图形,并生成新的预览图形cur_shape_num = pre_shape_num;cur_sh
17、ape_direction = pre_shape_direction;cur_pos.x = 8;cur_pos.y = 1;New_shape(hwnd);void Down_shape(HWND hwnd)/处理图形下移if(!Downable()/如果不可以下移、Update_map();/在坐标系中记录if(Over()/判断是否结束KillTimer(hwnd,1);Show_over(hwnd);return;int row;while(row = Full_row()/是否有满行Clean_full_row(hwnd,row);Pre_to_cur(hwnd);/产生新的图形D
18、raw_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;/图形的纵坐标+1Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);/画出下降后的图形void Right_shape(HWND hwnd)/处理右移if(!Rightab
19、le()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);void Left_shape(HWND hwnd)/处理左移if(!Leftable()return;Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,bg_color);cur_pos.x -= 1;
20、Draw_shape(hwnd,cur_shape_num,cur_shape_direction,cur_pos,shape_color);void Change_direction(HWND hwnd)/变形处理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,c
21、ur_pos,shape_color);bool Downable()/判断是否可以向下POINT down_pos,block_pos;down_pos.x = cur_pos.x;down_pos.y = cur_pos.y + 1;for(int i = 0;i 4;i+)/分别检查图形中的4个小块是否会与下方重叠block_pos.x = down_pos.x + shapecur_shape_numcur_shape_directioni % 4;block_pos.y = down_pos.y + shapecur_shape_numcur_shape_directioni / 4
22、;if(mapblock_pos.yblock_pos.x = 1)return false;return true;bool Rightable()/判断是否可以向右POINT down_pos,block_pos;down_pos.x = cur_pos.x + 1;down_pos.y = cur_pos.y;for(int i = 0;i 4;i+)/分别检查图形中的4个小块是否会与右方重叠block_pos.x = down_pos.x + shapecur_shape_numcur_shape_directioni % 4;block_pos.y = down_pos.y + shapecur_shape_numcur_shape_directioni / 4;if(mapblock_pos.yblock_pos.x = 1)return false;return true;bool Leftable()/判断是否可以向左POINT down_pos,block_pos;down_pos.x = cur_pos.x - 1;down_pos.y = cur_pos.y;for(int i = 0;i 4;i+)/分别检查图形中的4个小块是否会与左方重叠block_pos.x = down_pos.x + shapecur_shape_numcur_s
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1