首页 > 代码库 > Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市
Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市
当角色走到股市图标时,进入股市界面。每走完一个回合,增加一条股票数据,
股市界面上半部分显示股票信息,包括代码,名称,当前价格,买入价格,涨跌百分比,角色持有的股票数量
下半部分显示股票价格走势,当点击一个股票时,显示相关股票的价格走势,共显示最新14条的价格走势。
每次点击购买,买入100股 。点击卖出,则卖出所持有的该股的所有股票。成交价格 等信息动态更新
点击返回,返回到游戏主界面,并更新角色资金值
1、首先添加股票类
包括代码,名称,买入价格,涨跌百分比,持仓数量等定义以及相关的get 、set方法
class Stock : public Sprite { ................ private: <span style="white-space:pre"> </span>int stockCode; //股票代码 String* stockName;//股票名称 int nowPrice;//当前价格 int makedealprice;//成交价格 float percent;//涨跌百分比 int storeNumber;//持仓数量 };
2、VisibleRect类,主要是为了方便的获取屏幕的尺寸等信息
class VisibleRect { public: static cocos2d::Rect getVisibleRect(); ................... private: static void lazyInit(); static cocos2d::Rect s_visibleRect; };
3、股票记录中的单元格类StockCellCard,包括单元格背景和显示文字的label。同彩票类card大体相同,不再解释了
4、在RicherPlayer的init方法中,给角色创建股票持有信息,主要是股票代码和持仓数量和买入价格。其他信息无关紧要
bool RicherPlayer::init(char* name, int tag,bool enemy,int money,int strength) { ........... stockMap.insert(0,Stock::create(800100,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,10,0,100)); stockMap.insert(1,Stock::create(800200,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,20,0,200)); stockMap.insert(2,Stock::create(800300,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,70,0,800)); stockMap.insert(3,Stock::create(800400,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,10,0,400)); stockMap.insert(4,Stock::create(800500,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),0,0,0,0)); .......... }
5、当角色走到股票图标时RicherGameController 控制器,发送进入股市的消息
if(passId == GameBaseScene::stock_tiledID) { String * str = String::createWithFormat("%d-%f-%f-%d-%d",MSG_STOCK_TAG,1.0f,1.0f,_richerPlayer->getTag(),MOVEPASS); NotificationCenter::getInstance()->postNotification(MSG_STOCK,str); return; }
6、GameBaseScene类定义了股票容器类,存放所以股票相关信息
vector<float> GameBaseScene::stock_pointvec1;
vector<float> GameBaseScene::stock_pointvec2;
vector<float> GameBaseScene::stock_pointvec3;
vector<float> GameBaseScene::stock_pointvec4;
vector<float> GameBaseScene::stock_pointvec5;
在更新回合计数时,调用updateStockVec()方法,添加股票一条新纪录
void GameBaseScene::refreshRoundDisplay()
{
..........
updateStockVec()
.......
}
//最多纪录14条纪录,超过的会被覆盖掉 void GameBaseScene::updateStockVec() { float valule1 = rand()%800+10; float valule2 = rand()%800+10; float valule3 = rand()%800+10; float valule4 = rand()%800+10; float valule5 = rand()%800+10; if(stock_pointvec1.size()>13) { for(int i=0;i<13;i++) { stock_pointvec1.at(i)=stock_pointvec1.at(i+1); stock_pointvec2.at(i)=stock_pointvec2.at(i+1); stock_pointvec3.at(i)=stock_pointvec3.at(i+1); stock_pointvec4.at(i)=stock_pointvec4.at(i+1); stock_pointvec5.at(i)=stock_pointvec5.at(i+1); } stock_pointvec1.at(13) =valule1; stock_pointvec2.at(13) =valule2; stock_pointvec3.at(13) =valule3; stock_pointvec4.at(13) =valule4; stock_pointvec5.at(13) =valule5; }else { stock_pointvec1.push_back(valule1); stock_pointvec2.push_back(valule2); stock_pointvec3.push_back(valule3); stock_pointvec4.push_back(valule4); stock_pointvec5.push_back(valule5); } }
7、当GameBaseScene收到进入股市界面的消息时,添加股市layer,显示股市
case MSG_STOCK_TAG: { auto lineView = LineChart::createChart(player1); lineView->setPosition(Point(0, 0)); moveTag = messageVector.at(4)->intValue(); lineView->moveTag = moveTag; addChild(lineView); .............. break; }
8、LineChart类是股票界面类
(1)initChart方法
bool LineChart::initChart(RicherPlayer* player) { if ( !LayerColor::initWithColor(Color4B(0,0,0,255))) //黑色背景 { return false; } richerPlayer = player; //角色 playerStockMap = player->stockMap;//角色持有的股票容器 initStockVector(playerStockMap); drawNode = DrawNode::create();//创建DrawNode ,准备画图 this->addChild(drawNode); tv = TableView::create(this, Size(650, 160));//创建TableView对象 tv->setAnchorPoint(Point(0, 0)); tv->setPosition(10, VisibleRect::getVisibleRect().size.height * 1 /2); tv->setDelegate(this); addChild(tv); initMenu();//创建菜单,包括买入,卖出,返回Menu selectedTag =0; float tableY = VisibleRect::getVisibleRect().size.height * 1/2; //选择股票时,箭头移动到相关股票 arrowSprite_left->setPosition(600+arrowSprite_left->getContentSize().width,tableY +selectedTag*32); arrowSprite_right->setPosition(10,tableY + selectedTag*32); setData(getsock_pointVec(selectedTag));//设置股票数据 drawpic();//画走势图 return true; }
(2)initMenu方法创建菜单,包括买入,卖出,返回Menu,以及箭头提示
void LineChart::initMenu() { Menu* menu = Menu::create(); menu->setPosition(CCPointZero); setMenu(menu); MenuItemImage* buyMenuItemButton = MenuItemImage::create("images/buy_normal.png", "images/buy_pressed.png", this, menu_selector(LineChart::buttonCallback)); buyMenuItemButton->setPosition(ccp(700,VisibleRect::getVisibleRect().size.height-110)); buyMenuItemButton->setAnchorPoint(ccp(0,0)); buyMenuItemButton->setTag(buy_button); menu->addChild(buyMenuItemButton); ............ arrowSprite_left = Sprite::create("images/arrow_left.png"); arrowSprite_left->setPosition(ccp(-500,-500)); arrowSprite_left->setAnchorPoint(ccp(0,0)); addChild(arrowSprite_left); arrowSprite_right = Sprite::create("images/arrow_right.png"); arrowSprite_right->setPosition(ccp(-500,-500)); arrowSprite_right->setAnchorPoint(ccp(0,0)); addChild(arrowSprite_right); }
(3)initStockVector()添加股票上半部分表格的标题,以及给股票容器添加各个股票信息
void LineChart::initStockVector(Map<int,Stock*> stockMap) { float percent = 0; if(GameBaseScene::stock_pointvec1.size()>1) { percent = (GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-1) - GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-2))/GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-2)*100; } stockVector.pushBack(Stock::create(800100,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-1),stockMap.at(0)->getMakedealprice(),percent,stockMap.at(0)->getStoreNumber())); .................. Label* code = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_CODE)->getCString(),"", 20); code->setPosition(Point(20, 410 )); code->setAnchorPoint(ccp(0,0)); addChild(code); Label* name = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_NAME)->getCString(),"", 20); name->setPosition(Point(stockCellWidth+20, 410 )); name->setAnchorPoint(ccp(0,0)); addChild(name); Label* nowprice = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_NOWPRICE)->getCString(),"", 20); nowprice->setPosition(Point(stockCellWidth*2+20, 410 )); nowprice->setAnchorPoint(ccp(0,0)); addChild(nowprice); Label* dealprice = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_DEALPRICE)->getCString(),"", 20); dealprice->setPosition(Point(stockCellWidth*3+20, 410 )); dealprice->setAnchorPoint(ccp(0,0)); addChild(dealprice); Label* percentLabel = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_PERCENT)->getCString(),"", 20); percentLabel->setPosition(Point(stockCellWidth*4+20, 410 )); percentLabel->setAnchorPoint(ccp(0,0)); addChild(percentLabel); Label* store = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_STORE)->getCString(),"", 20); store->setPosition(Point(540, 410 )); store->setAnchorPoint(ccp(0,0)); addChild(store); playerMoneyLabel = Label::createWithSystemFont( String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(PLAYER_MONEY)->getCString(),richerPlayer->getMoney())->getCString(), "", 20); playerMoneyLabel->setPosition(Point(20, 450 )); playerMoneyLabel->setAnchorPoint(ccp(0,0)); addChild(playerMoneyLabel); }
(4)buttonCallback(),点击买入 ,卖出,返回的回调方法
void LineChart::buttonCallback(CCObject* pSender)
{
.............
}
(5)TableView 相关实现方法
//当点击股票时,移动箭头,更新数据,重画走势图 void LineChart::tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell) { for(int i=0;i<30;i++) { this->removeChildByTag(100+i); } int tag = cell->getTag(); selectedTag =tag; log("******click id = %d",tag); float height = VisibleRect::getVisibleRect().size.height; float tableY = VisibleRect::getVisibleRect().size.height * 1/2; arrowSprite_left->setPosition(600+arrowSprite_left->getContentSize().width,tableY +tag*32); arrowSprite_right->setPosition(10,tableY + tag*32); log("all height is %f",height); log("all cellY is %f",tableY); setData(getsock_pointVec(tag)); drawpic(); }
//创建tableview相关单元格,当股市上涨时,背景色为红色,下跌时为绿色 TableViewCell* LineChart::tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx) { TableViewCell *cell = table->dequeueCell(); LabelTTF *label; int colorTag = 0; if(stockVector.at(idx)->getPercent()>0) { colorTag = 1; }else { colorTag = -1; } if (cell==NULL) { cell = TableViewCell::create(); cell->setTag(idx); for(int i=0; i<6; i++) { switch(i) { case 0: { StockCellCard* card = StockCellCard::createCardSprite(String::createWithFormat("%d",stockVector.at(idx)->getCode()), stockCellWidth, stockCellHeight, stockCellWidth*i+10, 0,colorTag); cell->addChild(card); break; } case 1: { StockCellCard* card = StockCellCard::createCardSprite(stockVector.at(idx)->getStockName(), stockCellWidth, stockCellHeight, stockCellWidth*i+10, 0,colorTag); cell->addChild(card); break; } .................................... } } } return cell; }
//共5支股票
ssize_t LineChart::numberOfCellsInTableView(cocos2d::extension::TableView *table){
return 5;
}
(6)画走势图
void LineChart::drawpic() { drawNode->clear(); int maxValue = http://www.mamicode.com/getMaxValue(pointvec);>
//画走势线条void LineChart::drawLine(vector<Point> vec,Color4B lineColor,Color4B dotColor) { Size layerSize = Size(VisibleRect::getVisibleRect().size.width, VisibleRect::getVisibleRect().size.height * 1 /2); float layerWidth = layerSize.width; float tempWidth = layerSize.height * spaceRatio; float tempWidth2 = 0; float tempHeight1 = maxValue1 ; double ratio = tempWidth/tempHeight1; /**********************开始画线**********************/ std::vector<Point>::iterator beforePoint; std::vector<Point>::iterator currentPoint; beforePoint = vec.begin(); for (currentPoint = vec.begin() + 1;currentPoint != vec.end() ; currentPoint++) { Point bPoint = *beforePoint; bPoint = Point(bPoint.x + layerWidth* leftRatioX, bPoint.y * ratio + layerHeight1 +tempWidth2); Point ePoint = *currentPoint; ePoint = Point(ePoint.x + layerWidth* leftRatioX, ePoint.y * ratio + layerHeight1 +tempWidth2); drawNode->drawSegment(bPoint, ePoint, 0.8,Color4F::RED); beforePoint = currentPoint; } /**********************结束画线**********************/ /********************开始画点**********************************************/ beforePoint = vec.begin(); DrawPrimitives::setDrawColor4B(dotColor.r, dotColor.g, dotColor.b, dotColor.a); Point bPoint = *beforePoint; bPoint = Point(bPoint.x +layerWidth* leftRatioX, bPoint.y * ratio + layerHeight1 +tempWidth2); drawNode->drawDot(bPoint, 5, Color4F::YELLOW); int i = 2; for (currentPoint = vec.begin() + 1;currentPoint != vec.end() ; currentPoint++) { Point ePoint = *currentPoint; ePoint = Point(ePoint.x + layerWidth* leftRatioX, ePoint.y * ratio + layerHeight1 + tempWidth2); drawNode->drawDot(ePoint, 5, Color4F::YELLOW); i++; } /********************结束画点*********************************************END**/ }
//设置股票数据void LineChart::setData(vector<float> data) { pointvec.clear(); vector<float>::iterator it; int i = 0; for (it = data.begin();it != data.end();it++) { float f = *it; pointvec.push_back(Point(50 * (i+1), f)); i++; } }//获取最大值 double LineChart::getMaxValue(std::vector<Point> vec) { double maxY =1; for (int i = 0; i < vec.size(); i++) { float num = vec.at(i).y; if (maxY < abs(num)) { maxY = abs(num); } } return maxY; }
这就是大体代码,详细内容,请参考代码,如下效果图
点击下载代码
未完待续...................
Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。