cocos2dx《单机斗地主》源码解剖之一 创建一副扑克牌(54张)

本项目基于cocos2dx引擎开发,根据网上搜索的斗地主文案(参见博客:斗地主规则)由本人独立完成,,废话少说(本人不善言辞,我也说不出来什么废话)下面我来做个项目总结。

1.创建一副扑克牌,写代码首先创建一张牌的类。如下所示:

class Poker : public Sprite{public:Poker();~Poker();static Poker* create(const char *pszFileName, const CCRect& rect);virtual void onEnter();virtual void onExit();virtual bool onTouchBegan(CCTouch *pTouch, CCEvent *pEvent);virtual void onTouchMoved(CCTouch *pTouch, CCEvent *pEvent);virtual void onTouchEnded(CCTouch *pTouch, CCEvent *pEvent);virtual void onTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);void showFront();//显示正面void showLast();//显示背面Poker* copy();//拷贝void setTouchPriority(int num);void SelectPkLuTou();//如果选择了牌就露出头void SelectPkSuoTou();//如果选择了牌就缩头private:CC_SYNTHESIZE(bool,m_isSelect,Select);//是否已选CC_SYNTHESIZE(GameScene*,m_gameMain,GameMain);CC_SYNTHESIZE(bool,m_isDianJi,DianJi);//是否能被点击CC_SYNTHESIZE(int,m_huaSe,HuaSe);//花色CC_SYNTHESIZE(int,m_num,Num);//牌值EventListenerTouchOneByOne* touchListener;};然后我们用这个类写了一个函数来生成一张牌,该函数如下(位于源码GameScene中):Poker* GameScene::selectPoker(int huaSe,int num){Poker* pk;if(huaSe != Gui)pk = Poker::create("poker.png",CCRect(num*pkWidth,huaSe*pkHeight,pkWidth,pkHeight));elsepk = Poker::create("poker.png",CCRect((num-XiaoGui)*pkWidth,huaSe*pkHeight,pkWidth,pkHeight));pk->setHuaSe(huaSe);pk->setNum(num);pk->setGameMain(this);return pk;}poker.png图片如下:

2.接下来我们就用来创建一副扑克牌了,请看代码(在GameScene文件中)GameScenebool GameScene::createPokers(){bool isRet = false;do {Size size = Director::sharedDirector()->getVisibleSize();Poker* pk;//创建52个除大鬼小鬼外的牌for (int i=0; i<4; ++i){for (int j=0; j<13; ++j){pk = selectPoker(i,j);pk->setPosition(ccp(size.width/2/*+j*20*/,size.height/2/*-i*20*/));pk->showLast();this->addChild(pk);this->m_arrPokers->addObject(pk);}}//创建小鬼pk = selectPoker(Gui,XiaoGui);pk->setPosition(ccp(size.width/2,size.height/2/*-4*20*/));pk->showLast();this->addChild(pk);this->m_arrPokers->addObject(pk);//创建大鬼pk = selectPoker(Gui,DaGui);pk->setPosition(ccp(size.width/2/*+20*/,size.height/2/*-4*20*/));pk->showLast();this->addChild(pk);this->m_arrPokers->addObject(pk);isRet = true;} while (0);return isRet;}源码下载:

生活中若没有朋友,就像生活中没有阳光一样

cocos2dx《单机斗地主》源码解剖之一 创建一副扑克牌(54张)

相关文章:

你感兴趣的文章:

标签云: