您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 高中教育 > 《打砖块》游戏设计报告
——《打砖块》学院:班级:学号:姓名:一.游戏设计规则游戏开始时,小球会停在挡板正中间,且此时挡板可左右自由移动,当按下空格键后小球会弹出,并在程序设定的区域内不停的碰撞反弹。当小球碰到墙壁、挡板和砖块时,均会以相反的速度反弹,并且砖块被碰撞后会自动消失。挡板可左右移动以接住小球,若小球没有被接住,超出指定区域下边缘,则程序会自动提示“游戏结束”,点击“确定”后退出程序。当所有的砖块均消失时,程序会弹出提示“恭喜你,你赢了”。二.游戏设计思路整个游戏可以分为三个类:小球类、挡板类和砖块类。其中最简单的当然是挡板了,在回调函数中增加“WM_KEYDOWN”的按键消息,在里面编写控制挡板移动的代码,挡板的移动是通过“上、下、左、右”四个键来实现的。砖块在整个程序中负责两件事情,其一是程序初始化时随着小球和挡板出现在游戏界面中,再就是被小球碰到后消失。控制砖块出现与否是通过一个bool变量“blockExist”来实现的,当该变量为“true”时,就将砖块贴到目标DC上,否则就不贴。由于砖块数量很多,故可以用一个for循环来实现,所有的砖块用的都是一张图片,而变化的只是砖块的坐标。本游戏中最复杂的要数小球了,因为小球要涉及到很多碰撞检测和反弹,而且这其中还和砖块、挡板有联系。小球涉及到的碰撞有:小球与墙壁碰撞、小球与挡板碰撞、小球与砖块碰撞。碰撞检测实际上是检测小球的坐标是否进入了某个特定的区域,若进入了,则小球要反弹;否则,按原来的路线继续前进。小球能不断的反弹,事实上靠的是小球速度方向的改变,对于小球而言,它能出现在某个点上,是它所在坐标系中的坐标导致的。若设小球的坐标为(x_ball,y_ball),其速度为水平:vx,竖直:vy,将其坐标与速度之间建立赋值关系,即:x_ball+=vx,y_ball-=vy。只要对其坐标进行相对应的计算,就能控制小球的反弹了。三.游戏主要代码intAPIENTRYWinMain(HINSTANCEhInstance,HINSTANCEhPrevInstance,LPSTRlpCmdLine,intnCmdShow){//TODO:Placecodehere.MSGmsg;HACCELhAccelTable;//InitializeglobalstringsLoadString(hInstance,IDS_APP_TITLE,szTitle,MAX_LOADSTRING);LoadString(hInstance,IDC_APIDEMOGAME,szWindowClass,MAX_LOADSTRING);MyRegisterClass(hInstance);//Performapplicationinitialization:if(!InitInstance(hInstance,nCmdShow)){returnFALSE;}hAccelTable=LoadAccelerators(hInstance,(LPCTSTR)IDC_APIDEMOGAME);//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_APIDEMOGAME);wcex.hCursor=LoadCursor(NULL,IDC_ARROW);wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName=(LPCSTR)IDC_APIDEMOGAME;wcex.lpszClassName=szWindowClass;wcex.hIconSm=LoadIcon(wcex.hInstance,(LPCTSTR)IDI_SMALL);returnRegisterClassEx(&wcex);}BOOLInitInstance(HINSTANCEhInstance,intnCmdShow){hInst=hInstance;//StoreinstancehandleinourglobalvariablehWnd=CreateWindow(szWindowClass,szTitle,WS_OVERLAPPEDWINDOW,400,100,400,500,NULL,NULL,hInstance,NULL);if(!hWnd){returnFALSE;}ShowWindow(hWnd,nCmdShow);UpdateWindow(hWnd);GetClientRect(hWnd,&rect);//取得内部窗口区域大小HBITMAPbmp;m_bg=(HBITMAP)LoadImage(NULL,matlab.bmp,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);m_board=(HBITMAP)LoadImage(NULL,Board.bmp,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);m_ball=(HBITMAP)LoadImage(NULL,ball_0.bmp,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);bgDC=GetDC(hWnd);mdc=CreateCompatibleDC(bgDC);scDC=CreateCompatibleDC(bgDC);boardDC=CreateCompatibleDC(bgDC);ballDC=CreateCompatibleDC(bgDC);bmp=CreateCompatibleBitmap(bgDC,1024,745);SelectObject(mdc,bmp);SelectObject(scDC,m_bg);SelectObject(ballDC,m_ball);SelectObject(boardDC,m_board);inti;intbx=40,by=30;//控制砖块坐标//初始化砖块for(i=0;iBN;i++){if(block[i].blockExist)block[i].bKilled=0;block[i].blockDC=CreateCompatibleDC(bgDC);m_block[i]=(HBITMAP)LoadImage(NULL,block.bmp,IMAGE_BITMAP,32,21,LR_LOADFROMFILE);SelectObject(block[i].blockDC,m_block[i]);block[i].rc.left=bx;block[i].rc.top=by;block[i].rc.right=block[i].rc.left+64;block[i].rc.bottom=block[i].rc.top+32;bx+=60;}returnTRUE;}/********控制小球碰壁后反弹*************/voidDrawBall(){//计算X轴方向贴图坐标与速度x_ball+=vx;if(x_ball=0){x_ball=0;vx=-vx;bHeng=false;}elseif(x_ball=rect.right-32){x_ball=rect.right-32;vx=-vx;bHeng=true;}//计算Y轴方向贴图坐标与速度y_ball-=vy;if(y_ball=0){y_ball=0;vy=-vy;bZong=false;}/*if(y_ball=rect.bottom-32){y_ball=rect.bottom-32;vy=-vy;}*/}/*****判断小球是否与砖块相碰*******/voidCheckCrashBlock(){inti;floatWIDTH=96;floatHEIGHT=64;floattan=HEIGHT/WIDTH;//tanθ的值for(i=0;iBN;i++){floatx_=(float)(x_ball-block[i].rc.left);floaty_=(float)(y_ball-block[i].rc.top);if((x_ball=block[i].rc.left-32&&x_ball=block[i].rc.right)&&(y_ball=block[i].rc.top-32&&y_ball=block[i].rc.bottom)){block[i].bKilled=1;block[i].blockExist=false;num--;}elseif(tanabs(y_/x_))//在1、3区域{vx=-vx;x_ball=vx;}else//在2、4区域{vy=-vy;y_ball-=vy;}*/}}}/*****判断小球是否与挡板相碰******/voidCheckCrashBoard(){if((y_ball+32)y){if(((x_ball+64)=x)&&(x_ball=(x+128))){vy=-vy;y_ball-=vy;//vx=10;}else{MessageBox(hWnd,游戏结束,提示,MB_OK|MB_ICONSTOP);exit(0);}}}voidDrawMap(){GetClientRect(hWnd,&rect);//取得内部窗口区域大小inti;if(!win){BitBlt(mdc,0,0,1024,745,scDC,0,0,SRCCOPY);for(i=0;iBN;i++){CheckCrashBlock();if(block[i].bKilled)//||block[i].blockDC==0continue;else{BitBlt(mdc,block[i].rc.left,block[i].rc.top,block[i].rc.right-block[i].rc.left,block[i].rc.bottom-block[i].rc.top,block[i].blockDC,0,0,SRCCOPY);}}//挡板贴图BitBlt(mdc,x,y,128,32,boardDC,0,0,SRCCOPY);//小球贴图if(isKeyDown){DrawBall();CheckCrashBoard();}BitBlt(mdc,x_ball,y_ball,32,32,ballDC,32,0,SRCAND);BitBlt(mdc,x_ball,y_ball,32,32,ballDC,0,0,SRCPAINT);}//将整个图贴到目的DC上BitBlt(bgDC,0,0,1024,745,mdc,0,0,SRCCOPY);}DWORDWINAPIThreadProc(LPVOIDlpParameter){void*ct=(void*)lpParameter;while(1){DrawMap();Sleep(80);}deletect;return0;}LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam){intwmId,wmEvent;PAINTSTRUCTps;HDChdc;void*ct;TCHAR
本文标题:《打砖块》游戏设计报告
链接地址:https://www.777doc.com/doc-7378617 .html