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

cocos2dx 泡泡龙游戏开发思路探究,第1张

概述       泡泡龙游戏如上图,开发重点有以下几个; 1.建立泡泡矩阵 Bubble* m_board[11][11];  //建立一个面板有11行11列, 依次把泡泡存到响应索引里边去,没有泡泡的就设置NULL Bubble* m_wait[4];            //保存后边准备的4个泡泡 Bubble* m_curReady;       //准备发射的泡泡 2.发射泡泡 void G

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

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;}


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

总结

以上是内存溢出为你收集整理的cocos2dx 泡泡龙游戏开发思路探究全部内容,希望文章能够帮你解决cocos2dx 泡泡龙游戏开发思路探究所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1016937.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存