資源描述:
《vc++游戲編程》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、第一章游戲基礎(chǔ)????????????一動畫?游戲是動畫的延伸,只要知道了動畫的原理,在動畫變化時,使它根據(jù)鍵盤或鼠標的輸入操作而變化,就是游戲了。所以在介紹游戲編程之前,先講講動畫。動畫一般分三種形式:形變、位變、色變。如下:?1.1.形變?即幾種形狀的圖形或位圖依次轉(zhuǎn)變而成。(圖1-1)圖1-1??先看一個簡單的實例:a.a(chǎn).?新建工程1_1,選擇單文檔,完成。b.b.?在view類加變量intituxing,并在構(gòu)造函數(shù)里賦值為ituxing=0;c.c.?在OnDraw(CDC*pDC)函數(shù)上添上以下語句:voidCMy1_1View::OnDraw(CDC*
2、pDC){CMy1_1Doc*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere//清除當前圖形CBrushmybrush1;mybrush1.CreateSolidBrush(RGB(255,255,255));CRectmyrect1(0,0,1024,768);pDC->FillRect(myrect1,&mybrush1);//判斷當前圖形ituxingif(ituxing==1)//ituxing=1,畫矩形pDC->Rectangle(20,20,89,89);
3、elseif(ituxing==2)//ituxing=2,畫圓pDC->Ellipse(20,20,89,89);else//否則,畫三角形{pDC->MoveTo(20,20);pDC->LineTo(89,45);pDC->LineTo(20,89);pDC->LineTo(20,20);}}d.d.?添加OnTimer(UINTnIDEvent)和OnCreate(LPCREATESTRUCTlpCreateStruct),并加上語句如下:voidCMy1_1View::OnTimer(UINTnIDEvent){//TODO:Addyourmessageha
4、ndlercodehereand/orcalldefault//獲取指針pdcCDC*pDC=GetDC();//調(diào)用OnDraw(pDC)重畫OnDraw(pDC);//使ituxing循環(huán)ituxing=(ituxing+1)%3;?CView::OnTimer(nIDEvent);}?intCMy1_1View::OnCreate(LPCREATESTRUCTlpCreateStruct){if(CView::OnCreate(lpCreateStruct)==-1)return-1;//TODO:Addyourspecializedcreationcodehe
5、re//設(shè)置更新時間SetTimer(1,750,NULL);return0;}a.e.?完成。?1.2.位變?即同一個圖形或位圖依次在不同的位置上出現(xiàn)而形成動畫。(圖1-2)?圖1-2?見實例1_2:a.a.??????復(fù)制實例1_1文件夾,改名為1_2。把voidCMy1_1View::OnTimer(UINTnIDEvent)中的ituxing=(ituxing+1)%3;改為ituxing=(ituxing+1)%10;b.b.??????把OnDraw(CDC*pDC)函數(shù)改為:voidCMy1_1View::OnDraw(CDC*pDC){CMy1_1Do
6、c*pDoc=GetDocument();ASSERT_VALID(pDoc);//TODO:adddrawcodefornativedatahere//清除當前圖形CBrushmybrush1;mybrush1.CreateSolidBrush(RGB(255,255,255));CRectmyrect1(0,0,1024,768);pDC->FillRect(myrect1,&mybrush1);?//用當前圖形位置ituxing//畫圓pDC->Ellipse(30*ituxing,30*ituxing,30*ituxing+50,30*ituxing+50);
7、}a.c.??????執(zhí)行,OK!1.3.?色變?即利用不同顏色的轉(zhuǎn)變而形成有動感的圖畫。(圖1-3)圖1-3??實例1_3:a.a.??????新建單文檔工程1_3。b.b.??????添加參數(shù)inticolor并賦值為0。c.c.??????添加函數(shù)OnTimer(UINTnIDEvent)和OnCreate(LPCREATESTRUCTlpCreateStruct),并添加語句如下:?voidCMy1_3View::OnTimer(UINTnIDEvent){//TODO:Addyourmessagehandlercodehereand/orc