VC++角色游戏中的人物初始化模块代码实例

2025-05-27 0 88

本文以一个实例讲述VC++游戏中的人物角色动画初始化实现代码,本代码只是实现人物角色动画的初始化,不包括其它功能,并不是完整的一个游戏应用,现在将这个角色初始化代码与大家分享。希望能够对大家学习VC++有所帮助。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71
#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的实现代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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

};

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 VC++角色游戏中的人物初始化模块代码实例 https://www.kuaiidc.com/75814.html

相关文章

发表评论
暂无评论