VC++角色游戲中的人物初始化模塊代碼實(shí)例
本文以一個(gè)實(shí)例講述VC++游戲中的人物角色動(dòng)畫初始化實(shí)現(xiàn)代碼,本代碼只是實(shí)現(xiàn)人物角色動(dòng)畫的初始化,不包括其它功能,并不是完整的一個(gè)游戲應(yīng)用,現(xiàn)在將這個(gè)角色初始化代碼與大家分享。希望能夠?qū)Υ蠹覍W(xué)習(xí)VC++有所幫助。
#include "StdAfx.h" #include "Character.h" CCharacter::CCharacter(void) { } CCharacter::~CCharacter(void) { } //初始化人物 bool CCharacter::InitCharacter() { int i; CString path; //初始化每一幀 for(i=0; i<this->MAXFRAME; i++) { //一個(gè)小技巧——獲取人物每一幀png的路徑 path.Format(L"res\\%d.png", i+1); this->m_imgCharacter[i].Load(path); //如果加載失敗 if(this->m_imgCharacter[i].IsNull()) { return false; } } //初始化人物大小 int w = m_imgCharacter[0].GetWidth(); int h = m_imgCharacter[0].GetHeight(); this->m_sCharacter.SetSize(w, h); //初始化人物位置 this->m_leftTop.SetPoint(0, VIEWHEIGHT - h - ELEVATION); //初始化為第1幀 this->m_curFrame = 0; return true; } //向前移動(dòng)(如果移動(dòng)到了客戶區(qū)中間, 不繼續(xù)移動(dòng)了) void CCharacter::MoveFront() { int border = (VIEWWIDTH - m_sCharacter.cx) / 2; if(this->m_leftTop.x <= border) { this->m_leftTop.x += 4; } } //下一幀 void CCharacter::NextFrame() { // 本可以直接使用求余運(yùn)算, 但是%求余運(yùn)算速 // 度及效率不好, 所以使用簡單的判斷操作代替 //進(jìn)入下一幀 this->m_curFrame++; if(this->m_curFrame == this->MAXFRAME) this->m_curFrame = 0; } //繪制人物 void CCharacter::StickCharacter(CDC& bufferDC) { int i = this->m_curFrame; //透明貼圖 this->m_imgCharacter[i].TransparentBlt(bufferDC, this->m_leftTop.x, this->m_leftTop.y, this->m_sCharacter.cx, this->m_sCharacter.cy, RGB(0, 0, 0)); } //釋放內(nèi)存資源 void CCharacter::ReleaseCharacter() { for(int i=0; i<this->MAXFRAME; i++) this->m_imgCharacter[i].Destroy(); }
以下是人物類CCharacter的實(shí)現(xiàn)代碼:
#pragma once #include<atlimage.h> //地面高度 #define ELEVATION 42 class CCharacter { //靜態(tài)常成員變量 private: //最大幀數(shù):16 static const int MAXFRAME = 16; //視口客戶區(qū)寬度 static const int VIEWWIDTH = 790; //視口客戶區(qū)高度 static const int VIEWHEIGHT = 568; //成員變量 private: CImage m_imgCharacter[MAXFRAME];//人物 CSize m_sCharacter;//人物大小 CPoint m_leftTop;//人物的位置(左上角點(diǎn)) int m_curFrame;//人物的當(dāng)前幀 //成員函數(shù) public: //初始化人物 bool InitCharacter(); //向前移動(dòng) void MoveFront(); //下一幀 void NextFrame(); //繪制人物(注:這里bufferDC是引用參數(shù)) void StickCharacter(CDC& bufferDC); //釋放內(nèi)存資源 void ReleaseCharacter(); //構(gòu)造與析構(gòu) public: CCharacter(void); ~CCharacter(void); };
相關(guān)文章
輸入一個(gè)字符串,取出其中的整數(shù)(實(shí)現(xiàn)代碼)
輸入一個(gè)字符串,內(nèi)含所有數(shù)字和非數(shù)字字符。將其中連續(xù)的數(shù)字作為一個(gè)整數(shù),依次存放到一個(gè)數(shù)組中,統(tǒng)計(jì)共有多少個(gè)整數(shù),并輸出這些數(shù)2013-09-09OpenCV實(shí)現(xiàn)簡單攝像頭視頻監(jiān)控程序
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)簡單攝像頭視頻監(jiān)控程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08C++實(shí)現(xiàn)LeetCode(90.子集合之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(90.子集合之二),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實(shí)現(xiàn)自定義撤銷重做功能的示例代碼
在使用c++做界面開發(fā)的時(shí)候,尤其是實(shí)現(xiàn)白板功能時(shí)需要自己實(shí)現(xiàn)一套撤銷重做功能.如果是qt則有QUndoable對象,可以直接拿來用。但是如果是使用gdi繪圖,則可能需要自己實(shí)現(xiàn)了。本文就來用C++實(shí)現(xiàn)自定義撤銷重做功能,需要的可以參考一下2022-12-12