cocos2d-x游戏开发系列教程-中国象棋04-摆棋

前情回顾

在之前的学习中,我们已经了解到,下棋主界面是由CCMainMenu类实现的,在它的init函数中,初始化了

主界面需要的各种数据,包括:创建控件,初始化32个棋子,初始化执行变量等等,在这个博文中,我们

主要来学习,32个棋子是如何被初始化并显示在界面上的。这个显示和initCoordinate以及

了解32个棋子显示在界面的目的,是为了让大家更好理解32个棋子的数据结构,这些数据结构在当棋子走动

时,需要改动,所以必须要理解。

initCoordinate

void CCMainMenu::initCoordinate()
{
	/*std::string str = "../coordinate.txt";
	std::ofstream f_output;
	f_output.open(str.c_str(), std::ios::app);*/
	float xSpan  = 48.0f*0.667f;
	float ySpan  = 32.0f*0.6f;
	float xStart = 1;
	float yStart = xSpan;
	for (int i = 0, x = 9; i < 10; ++i, --x)
	{
		for (int j = 0, y = 1; j < 9; ++j, ++y)
		{
			g_chess_coord[i][j] = ccp(xStart + xSpan*y, yStart*x + ySpan);  // 计算象棋坐标点的坐标,把象棋坐标转化成view坐标
			/*f_output<< "[" << i << "][" << j <<"]: (" << xStart + xSpan*y << ", " << yStart*x + ySpan << ")" << " ";
			if(j == 4 || j == 8)
			{
				f_output<<std::endl;
			}*/
		}
		/*f_output<<std::endl;*/
	}
	/*f_output.close();*/
}

这个函数功能是将象棋的坐标转化成屏幕坐标,并保存在g_chess_coord中。有了这些坐标,为后面的摆棋工作打下基础。

initListImage

initListImage函数,初始化了32个棋子的位置,代码的逻辑很简单,就是调用32次getListSprite,获得32个棋子精灵并保存在m_pChess中

然后调用initChessPosition去设置32个棋子的位置,代码在MainMenu.cpp中

void CCMainMenu::initListImage()
{
	// red
	m_pChess[1][0] = this->getListSprite(CHESS_RK);
	this->addChild(m_pChess[1][0], 0);

	m_pChess[1][1] = this->getListSprite(CHESS_RA);
	this->addChild(m_pChess[1][1], 0);

	m_pChess[1][2] = this->getListSprite(CHESS_RA);
	this->addChild(m_pChess[1][2], 0);

	m_pChess[1][3] = this->getListSprite(CHESS_RB);
	this->addChild(m_pChess[1][3], 0);

	m_pChess[1][4] = this->getListSprite(CHESS_RB);
	this->addChild(m_pChess[1][4], 0);

	m_pChess[1][5] = this->getListSprite(CHESS_RN);
	this->addChild(m_pChess[1][5], 0);

	m_pChess[1][6] = this->getListSprite(CHESS_RN);
	this->addChild(m_pChess[1][6], 0);

	m_pChess[1][7] = this->getListSprite(CHESS_RR);
	this->addChild(m_pChess[1][7], 0);

	m_pChess[1][8] = this->getListSprite(CHESS_RR);
	this->addChild(m_pChess[1][8], 0);

	m_pChess[1][9] = this->getListSprite(CHESS_RC);
	this->addChild(m_pChess[1][9], 0);
	
	m_pChess[1][10] = this->getListSprite(CHESS_RC);
	this->addChild(m_pChess[1][10], 0);

	m_pChess[1][11] = this->getListSprite(CHESS_RP);
	this->addChild(m_pChess[1][11], 0);

	m_pChess[1][12] = this->getListSprite(CHESS_RP);
	this->addChild(m_pChess[1][12], 0);

	m_pChess[1][13] = this->getListSprite(CHESS_RP);
	this->addChild(m_pChess[1][13], 0);

	m_pChess[1][14] = this->getListSprite(CHESS_RP);
	this->addChild(m_pChess[1][14], 0);

	m_pChess[1][15] = this->getListSprite(CHESS_RP);
	this->addChild(m_pChess[1][15], 0);

	//black
	m_pChess[0][0] = this->getListSprite(CHESS_BK);
	this->addChild(m_pChess[0][0], 0);

	m_pChess[0][1] = this->getListSprite(CHESS_BA);
	this->addChild(m_pChess[0][1], 0);

	m_pChess[0][2] = this->getListSprite(CHESS_BA);
	this->addChild(m_pChess[0][2], 0);

	m_pChess[0][3] = this->getListSprite(CHESS_BB);
	this->addChild(m_pChess[0][3], 0);

	m_pChess[0][4] = this->getListSprite(CHESS_BB);
	this->addChild(m_pChess[0][4], 0);

	m_pChess[0][5] = this->getListSprite(CHESS_BN);
	this->addChild(m_pChess[0][5], 0);

	m_pChess[0][6] = this->getListSprite(CHESS_BN);
	this->addChild(m_pChess[0][6], 0);

	m_pChess[0][7] = this->getListSprite(CHESS_BR);
	this->addChild(m_pChess[0][7], 0);

	m_pChess[0][8] = this->getListSprite(CHESS_BR);
	this->addChild(m_pChess[0][8], 0);

	m_pChess[0][9] = this->getListSprite(CHESS_BC);
	this->addChild(m_pChess[0][9], 0);

	m_pChess[0][10] = this->getListSprite(CHESS_BC);
	this->addChild(m_pChess[0][10], 0);

	m_pChess[0][11] = this->getListSprite(CHESS_BP);
	this->addChild(m_pChess[0][11], 0);

	m_pChess[0][12] = this->getListSprite(CHESS_BP);
	this->addChild(m_pChess[0][12], 0);

	m_pChess[0][13] = this->getListSprite(CHESS_BP);
	this->addChild(m_pChess[0][13], 0);

	m_pChess[0][14] = this->getListSprite(CHESS_BP);
	this->addChild(m_pChess[0][14], 0);

	m_pChess[0][15] = this->getListSprite(CHESS_BP);
	this->addChild(m_pChess[0][15], 0);

	this->initChessPosition();
}

getListSprite

这个函数负责创建棋子精灵,根据不同的棋子类型,使用不同的图片创建精灵,在你们自己的环境中,尤其注意图片路径

图片路劲不对,那什么都不显示,所以如果有错,先考虑是不是图片路径不对了。

CCSprite* CCMainMenu::getListSprite(CHESS_TYPE nType)
{
	CCSprite* pSprite;
	switch(nType)
	{
	case CHESS_RK:
		pSprite = CCSprite::create(RES_PATH"rk.png");
		break;
	case CHESS_RA:
		pSprite = CCSprite::create(RES_PATH"ra.png");
		break;
	case CHESS_RB: 
		pSprite = CCSprite::create(RES_PATH"rb.png");
		break;
	case CHESS_RN: 
		pSprite = CCSprite::create(RES_PATH"rn.png");
		break;
	case CHESS_RR: 
		pSprite = CCSprite::create(RES_PATH"rr.png");
		break;
	case CHESS_RC:
		pSprite = CCSprite::create(RES_PATH"rc.png");
		break;
	case CHESS_RP: 
		pSprite = CCSprite::create(RES_PATH"rp.png");
		break;
	case CHESS_BK:
		pSprite = CCSprite::create(RES_PATH"bk.png");
		break;
	case CHESS_BA:
		pSprite = CCSprite::create(RES_PATH"ba.png");
		break;
	case CHESS_BB: 
		pSprite = CCSprite::create(RES_PATH"bb.png");
		break;
	case CHESS_BN: 
		pSprite = CCSprite::create(RES_PATH"bn.png");
		break;
	case CHESS_BR: 
		pSprite = CCSprite::create(RES_PATH"br.png");
		break;
	case CHESS_BC:
		pSprite = CCSprite::create(RES_PATH"bc.png");
		break;
	case CHESS_BP:
		pSprite = CCSprite::create(RES_PATH"bp.png");
		break;
	}

	pSprite->setScaleX(0.5f);
	pSprite->setScaleY(0.5f);
	return pSprite;
}

initChessPosition

initListImage最后调用的是initChessPosition,为32个棋子指定位置。这里位置信息用到了g_chess_coord,这个坐标信息是之前函数

void CCMainMenu::initChessPosition()
{
	m_pChess[1][0]->setPosition(g_chess_coord[9][4]);
	m_pChess[1][1]->setPosition(g_chess_coord[9][3]);
	m_pChess[1][2]->setPosition(g_chess_coord[9][5]);
	m_pChess[1][3]->setPosition(g_chess_coord[9][2]);
	m_pChess[1][4]->setPosition(g_chess_coord[9][6]);
	m_pChess[1][5]->setPosition(g_chess_coord[9][1]);
	m_pChess[1][6]->setPosition(g_chess_coord[9][7]);
	m_pChess[1][7]->setPosition(g_chess_coord[9][0]);
	m_pChess[1][8]->setPosition(g_chess_coord[9][8]);
	m_pChess[1][9]->setPosition(g_chess_coord[7][1]);
	m_pChess[1][10]->setPosition(g_chess_coord[7][7]);
	m_pChess[1][11]->setPosition(g_chess_coord[6][0]);
	m_pChess[1][12]->setPosition(g_chess_coord[6][2]);
	m_pChess[1][13]->setPosition(g_chess_coord[6][4]);
	m_pChess[1][14]->setPosition(g_chess_coord[6][6]);
	m_pChess[1][15]->setPosition(g_chess_coord[6][8]);
	//black
	m_pChess[0][0]->setPosition(g_chess_coord[0][4]);
	m_pChess[0][1]->setPosition(g_chess_coord[0][3]);
	m_pChess[0][2]->setPosition(g_chess_coord[0][5]);
	m_pChess[0][3]->setPosition(g_chess_coord[0][2]);
	m_pChess[0][4]->setPosition(g_chess_coord[0][6]);
	m_pChess[0][5]->setPosition(g_chess_coord[0][1]);
	m_pChess[0][6]->setPosition(g_chess_coord[0][7]);
	m_pChess[0][7]->setPosition(g_chess_coord[0][0]);
	m_pChess[0][8]->setPosition(g_chess_coord[0][8]);
	m_pChess[0][9]->setPosition(g_chess_coord[2][1]);
	m_pChess[0][10]->setPosition(g_chess_coord[2][7]);
	m_pChess[0][11]->setPosition(g_chess_coord[3][0]);
	m_pChess[0][12]->setPosition(g_chess_coord[3][2]);
	m_pChess[0][13]->setPosition(g_chess_coord[3][4]);
	m_pChess[0][14]->setPosition(g_chess_coord[3][6]);
	m_pChess[0][15]->setPosition(g_chess_coord[3][8]);
}

好,当程序完全执行完这些代码后,就完成了32个棋子的初始化。

下一遍博文我们讲讲述这个象棋是怎么走起来

版权声明:本文为niulanshan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/niulanshan/p/6175071.html