⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maze.cpp

📁 Visual C++游戏开发技术与实例一书配套光盘。包含了冒险屠宰场、入侵者、赛车、网络五子棋、网络台球、对战坦克大战和面包圈7个游戏实例的完整源代码。
💻 CPP
字号:
// Maze.cpp: implementation of the CMaze class.
//
//////////////////////////////////////////////////////////////////////
#include "ddutil.h"
#include <list>
#include "Maze.h"
#include <fstream>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace std;
CMaze::CMaze(char* pszTextureBMP): m_bDisplayGrid(false),m_pPtDeploy(0)
{
	m_pszTextureBMP = pszTextureBMP;
	m_szName[0]=0;
}

CMaze::~CMaze()
{
	delete[] m_pPtDeploy;
}


bool CMaze::LoadMaze(char *pFilename)
{
	fstream MazeData(pFilename,std::ios::in );
    MazeData.getline(m_szName,200);
	long nHV;
	SMazeCoord MazeCoord;
	m_lHLines.erase(m_lHLines.begin(), m_lHLines.end());
	m_lVLines.erase(m_lVLines.begin(), m_lVLines.end());
	delete[] m_pPtDeploy;

	// Read number of horizontal lines
	MazeData >> nHV;   // Number of Horizontal rows
	for (int nI = 0; nI < nHV; nI++) {
		MazeData >> MazeCoord.nY >> MazeCoord.nX1 >> MazeCoord.nX2;
		m_lHLines.push_back(MazeCoord);
	}

	MazeData >> nHV;   // Number of Horizontal rows
	for (nI = 0; nI < nHV; nI++) {
		MazeData >> MazeCoord.nX >> MazeCoord.nY1 >> MazeCoord.nY2;
		m_lVLines.push_back(MazeCoord);
	}

	MazeData >> m_nNoOfDeployPt; // No of sprite deploy location on the Maze.
	m_pPtDeploy = new POINT[m_nNoOfDeployPt];
	if (m_pPtDeploy == NULL)
		return false;
	for (nI = 0; nI < m_nNoOfDeployPt; nI++) {
		MazeData >> m_pPtDeploy[nI].x >> m_pPtDeploy[nI].y ;
	}

	return true;
}

void CMaze::Draw(CSurface *pSurface)
{
    HBITMAP        hBMP = NULL;
    BITMAP         bmp;

    if( pSurface == NULL ) 
        return;

	pSurface->Clear(0);
    //  Try to load the bitmap as a resource, if that fails, try it as a file
    hBMP = (HBITMAP) LoadImage( GetModuleHandle(NULL), m_pszTextureBMP, 
                                IMAGE_BITMAP, 0, 0, 
                                LR_CREATEDIBSECTION );
    if( hBMP == NULL )
    {
        hBMP = (HBITMAP) LoadImage( NULL, m_pszTextureBMP, 
                                    IMAGE_BITMAP, 0, 0, 
                                    LR_LOADFROMFILE | LR_CREATEDIBSECTION );
        if( hBMP == NULL )
            return;
    }
	// Get size of the bitmap
    GetObject( hBMP, sizeof(bmp), &bmp );
	m_bmpTexture = bmp;

	list<SMazeCoord>::iterator iList;

	//Draw horizontal lines
	for (iList = m_lHLines.begin(); iList != m_lHLines.end(); iList++) 
	{
		for (int nI= iList->nX1; nI <= iList->nX2; nI++) {
			pSurface->DrawBitmap(hBMP,nI * bmp.bmWidth,iList->nY * bmp.bmHeight, bmp.bmWidth, bmp.bmHeight);
		}
	}

	// Draw vertical lines
	for (iList = m_lVLines.begin(); iList != m_lVLines.end(); iList++) 
	{
		for (int nI= iList->nY1; nI <= iList->nY2; nI++) {
			pSurface->DrawBitmap(hBMP,iList->nX * bmp.bmWidth, nI * bmp.bmHeight, bmp.bmWidth, bmp.bmHeight);
		}
	}
	DeleteObject( hBMP );

	// Draw grid based on the texture size
	if (m_bDisplayGrid) {
		POINT p1= {0,0};
		POINT p2={0,0};
		// Draw horizontal lines
		p2.x=pSurface->GetSurfaceDesc().dwWidth;
		for (long i= 0 + bmp.bmHeight; i < pSurface->GetSurfaceDesc().dwHeight; i+= bmp.bmHeight) {
			p1.y = p2.y=i;
			pSurface->DrawLine(p1,p2,RGB(255,0,0));
		}

  		// Draw vertical lines
		p1.y=0;
		p2.y=pSurface->GetSurfaceDesc().dwHeight;
		for ( i= 0 + bmp.bmWidth; i < pSurface->GetSurfaceDesc().dwWidth; i+= bmp.bmWidth) {
			p1.x = p2.x=i;
			pSurface->DrawLine(p1,p2,RGB(255,0,0));
		}

	}
	if (m_bDisplayDeploy) {
		RECT rc;
		for (long i = 0; i < m_nNoOfDeployPt ; i++) {
			rc.top = m_pPtDeploy[i].y * bmp.bmHeight;
			rc.bottom = rc.top + m_szDeploy.cy;
			rc.left = m_pPtDeploy[i].x * bmp.bmWidth;
			rc.right = rc.left + m_szDeploy.cx;
			pSurface->DrawRect(rc,RGB(0,255,0));
		}

	}

}

 

void CMaze::DisplayGrid(bool bDisplay /* = false */)
{
	m_bDisplayGrid = bDisplay;
}


bool CMaze::IsHit(RECT& rcRect)
{
	list<SMazeCoord>::iterator iList;
	RECT rcMaze;
	RECT rcIntersect;
	
	//Check horizontal lines
	for (iList = m_lHLines.begin(); iList != m_lHLines.end(); iList++) 
	{
		rcMaze.top = iList->nY * m_bmpTexture.bmHeight;
		rcMaze.left = iList->nX1 * m_bmpTexture.bmWidth;
		rcMaze.bottom = (iList->nY + 1) * m_bmpTexture.bmHeight;
		rcMaze.right = (iList->nX2 + 1) * m_bmpTexture.bmWidth;
		
		
		if (::IntersectRect(&rcIntersect, &rcMaze, &rcRect))
			return true;
	}
	
	// Check vertical lines
	for (iList = m_lVLines.begin(); iList != m_lVLines.end(); iList++) 
	{
		rcMaze.top = iList->nY1 * m_bmpTexture.bmHeight;
		rcMaze.left = iList->nX * m_bmpTexture.bmWidth;
		
		rcMaze.bottom = (iList->nY2 + 1) * m_bmpTexture.bmHeight;
		rcMaze.right = (iList->nX + 1) * m_bmpTexture.bmWidth;
		
		if (::IntersectRect(&rcIntersect, &rcMaze, &rcRect))
			return true;
	}
	
	
	return false;
}

void CMaze::DisplayDeployRects(SIZE& sz, bool bDisplay)
{
	m_szDeploy = sz;
	m_bDisplayDeploy = bDisplay;
}

POINT CMaze::GetRandDeployPoint()
{
	if (m_pPtDeploy) {
		return m_pPtDeploy[rand() % m_nNoOfDeployPt];
	} else {
		POINT pt = {0,0};
		return pt;
	}
}

const char* CMaze::GetName()
{
	return m_szName;
}

void CMaze::GetPathFindingArray(short* pnPathArray, int nCols)
{
	list<SMazeCoord>::iterator iList;

	for (iList = m_lHLines.begin(); iList != m_lHLines.end(); iList++) 
	{
		for (int nI= iList->nX1; nI <= iList->nX2; nI++) {
			pnPathArray[iList->nY * nCols + nI ] = 1;
		}
	}

	// Draw vertical lines
	for (iList = m_lVLines.begin(); iList != m_lVLines.end(); iList++) 
	{
		for (int nI= iList->nY1; nI <= iList->nY2; nI++) {
			pnPathArray[nI*nCols + iList->nX] = 1;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -