cocos2dx 泡泡龙游戏开发思路探究

前端之家收集整理的这篇文章主要介绍了cocos2dx 泡泡龙游戏开发思路探究前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

泡泡龙游戏如上图,开发重点有以下几个;

1.建立泡泡矩阵

Bubble* m_board[11][11]; //建立一个面板有11行11列,依次把泡泡存到响应索引里边去,没有泡泡的就设置NULL
Bubble* m_wait[4]; //保存后边准备的4个泡泡
Bubble* m_curReady; //准备发射的泡泡

2.发射泡泡

void GameScene::ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent)
{
	CCPoint pos = pTouch->getLocation();
	m_real = ccpNormalize(ccpSub(pos,m_curReady->getPosition())); //标准化长度,以设置泡泡飞行速度

	setDisableEnable();
	this->scheduleUpdate();
}

void GameScene::update(float delta)
{
	if (isCollisionWithBorder())
	{
		m_real.x = -m_real.x;
	}

	CCPoint pos = m_curReady->getPosition();
	m_curReady->setPosition(ccp(pos.x + m_real.x * BUBBLE_SPEED,pos.y + m_real.y * BUBBLE_SPEED));
        //碰到其它泡泡的画就让它定住
	if (isCollision())
	{		
		m_real = CCPointZero;
		adjustBubblePosition();
        
        //查找身边符合条件的泡泡进行清除
		execClearBubble(m_curReady);
        
		//检查没有连接的泡泡就让它掉落
		ROWCOL_LIST fallList = checkFallBubble();
		FallBubble(fallList);
        
		this->unscheduleUpdate();
 		changeWaitToReady();
		setEnable();
	}

}

3. 检测是否碰到其他泡泡

bool GameScene::isCollision()
{
	bool bRet = false;

	CCSize size = CCDirector::sharedDirector()->getWinSize();
	if (m_curReady->getPosition().y > size.height - BUBBLE_RADIUS)
	{
	   bRet = true;
           return bRet;
	}

	for (BUBBLE_LIST::reverse_iterator iterBubble = m_listBubble.rbegin(); iterBubble != m_listBubble.rend(); ++iterBubble)
	{
	   Bubble *pBubble = *iterBubble;
	   if (pBubble && isCollisionWithBubble(pBubble->getPosition(),BUBBLE_RADIUS,m_curReady->getPosition(),BUBBLE_RADIUS))
	   {
	      bRet = true;1
	      return bRet;
	   }
	}

	return bRet;
}


判断两圆是否临近

bool GameScene::isCollisionWithBubble(CCPoint pos1,float radius1,CCPoint pos2,float radius2)
{
    //根据两球的面积来判断是否临近,x和y的相减来得出两圆的距离然后这样求出面积来比较   右边的是正统两圆的面积
    return pow(pos1.x - pos2.x,2) + pow(pos1.y - pos2.y,2) < pow(radius1 + radius2,2);	//(x1-x2)^2 + (y1-y2)^2 < (r1 + r2)^2
}



检查是否有3个同色的泡泡相连并消除.图解如上

假设中心是发射的泡泡,那么依次检查其他泡泡的顺序为123456,颜色相同的添加到列表,都检查完后,继续开始从颜色相同列表里的下一个泡泡开始重复上述步骤.

ROWCOL_LIST GameScene::findSameBubble(Bubble *pReadyBubble)
{
	ROWCOL_LIST samelist;
	BUBBLE_COLOR nColor= pReadyBubble->getBubbleColor();
	int nRow = pReadyBubble->getRowIndex();
	int nCol = pReadyBubble->getColumnIndex();
	samelist.push_back(RowCol(nRow,nCol));

	ROWCOL_LIST::iterator itCur = samelist.begin();

	do 
	{
		std::vector<RowCol> vecRowCol;
        //以发射的泡泡为中心开始对周围递归检查
		GetAround(itCur->m_nRow,itCur->m_nCol,vecRowCol);

		for (size_t i = 0; i < vecRowCol.size(); i++)
		{
			Bubble* pCurBubble = m_board[ vecRowCol[i].m_nRow ][ vecRowCol[i].m_nCol ];
			if (pCurBubble && pCurBubble->getBubbleColor() == nColor)
			{
				RowCol rc(vecRowCol[i].m_nRow,vecRowCol[i].m_nCol);
				ROWCOL_LIST::iterator itFind = std::find(samelist.begin(),samelist.end(),rc);
				//如果此点不在相同的相同表里就添加斤
				if (itFind == samelist.end())
				{
					samelist.push_back(vecRowCol[i]);
				}
			}
		}
		itCur++;
	} while (itCur != samelist.end());
	
	return samelist;
}

消除相连的泡泡

void GameScene::clearBubble(const ROWCOL_LIST &bubbleList)
{
	int nRow,nCol;
	for (ROWCOL_LIST::const_iterator iterBubble = bubbleList.begin();
		iterBubble != bubbleList.end();
		iterBubble++)
	{
		nRow = iterBubble->m_nRow;
		nCol = iterBubble->m_nCol;

		Bubble *obj = m_board[nRow][nCol];
		if (obj)
		{
			removeBubbleAction(obj);
			m_board[nRow][nCol] = NULL;
		}
	
		BUBBLE_LIST::iterator itFind = std::find(m_listBubble.begin(),m_listBubble.end(),obj);
		if (itFind != m_listBubble.end())
		{
			m_listBubble.erase(itFind);
		}
		
	}
}

掉落没有连接的泡泡

ROWCOL_LIST GameScene::checkFallBubble()
{
	ROWCOL_LIST LinkBubbleList;

    //把第一行的所有泡泡添加到连接列表里
	for (int i = 0; i < MAX_COLS; i++)
	{
		if (m_board[0][i] != NULL)
		{
			LinkBubbleList.push_back(RowCol(0,i));
		}
	}
	if (LinkBubbleList.empty())
	{
		return LinkBubbleList;
	}

	//循环开始从第一个泡泡检查周围是否有连接的.
	ROWCOL_LIST::iterator itCur = LinkBubbleList.begin();
	do 
	{
		std::vector<RowCol> vecRowCol;
		GetAround(itCur->m_nRow,vecRowCol);

		for (size_t i = 0; i < vecRowCol.size(); i++)
		{
			Bubble *pBubble = m_board[ vecRowCol[i].m_nRow ][ vecRowCol[i].m_nCol ];
			if (pBubble)
			{
				RowCol pos(vecRowCol[i].m_nRow,vecRowCol[i].m_nCol);
				ROWCOL_LIST::iterator itFind = std::find(LinkBubbleList.begin(),LinkBubbleList.end(),pos);

				//≤È’“«∑Ò“—æ≠‘⁄¡–±Ì÷–
				if (itFind == LinkBubbleList.end())
				{
					LinkBubbleList.push_back(vecRowCol[i]);
				}
			}
		}
		itCur++;
	} while (itCur != LinkBubbleList.end());

    //从所有泡泡中查出没有连接的泡泡
	ROWCOL_LIST NoLinkBubblelist;
	for (int i = 0; i < MAX_ROWS; i++)
	{
		for (int j = 0; j < MAX_COLS - i % 2; j++)
		{
			if (m_board[i][j] != NULL)
			{
				RowCol findRowCol(i,j);
				ROWCOL_LIST::iterator itFind = std::find(LinkBubbleList.begin(),findRowCol);
				if (itFind == LinkBubbleList.end())
				{
					NoLinkBubblelist.push_back(findRowCol);
				}
			}
		}
	}

	return NoLinkBubblelist;
}


主要还是算法方面...学习中.

原文链接:https://www.f2er.com/cocos2dx/345877.html

猜你在找的Cocos2d-x相关文章