c语言角色游戏,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; iMAXFRAME; 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; iMAXFRAME; i++)

this->m_imgCharacter[i].Destroy();

}

以下是人物类CCharacter的实现代码:

#pragma once

#include

//地面高度

#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);

};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值