MFC 的泡泡龙游戏
将 WinCE 的泡泡龙游戏移植到 Windows XP 使用 MFC


引言
这是一个“泡泡消除”游戏。它包含通用的 CWnd
CBubbleBreakerPanel
CBubbleMovingTool
CBubble2DArray
CBubbleImageList
CScene
CUndoManager
CBubbleSelector
背景
我喜欢 oopdreams software Inc. 开发的 WindowsCE 上的泡泡消除游戏,所以我想把它移植到 Windows XP 上,使用 MFC 实现。
Using the Code
CBubbleBreakerPanel
是从 CWnd
派生的,它是主要框架。它处理鼠标和键盘事件,以及 GDI 绘图等。
OnLButtonDown
是这样的
void CBubbleBreakerPanel::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWnd::OnLButtonDown(nFlags, point);
if (m_rcNewGameZone.PtInRect(point)){
NewScene(TRUE);
Invalidate(FALSE);
return;
}
else if (m_rcMenuZone.PtInRect(point)){
if (m_Menu.GetSafeHmenu()){
CMenu*pSubMenu = m_Menu.GetSubMenu(0);
if (pSubMenu){
ClientToScreen(&point);
}
}
return;
}
void CBubbleBreakerPanel::DrawBubbleSelector(CDC*pDC)
{
if (m_pBubbleSelector && (m_pBubbleSelector->GetCount() >= 2 ))
{
for(int iIndex = 0;iIndex<m_pBubbleSelector->GetCount();iIndex++){
CBubble*pBubble = m_pBubbleSelector->GetAt(iIndex);
if (pBubble->iState == BubbleSelected){
CPen penBlack(PS_SOLID,1,COLOR_BLACK);
(CPen*)pDC->SelectObject(&penBlack);
CBubble*pLeftBubble = (pBubble->X - 1 >= 0) ?
&m_pBubbles[pBubble->Y*m_iXBubbleNums+
pBubble->X-1] : NULL;
CBubble*pRightBubble = (pBubble->X + 1 <
m_iXBubbleNums) ? &m_pBubbles
[pBubble->Y*m_iXBubbleNums+pBubble->X+1]
: NULL;
CBubble*pTopBubble = (pBubble->Y - 1 >= 0) ?
&m_pBubbles[(pBubble->Y-1)*
m_iXBubbleNums+pBubble->X] : NULL;
CBubble*pBottomBubble = (pBubble->Y + 1 <
m_iYBubbleNums) ? &m_pBubbles
[(pBubble->Y+1)*m_iXBubbleNums+pBubble->X]
: NULL;
int iPosX = pBubble->PosX + pBubble->OffsetX;
int iPosY = pBubble->PosY + pBubble->OffsetY;
if ((pLeftBubble == NULL) ||
(pLeftBubble->iState != BubbleSelected)) {
pDC->MoveTo(iPosX - pBubble->
iRadius - 1,iPosY - pBubble->iRadius - 1);
pDC->LineTo(iPosX - pBubble->
iRadius - 1,iPosY + pBubble->iRadius + 1);
}
if ((pRightBubble == NULL) ||
(pRightBubble->iState != BubbleSelected)){
pDC->MoveTo(iPosX + pBubble->iRadius + 1,
iPosY - pBubble->iRadius - 1);
pDC->LineTo(iPosX + pBubble->iRadius + 1,
iPosY + pBubble->iRadius + 1);
}
if ((pTopBubble == NULL) || (pTopBubble->iState
!= BubbleSelected)){
pDC->MoveTo(iPosX - pBubble->
iRadius - 1,iPosY - pBubble->iRadius - 1);
pDC->LineTo(iPosX + pBubble->
iRadius + 1,iPosY - pBubble->iRadius - 1);
}
if ((pBottomBubble == NULL) || (pBottomBubble->
iState != BubbleSelected)){
pDC->MoveTo(iPosX - pBubble->iRadius - 1,
iPosY + pBubble->iRadius + 1);
pDC->LineTo(iPosX + pBubble->iRadius + 1,
iPosY + pBubble->iRadius + 1);
}
}
}
}
}
CBubbleMovingTool
被设计用来处理泡泡的移动,而 CBubbleMovingToolH
是从 CBubbleMovingTool
派生出来的,用于处理水平移动,CBubbleMovingToolV
用于处理垂直移动,HCBubbleMovingToolVH
用于先处理水平移动,再处理垂直移动。
CBubbleMovingToolH::ReArrangeRow
是这样的
void CBubbleMovingToolH::ReArrangeRow(int iyIndex)
{
if (m_pBubbles)
{
CBubble2DArray bubble2dArray(m_pBubbles,m_iXBubbleNums,m_iYBubbleNums);
CBubble *pTempBubbles = new CBubble[m_iXBubbleNums];
int ixValidCount = 0;
if (pTempBubbles){
for (int ixIndex = m_iXBubbleNums - 1;ixIndex >= 0;ixIndex--)
{
CBubble *pBubble = bubble2dArray.GetBubble
(ixIndex,iyIndex);
if (pBubble && (pBubble->iState == BubbleActive) )
{
pTempBubbles[ixValidCount].iColorIndex =
pBubble->iColorIndex;
pTempBubbles[ixValidCount].iState =
pBubble->iState;
ixValidCount++;
}
}
for (int ixIndex = 0;ixIndex<m_iXBubbleNums;ixIndex++){
CBubble *pBubble = bubble2dArray.GetBubble
(ixIndex,iyIndex);
pBubble->iState = BubbleNone;
}
}
if (pTempBubbles)
{
for (int ixIndex = m_iXBubbleNums - 1 ,ixValidIndex = 0;
ixValidIndex<ixValidCount;ixIndex--)
{
CBubble *pBubble = bubble2dArray.GetBubble
(ixIndex,iyIndex);
if (pBubble)
{
pBubble->iColorIndex =
pTempBubbles[ixValidIndex].iColorIndex;
pBubble->iState = pTempBubbles
[ixValidIndex].iState;
ixValidIndex++;
}
}
delete []pTempBubbles;
pTempBubbles = NULL;
}
}
}
CBubbleSelector
被设计用来处理泡泡的选择。
CBubbleSelector PtInSelector
是这样的
BOOL CBubbleSelector::PtInSelector(CPoint point)
{
BOOL bFounded = FALSE;
if (GetCount() >= 2)
{
for(int iIndex = 0;iIndex<GetCount();iIndex++)
{
CBubble*pBubble = GetAt(iIndex);
if (pBubble)
{
CRect rcBubbble = pBubble->GetRect();
rcBubbble.InflateRect(1,1,1,1);
if (rcBubbble.PtInRect(point)){
bFounded = TRUE;// Action
}
}
}
}
return bFounded;
}
关注点
我计划为其提供完整的 GDI+ 支持来进行绘图。
我也计划确保它在 Windows 平台上完美运行。
历史
- 2009年10月22日:首次发布
关于作者
我从 1997 年开始编写 Windows 程序。
我对 Windows 内核编程、图像处理、网络技术、UI 框架以及 C/C++/C#/MFC 等方面感兴趣。
我从 2000 年开始在政府部门工作。