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

📄 chessman.cpp

📁 华容道游戏
💻 CPP
字号:
#include "StdAfx.h"
#include "chessman.h"
#include "resource.h"       // main symbols

CBitmap CChessMan::s_bmpMan[];
CPoint CChessMan::s_ptStart = CPoint(0, 0);

CChessMan::CChessMan(void)
{
	m_ptPos = CPoint(0, 0);
	m_nType = 0;
}

CChessMan::CChessMan(CPoint pt, char nType)
{
	m_ptPos = pt;
	m_nType = nType;
}

CChessMan::CChessMan(CChessMan& m)
{
	m_ptPos = m.m_ptPos;
	m_nType = m.m_nType;
}

CChessMan::~CChessMan(void)
{
}

BOOL CChessMan::InitImage(void)
{
	if (!s_bmpMan[0].LoadBitmap(IDB_MAN1)) return FALSE;
	if (!s_bmpMan[1].LoadBitmap(IDB_MAN2)) return FALSE;
	if (!s_bmpMan[2].LoadBitmap(IDB_MAN3)) return FALSE;
	if (!s_bmpMan[3].LoadBitmap(IDB_MAN4)) return FALSE;
	if (!s_bmpMan[4].LoadBitmap(IDB_MAN5)) return FALSE;
	return TRUE;
}

void CChessMan::ClearImage(void)
{
	for (int i=0; i<MAN_COUNT; i++)
	{
		s_bmpMan[i].DeleteObject();
	}
}

void CChessMan::Draw(CDC* pDC)
{
	Draw(GetStartPoint(), pDC);
}

void CChessMan::Draw(CPoint pt, CDC* pDC)
{
	CDC memDC;
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&s_bmpMan[m_nType]);

	CSize sz = GetSize();

	pDC->BitBlt(pt.x, pt.y, sz.cx * CELL_SIZE, sz.cy * CELL_SIZE, &memDC, 0, 0, SRCCOPY);

	memDC.DeleteDC();
}

CSize CChessMan::GetSize(void)
{
	switch (m_nType)
	{
		case CT_MAIN:
		case CT_H2:
			return CSize(2, 1);
		case CT_V2:
			return CSize(1, 2);
		case CT_H3:
			return CSize(3, 1);
		case CT_V3:
			return CSize(1, 3);
		default:
			return CSize(0, 0);
	}
}

BOOL CChessMan::PointInMe(CPoint pt)
{
	CSize sz = CSize(GetSize().cx * CELL_SIZE, GetSize().cy * CELL_SIZE);
	CRect rc(GetStartPoint(), sz);
	return rc.PtInRect(pt);
}

CPoint CChessMan::GetStartPoint(void)
{
	int x = s_ptStart.x + m_ptPos.x * CELL_SIZE;
	int y = s_ptStart.y + m_ptPos.y * CELL_SIZE;
	return CPoint(x, y);
}

int CChessMan::GetDirection(void)
{
	switch (m_nType)
	{
		case CT_MAIN:
		case CT_H2:
		case CT_H3:
			return 0;
		case CT_V2:
		case CT_V3:
			return 1;
		default:
			return 0;
	}
}

CRect CChessMan::GetRect(void)
{
	CSize sz = CSize(GetSize().cx * CELL_SIZE, GetSize().cy * CELL_SIZE);
	return CRect(GetStartPoint(), sz);
}

BOOL CChessMan::IsRectInMe(CRect rc)
{
	CRect rcMe = CRect(m_ptPos, GetSize());
	return !(rcMe & rc).IsRectEmpty();
}

CSelectChessMan::CSelectChessMan() : CChessMan()
, m_bUsed(FALSE)
{
}

CSelectChessMan::~CSelectChessMan() 
{
}

void CSelectChessMan::CopyChessMan(CChessMan& man, int nIndex)
{
	m_nIndex = nIndex;
	m_bUsed = TRUE;
	m_ptPos = man.m_ptPos;
	m_nType = man.m_nType;
}

void CSelectChessMan::SetMoveStep(int nMoveUp, int nMoveDown, int nMoveLeft, int nMoveRight)
{
	m_nMoveUp = nMoveUp;
	m_nMoveDown = nMoveDown;
	m_nMoveLeft = nMoveLeft;
	m_nMoveRight = nMoveRight;
}

CRect CSelectChessMan::GetRect(void)
{
	CSize sz = CSize(GetSize().cx * CELL_SIZE, GetSize().cy * CELL_SIZE);
	return CRect(GetMovePoint(), sz);
}

CPoint CSelectChessMan::GetMovePoint(void)
{
	CPoint ptStart = GetStartPoint();
	CSize szMove = m_ptMouseNow - m_ptMouseStart;

	if (szMove.cx > 0)
	{
		szMove.cx = (szMove.cx > m_nMoveRight*CELL_SIZE) ? m_nMoveRight*CELL_SIZE : szMove.cx;
	}
	else
	{
		szMove.cx = (-szMove.cx > m_nMoveLeft*CELL_SIZE) ? -m_nMoveLeft*CELL_SIZE : szMove.cx;
	}

	if (szMove.cy > 0)
	{
		szMove.cy = (szMove.cy > m_nMoveDown*CELL_SIZE) ? m_nMoveDown*CELL_SIZE : szMove.cy;
	}
	else
	{
		szMove.cy = (-szMove.cy > m_nMoveUp*CELL_SIZE) ? -m_nMoveUp*CELL_SIZE : szMove.cy;
	}

	ptStart.Offset(szMove);
	return ptStart;
}

void CSelectChessMan::Draw(CDC* pDC)
{
	CChessMan::Draw(GetMovePoint(), pDC);
}

CSize CSelectChessMan::PlaceChessMan(CPoint pt)
{
	CSize szMove = m_ptMouseNow - m_ptMouseStart;
	float fx = float(szMove.cx) / CELL_SIZE;
	float fy = float(szMove.cy) / CELL_SIZE;
	int cx = int((fx > 0) ? fx + 0.5 : fx - 0.5);
	int cy = int((fy > 0) ? fy + 0.5 : fy - 0.5);
	cx = (cx > 0) ? (cx > m_nMoveRight ? m_nMoveRight : cx) : (cx < -m_nMoveLeft ? -m_nMoveLeft : cx);
	cy = (cy > 0) ? (cy > m_nMoveDown ? m_nMoveDown : cy) : (cy < -m_nMoveUp ? -m_nMoveUp : cy);
	return CSize(cx, cy);
}

⌨️ 快捷键说明

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