VC++ role game character initialization module code instance
- 2020-04-02 02:33:27
- OfStack
This article with an example of VC++ game character animation initialization code, the code is to achieve the character animation initialization, does not include other functions, is not a complete game application, now the character initialization code to share with you. I hope to be able to help you learn VC++.
#include "StdAfx.h"
#include "Character.h"
CCharacter::CCharacter(void)
{
}
CCharacter::~CCharacter(void)
{
}
//Initialize character
bool CCharacter::InitCharacter()
{
int i;
CString path;
//Initialize each frame
for(i=0; i<this->MAXFRAME; i++)
{
//A tip - get the path of each PNG frame for the character
path.Format(L"res\%d.png", i+1);
this->m_imgCharacter[i].Load(path);
//If the load fails
if(this->m_imgCharacter[i].IsNull())
{
return false;
}
}
//Initialize character The size of the
int w = m_imgCharacter[0].GetWidth();
int h = m_imgCharacter[0].GetHeight();
this->m_sCharacter.SetSize(w, h);
//Initialize character location
this->m_leftTop.SetPoint(0,
VIEWHEIGHT - h - ELEVATION);
//Initialize to frame 1
this->m_curFrame = 0;
return true;
}
//Move forward (if you move to the middle of the client area, do not continue to move)
void CCharacter::MoveFront()
{
int border = (VIEWWIDTH - m_sCharacter.cx) / 2;
if(this->m_leftTop.x <= border)
{
this->m_leftTop.x += 4;
}
}
//The next frame
void CCharacter::NextFrame()
{
//You could have used the remainder operation directly, but the % remainder operation speed
//Degree and efficiency are not good, so use simple judgment operation instead
// Enter the The next frame
this->m_curFrame++;
if(this->m_curFrame == this->MAXFRAME)
this->m_curFrame = 0;
}
//Draw the characters
void CCharacter::StickCharacter(CDC& bufferDC)
{
int i = this->m_curFrame;
//Transparent textures
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));
}
//Freeing memory resources
void CCharacter::ReleaseCharacter()
{
for(int i=0; i<this->MAXFRAME; i++)
this->m_imgCharacter[i].Destroy();
}
The following is the implementation code of the character class CCharacter:
#pragma once
#include<atlimage.h>
//The height
#define ELEVATION 42
class CCharacter
{
//Static constant member variables
private:
//Maximum frames :16
static const int MAXFRAME = 16;
//Viewport client area width
static const int VIEWWIDTH = 790;
//Visual port customer area height
static const int VIEWHEIGHT = 568;
//Member variables
private:
CImage m_imgCharacter[MAXFRAME];//figure
CSize m_sCharacter;//figure The size of the
CPoint m_leftTop;//figure The location of the ( The upper left corner point )
int m_curFrame;//figure The current frame
//A member function
public:
// Initialize the figure
bool InitCharacter();
//To move forward
void MoveFront();
//The next frame
void NextFrame();
// draw figure( Note: here bufferDC Is a reference parameter )
void StickCharacter(CDC& bufferDC);
//Freeing memory resources
void ReleaseCharacter();
//Structure and destruction
public:
CCharacter(void);
~CCharacter(void);
};