VC++腳色游戲中的人物初始化模塊代碼實例。本站提示廣大學習愛好者:(VC++腳色游戲中的人物初始化模塊代碼實例)文章只能為提供參考,不一定能成為您想要的結果。以下是VC++腳色游戲中的人物初始化模塊代碼實例正文
本文以一個實例講述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 42
class 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);
};