Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 大富翁游戏项目开发-第二十五部分 大富翁股市前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

当角色走到股市图标时,进入股市界面。每走完一个回合,增加一条股票数据,
股市界面上半部分显示股票信息,包括代码名称,当前价格,买入价格,涨跌百分比,角色持有的股票数量
下半部分显示股票价格走势,当点击一个股票时,显示相关股票的价格走势,共显示最新14条的价格走势。

每次点击购买,买入100股 。点击卖出,则卖出所持有的该股的所有股票。成交价格 等信息动态更新
点击返回,返回到游戏主界面,并更新角色资金值

1、首先添加股票类
包括代码名称,买入价格,涨跌百分比,持仓数量等定义以及相关的get 、set方法

  1. class Stock : public Sprite
  2. {
  3. ................
  4. private:
  5. <span style="white-space:pre"> </span>int stockCode; //股票代码
  6. String* stockName;//股票名称
  7. int nowPrice;//当前价格
  8. int makedealprice;//成交价格
  9. float percent;//涨跌百分比
  10. int storeNumber;//持仓数量
  11. };

2、VisibleRect类,主要是为了方便的获取屏幕的尺寸等信息
  1. class VisibleRect
  2. {
  3. public:
  4. static cocos2d::Rect getVisibleRect();
  5. ...................
  6. private:
  7. static void lazyInit();
  8. static cocos2d::Rect s_visibleRect;
  9. };

3、股票记录中的单元格类StockCellCard,包括单元格背景和显示文字的label。同彩票类card大体相同,不再解释了


4、在RicherPlayer的init方法中,给角色创建股票持有信息,主要是股票代码和持仓数量和买入价格。其他信息无关紧要
  1. bool RicherPlayer::init(char* name,int tag,bool enemy,int money,int strength)
  2. {
  3. ...........
  4. stockMap.insert(0,Stock::create(800100,LanguageString::getInstance()->getLanguageString(RICH_TECHNOLOGY),10,100));
  5. stockMap.insert(1,Stock::create(800200,20,200));
  6. stockMap.insert(2,Stock::create(800300,70,800));
  7. stockMap.insert(3,Stock::create(800400,400));
  8. stockMap.insert(4,Stock::create(800500,0));
  9. ..........
  10. }

5、当角色走到股票图标时RicherGameController 控制器,发送进入股市的消息
  1. if(passId == GameBaseScene::stock_tiledID)
  2. {
  3. String * str = String::createWithFormat("%d-%f-%f-%d-%d",MSG_STOCK_TAG,1.0f,_richerPlayer->getTag(),MOVEPASS);
  4. NotificationCenter::getInstance()->postNotification(MSG_STOCK,str);
  5. return;
  6. }

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()
.......

}

  1. //最多纪录14条纪录,超过的会被覆盖掉
  2. void GameBaseScene::updateStockVec()
  3. {
  4. float valule1 = rand()%800+10;
  5. float valule2 = rand()%800+10;
  6. float valule3 = rand()%800+10;
  7. float valule4 = rand()%800+10;
  8. float valule5 = rand()%800+10;
  9.  
  10. if(stock_pointvec1.size()>13)
  11. {
  12. for(int i=0;i<13;i++)
  13. {
  14. stock_pointvec1.at(i)=stock_pointvec1.at(i+1);
  15. stock_pointvec2.at(i)=stock_pointvec2.at(i+1);
  16. stock_pointvec3.at(i)=stock_pointvec3.at(i+1);
  17. stock_pointvec4.at(i)=stock_pointvec4.at(i+1);
  18. stock_pointvec5.at(i)=stock_pointvec5.at(i+1);
  19. }
  20. stock_pointvec1.at(13) =valule1;
  21. stock_pointvec2.at(13) =valule2;
  22. stock_pointvec3.at(13) =valule3;
  23. stock_pointvec4.at(13) =valule4;
  24. stock_pointvec5.at(13) =valule5;
  25. }else
  26. {
  27. stock_pointvec1.push_back(valule1);
  28. stock_pointvec2.push_back(valule2);
  29. stock_pointvec3.push_back(valule3);
  30. stock_pointvec4.push_back(valule4);
  31. stock_pointvec5.push_back(valule5);
  32. }
  33. }

7、当GameBaseScene收到进入股市界面的消息时,添加股市layer,显示股市
  1. case MSG_STOCK_TAG:
  2. {
  3. auto lineView = LineChart::createChart(player1);
  4. lineView->setPosition(Point(0,0));
  5. moveTag = messageVector.at(4)->intValue();
  6. lineView->moveTag = moveTag;
  7. addChild(lineView);
  8. ..............
  9. break;
  10. }

8、LineChart类是股票界面类

(1)initChart方法

  1. bool LineChart::initChart(RicherPlayer* player)
  2. {
  3. if ( !LayerColor::initWithColor(Color4B(0,255))) //黑色背景
  4. {
  5. return false;
  6. }
  7. richerPlayer = player; //角色
  8. playerStockMap = player->stockMap;//角色持有的股票容器
  9. initStockVector(playerStockMap);
  10. drawNode = DrawNode::create();//创建DrawNode ,准备画图
  11. this->addChild(drawNode);
  12. tv = TableView::create(this,Size(650,160));//创建TableView对象
  13. tv->setAnchorPoint(Point(0,0));
  14. tv->setPosition(10,VisibleRect::getVisibleRect().size.height * 1 /2);
  15. tv->setDelegate(this);
  16. addChild(tv);
  17. initMenu();//创建菜单包括买入,卖出,返回Menu
  18. selectedTag =0;
  19. float tableY = VisibleRect::getVisibleRect().size.height * 1/2;
  20. //选择股票时,箭头移动到相关股票
  21. arrowSprite_left->setPosition(600+arrowSprite_left->getContentSize().width,tableY +selectedTag*32);
  22. arrowSprite_right->setPosition(10,tableY + selectedTag*32);
  23. setData(getsock_pointVec(selectedTag));//设置股票数据
  24. drawpic();//画走势图
  25. return true;
  26. }

(2)initMenu方法创建菜单包括买入,卖出,返回Menu,以及箭头提示
  1. void LineChart::initMenu()
  2. {
  3. Menu* menu = Menu::create();
  4. menu->setPosition(CCPointZero);
  5. setMenu(menu);
  6. MenuItemImage* buyMenuItemButton = MenuItemImage::create("images/buy_normal.png","images/buy_pressed.png",this,menu_selector(LineChart::buttonCallback));
  7. buyMenuItemButton->setPosition(ccp(700,VisibleRect::getVisibleRect().size.height-110));
  8. buyMenuItemButton->setAnchorPoint(ccp(0,0));
  9. buyMenuItemButton->setTag(buy_button);
  10. menu->addChild(buyMenuItemButton);
  11. ............
  12. arrowSprite_left = Sprite::create("images/arrow_left.png");
  13. arrowSprite_left->setPosition(ccp(-500,-500));
  14. arrowSprite_left->setAnchorPoint(ccp(0,0));
  15. addChild(arrowSprite_left);
  16.  
  17. arrowSprite_right = Sprite::create("images/arrow_right.png");
  18. arrowSprite_right->setPosition(ccp(-500,-500));
  19. arrowSprite_right->setAnchorPoint(ccp(0,0));
  20. addChild(arrowSprite_right);
  21. }

(3)initStockVector()添加股票上半部分表格的标题,以及给股票容器添加各个股票信息
  1. void LineChart::initStockVector(Map<int,Stock*> stockMap)
  2. {
  3. float percent = 0;
  4. if(GameBaseScene::stock_pointvec1.size()>1)
  5. {
  6. 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;
  7. }
  8.  
  9. stockVector.pushBack(Stock::create(800100,GameBaseScene::stock_pointvec1.at(GameBaseScene::stock_pointvec1.size()-1),stockMap.at(0)->getMakedealprice(),percent,stockMap.at(0)->getStoreNumber()));
  10. ..................
  11. Label* code = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_CODE)->getCString(),"",20);
  12. code->setPosition(Point(20,410 ));
  13. code->setAnchorPoint(ccp(0,0));
  14. addChild(code);
  15.  
  16. Label* name = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_NAME)->getCString(),20);
  17. name->setPosition(Point(stockCellWidth+20,410 ));
  18. name->setAnchorPoint(ccp(0,0));
  19. addChild(name);
  20.  
  21. Label* nowprice = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_NOWPRICE)->getCString(),20);
  22. nowprice->setPosition(Point(stockCellWidth*2+20,410 ));
  23. nowprice->setAnchorPoint(ccp(0,0));
  24. addChild(nowprice);
  25.  
  26. Label* dealprice = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_DEALPRICE)->getCString(),20);
  27. dealprice->setPosition(Point(stockCellWidth*3+20,410 ));
  28. dealprice->setAnchorPoint(ccp(0,0));
  29. addChild(dealprice);
  30.  
  31. Label* percentLabel = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_PERCENT)->getCString(),20);
  32. percentLabel->setPosition(Point(stockCellWidth*4+20,410 ));
  33. percentLabel->setAnchorPoint(ccp(0,0));
  34. addChild(percentLabel);
  35.  
  36.  
  37. Label* store = Label::createWithSystemFont(LanguageString::getInstance()->getLanguageString(STOCK_STORE)->getCString(),20);
  38. store->setPosition(Point(540,410 ));
  39. store->setAnchorPoint(ccp(0,0));
  40. addChild(store);
  41.  
  42.  
  43. playerMoneyLabel = Label::createWithSystemFont(
  44. String::createWithFormat("%s %d",LanguageString::getInstance()->getLanguageString(PLAYER_MONEY)->getCString(),richerPlayer->getMoney())->getCString(),20);
  45. playerMoneyLabel->setPosition(Point(20,450 ));
  46. playerMoneyLabel->setAnchorPoint(ccp(0,0));
  47. addChild(playerMoneyLabel);
  48.  
  49. }

(4)buttonCallback(),点击买入 ,卖出,返回的回调方法
void LineChart::buttonCallback(CCObject* pSender)
{
.............

}

(5)TableView 相关实现方法

  1. //当点击股票时,移动箭头,更新数据,重画走势图
  2. void LineChart::tableCellTouched(cocos2d::extension::TableView *table,cocos2d::extension::TableViewCell *cell)
  3. {
  4. for(int i=0;i<30;i++)
  5. {
  6. this->removeChildByTag(100+i);
  7. }
  8.  
  9. int tag = cell->getTag();
  10. selectedTag =tag;
  11. log("******click id = %d",tag);
  12. float height = VisibleRect::getVisibleRect().size.height;
  13. float tableY = VisibleRect::getVisibleRect().size.height * 1/2;
  14. arrowSprite_left->setPosition(600+arrowSprite_left->getContentSize().width,tableY +tag*32);
  15. arrowSprite_right->setPosition(10,tableY + tag*32);
  16. log("all height is %f",height);
  17. log("all cellY is %f",tableY);
  18. setData(getsock_pointVec(tag));
  19. drawpic();
  20. }

  1. //创建tableview相关单元格,当股市上涨时,背景色为红色,下跌时为绿色
  2. TableViewCell* LineChart::tableCellAtIndex(cocos2d::extension::TableView *table,ssize_t idx)
  3. {
  4. TableViewCell *cell = table->dequeueCell();
  5. LabelTTF *label;
  6.  
  7. int colorTag = 0;
  8. if(stockVector.at(idx)->getPercent()>0)
  9. {
  10. colorTag = 1;
  11. }else
  12. {
  13. colorTag = -1;
  14. }
  15.  
  16. if (cell==NULL)
  17. {
  18. cell = TableViewCell::create();
  19. cell->setTag(idx);
  20. for(int i=0; i<6; i++)
  21. {
  22. switch(i)
  23. {
  24. case 0:
  25. {
  26. StockCellCard* card = StockCellCard::createCardSprite(String::createWithFormat("%d",stockVector.at(idx)->getCode()),stockCellWidth,stockCellHeight,stockCellWidth*i+10,colorTag);
  27. cell->addChild(card);
  28. break;
  29. }
  30. case 1:
  31. {
  32. StockCellCard* card = StockCellCard::createCardSprite(stockVector.at(idx)->getStockName(),colorTag);
  33. cell->addChild(card);
  34. break;
  35. }
  36. ....................................
  37. }
  38.  
  39. }
  40.  
  41. }
  42. return cell;
  43. }

//共5支股票
ssize_t LineChart::numberOfCellsInTableView(cocos2d::extension::TableView *table){
return 5;
}

(6)画走势图

  1. void LineChart::drawpic()
  2. {
  3. drawNode->clear();
  4. int maxValue = getMaxValue(pointvec);
  5. int maxValue2 = int((maxValue+100) / 100)* 100 ;
  6. maxValue1 = maxValue2 / 10;
  7. spaceRatio = 0.08f; //y轴间距系数
  8. leftRatioX = 0.1f; //x轴左侧间距系数
  9. int fontSize = 20;
  10. string fontName = StringUtils::format("Thonburi");
  11. Size layerSize = Size(VisibleRect::getVisibleRect().size.width,VisibleRect::getVisibleRect().size.height * 1 /2);
  12. layerHeight1 = 30;
  13. float layerHeight = layerHeight1;
  14. float layerWidth = layerSize.width;
  15. int count = layerSize.width /50;
  16. /***********************画xy轴标签*************************************/
  17.  
  18. for (int i = 0; i < 11; i++) {
  19. Point bPoint = Point(layerWidth* leftRatioX,layerHeight );
  20. Point ePoint = Point(layerWidth* leftRatioX+(count-2) * 50,layerHeight );
  21. Label* label = Label::createWithSystemFont(StringUtils::format("%d",maxValue1* i).c_str(),fontName.c_str(),fontSize);
  22. label->setPosition(Point(layerWidth* 0.05f,layerHeight ));
  23. label->setTag(100+i);
  24. addChild(label);
  25. drawNode->drawSegment(bPoint,ePoint,0.5,Color4F(100,100,200,200));
  26. layerHeight += layerSize.height * spaceRatio;
  27. }
  28.  
  29.  
  30. float layer_wd = layerSize.width * leftRatioX;
  31. for (int i = 0; i < count; i++) {
  32.  
  33. Point bPoint = Point(layer_wd,layerHeight1);
  34. Point ePoint = Point(layer_wd,layerSize.height * spaceRatio*10+layerHeight1);
  35. if(i%2 == 0)
  36. {
  37. drawNode->drawSegment(bPoint,200));
  38. }
  39.  
  40. auto labelX = Label::createWithSystemFont(StringUtils::format("%d",i).c_str(),"Thonburi",20);
  41. labelX->setPosition(Point(ePoint.x,0));
  42. labelX->setAnchorPoint(ccp(0,0));
  43. labelX->setTag(100+11+i);
  44. this->addChild(labelX);
  45. layer_wd += 50;
  46. }
  47.  
  48. drawLine(pointvec,Color4B(0,255,255),Color4B(255,255));
  49. }

//画走势线条
  1. void LineChart::drawLine(vector<Point> vec,Color4B lineColor,Color4B dotColor)
  2. {
  3. Size layerSize = Size(VisibleRect::getVisibleRect().size.width,VisibleRect::getVisibleRect().size.height * 1 /2);
  4. float layerWidth = layerSize.width;
  5. float tempWidth = layerSize.height * spaceRatio;
  6. float tempWidth2 = 0;
  7. float tempHeight1 = maxValue1 ;
  8.  
  9. double ratio = tempWidth/tempHeight1;
  10. /**********************开始画线**********************/
  11. std::vector<Point>::iterator beforePoint;
  12. std::vector<Point>::iterator currentPoint;
  13. beforePoint = vec.begin();
  14. for (currentPoint = vec.begin() + 1;currentPoint != vec.end() ; currentPoint++) {
  15. Point bPoint = *beforePoint;
  16. bPoint = Point(bPoint.x + layerWidth* leftRatioX,bPoint.y * ratio + layerHeight1 +tempWidth2);
  17. Point ePoint = *currentPoint;
  18. ePoint = Point(ePoint.x + layerWidth* leftRatioX,ePoint.y * ratio + layerHeight1 +tempWidth2);
  19. drawNode->drawSegment(bPoint,0.8,Color4F::RED);
  20. beforePoint = currentPoint;
  21. }
  22.  
  23. /**********************结束画线**********************/
  24. /********************开始画点**********************************************/
  25. beforePoint = vec.begin();
  26. DrawPrimitives::setDrawColor4B(dotColor.r,dotColor.g,dotColor.b,dotColor.a);
  27. Point bPoint = *beforePoint;
  28. bPoint = Point(bPoint.x +layerWidth* leftRatioX,bPoint.y * ratio + layerHeight1 +tempWidth2);
  29.  
  30. drawNode->drawDot(bPoint,5,Color4F::YELLOW);
  31.  
  32. int i = 2;
  33. for (currentPoint = vec.begin() + 1;currentPoint != vec.end() ; currentPoint++) {
  34. Point ePoint = *currentPoint;
  35. ePoint = Point(ePoint.x + layerWidth* leftRatioX,ePoint.y * ratio + layerHeight1 + tempWidth2);
  36. drawNode->drawDot(ePoint,Color4F::YELLOW);
  37. i++;
  38. }
  39. /********************结束画点*********************************************END**/
  40. }

//设置股票数据
  1. void LineChart::setData(vector<float> data)
  2. {
  3. pointvec.clear();
  4. vector<float>::iterator it;
  5. int i = 0;
  6. for (it = data.begin();it != data.end();it++) {
  7. float f = *it;
  8. pointvec.push_back(Point(50 * (i+1),f));
  9. i++;
  10. }
  11. }

  1. //获取最大值
  2. double LineChart::getMaxValue(std::vector<Point> vec)
  3. {
  4. double maxY =1;
  5. for (int i = 0; i < vec.size(); i++)
  6. {
  7. float num = vec.at(i).y;
  8. if (maxY < abs(num))
  9. {
  10. maxY = abs(num);
  11. }
  12. }
  13. return maxY;
  14. }

这就是大体代码,详细内容,请参考代码,如下效果




点击下载代码

未完待续...................

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