VC++角色遊戲中的人物初始化模組代碼執行個體_C 語言

來源:互聯網
上載者:User

本文以一個執行個體講述VC++遊戲中的人物角色動畫初始化實現代碼,本代碼只是實現人物角色動畫的初始化,不包括其它功能,並不是完整的一個遊戲應用,現在將這個角色初始化代碼與大家分享。希望能夠對大家學習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++) { //一個小技巧——擷取人物每一幀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;}//向前移動(如果移動到了客戶區中間, 不繼續移動了)void CCharacter::MoveFront(){ int border = (VIEWWIDTH - m_sCharacter.cx) / 2; if(this->m_leftTop.x <= border) { this->m_leftTop.x += 4; }}//下一幀void CCharacter::NextFrame(){ // 本可以直接使用求餘運算, 但是%求餘運算速 // 度及效率不好, 所以使用簡單的判斷操作代替 //進入下一幀 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));}//釋放記憶體資源void CCharacter::ReleaseCharacter(){ for(int i=0; i<this->MAXFRAME; i++) this->m_imgCharacter[i].Destroy();}

以下是人物類CCharacter的實現代碼:

#pragma once#include<atlimage.h>//地面高度#define ELEVATION 42class CCharacter{//靜態常成員變數private: //最大幀數:16 static const int MAXFRAME = 16; //視口客戶區寬度 static const int VIEWWIDTH = 790; //視口客戶區高度 static const int VIEWHEIGHT = 568;//成員變數private: CImage m_imgCharacter[MAXFRAME];//人物 CSize m_sCharacter;//人物大小 CPoint m_leftTop;//人物的位置(左上方點) int m_curFrame;//人物的當前幀//成員函數public: //初始化人物 bool InitCharacter(); //向前移動 void MoveFront(); //下一幀 void NextFrame(); //繪製人物(註:這裡bufferDC是引用參數) void StickCharacter(CDC& bufferDC); //釋放記憶體資源 void ReleaseCharacter();//構造與析構public: CCharacter(void); ~CCharacter(void);};

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.