├── README.md └── brick ├── Arrow └── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── ArrowPath.cpp │ ├── GameScene.cpp │ ├── GameScene.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ ├── Player.cpp │ └── Player.h ├── Classes ├── AppDelegate.cpp ├── AppDelegate.h ├── GameScene.cpp ├── GameScene.h ├── HelloWorldScene.cpp ├── HelloWorldScene.h ├── MainScene.cpp └── MainScene.h ├── FlippyBird └── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── GameScene.cpp │ ├── GameScene.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ ├── MenuScene.cpp │ └── MenuScene.h ├── Plane └── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── EnemyPlane.cpp │ ├── EnemyPlane.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ ├── MainScene.cpp │ ├── MainScene.h │ ├── Plane.cpp │ └── Plane.h ├── carrot └── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── GameManager.cpp │ ├── GameManager.h │ ├── GameScene.cpp │ ├── GameScene.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ ├── Monster.cpp │ ├── Monster.h │ ├── Tower.cpp │ └── Tower.h ├── doudizhu ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── DeckScene.cpp │ ├── DeckScene.h │ ├── GameManager.cpp │ ├── GameManager.h │ ├── GameRoomList.cpp │ ├── GameRoomList.h │ ├── GameRoomScene.cpp │ ├── GameRoomScene.h │ ├── LoginScene.cpp │ ├── LoginScene.h │ ├── MainScene.cpp │ ├── MainScene.h │ ├── Player.cpp │ ├── Player.h │ ├── PokerCard.cpp │ └── PokerCard.h └── server │ ├── a.c │ ├── head.cpp │ ├── head.h │ ├── main │ ├── main.cpp │ └── tags └── sanxiao └── Classes ├── AppDelegate.cpp ├── AppDelegate.h ├── FruitSprite.cpp ├── FruitSprite.h ├── GameScene.cpp ├── GameScene.h ├── HelloWorldScene.cpp └── HelloWorldScene.h /README.md: -------------------------------------------------------------------------------- 1 | # cocos2dx 2 | cocos2dx编写的常见游戏 3 | 目前有: 4 | 1.微信打飞机 Plane 5 | 2.塔防 carrot 6 | 3.射击动作 Arrow 7 | 4.flappybird 8 | 5.三消类 sanxiao 9 | 5.斗地主 doudizhu 还在写 10 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "GameScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); 7 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 8 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 9 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 10 | 11 | AppDelegate::AppDelegate() { 12 | 13 | } 14 | 15 | AppDelegate::~AppDelegate() 16 | { 17 | } 18 | 19 | //if you want a different context,just modify the value of glContextAttrs 20 | //it will takes effect on all platforms 21 | void AppDelegate::initGLContextAttrs() 22 | { 23 | //set OpenGL context attributions,now can only set six attributions: 24 | //red,green,blue,alpha,depth,stencil 25 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 26 | 27 | GLView::setGLContextAttrs(glContextAttrs); 28 | } 29 | 30 | // If you want to use packages manager to install more packages, 31 | // don't modify or remove this function 32 | static int register_all_packages() 33 | { 34 | return 0; //flag for packages manager 35 | } 36 | 37 | bool AppDelegate::applicationDidFinishLaunching() { 38 | // initialize director 39 | auto director = Director::getInstance(); 40 | auto glview = director->getOpenGLView(); 41 | if(!glview) { 42 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 43 | glview = GLViewImpl::createWithRect("Arrow", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 44 | #else 45 | glview = GLViewImpl::create("Arrow"); 46 | #endif 47 | director->setOpenGLView(glview); 48 | } 49 | 50 | glview->setFrameSize(960, 640); 51 | // turn on display FPS 52 | director->setDisplayStats(true); 53 | 54 | // set FPS. the default value is 1.0/60 if you don't call this 55 | director->setAnimationInterval(1.0 / 60); 56 | 57 | // Set the design resolution 58 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT); 59 | director->setContentScaleFactor(480.0f / 320.0f); 60 | Size frameSize = glview->getFrameSize(); 61 | // if the frame's height is larger than the height of medium size. 62 | if (frameSize.height > mediumResolutionSize.height) 63 | { 64 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 65 | } 66 | // if the frame's height is larger than the height of small size. 67 | else if (frameSize.height > smallResolutionSize.height) 68 | { 69 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 70 | } 71 | // if the frame's height is smaller than the height of medium size. 72 | else 73 | { 74 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 75 | } 76 | 77 | register_all_packages(); 78 | 79 | // create a scene. it's an autorelease object 80 | auto scene = GameScene::createScene(); 81 | 82 | // run 83 | director->runWithScene(scene); 84 | 85 | return true; 86 | } 87 | 88 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 89 | void AppDelegate::applicationDidEnterBackground() { 90 | Director::getInstance()->stopAnimation(); 91 | 92 | // if you use SimpleAudioEngine, it must be pause 93 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 94 | } 95 | 96 | // this function will be called when the app is active again 97 | void AppDelegate::applicationWillEnterForeground() { 98 | Director::getInstance()->startAnimation(); 99 | 100 | // if you use SimpleAudioEngine, it must resume here 101 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 102 | } 103 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/ArrowPath.cpp: -------------------------------------------------------------------------------- 1 | #include "Player.h" 2 | 3 | 4 | static inline float bezier1(float a, float b, float c, float t) { 5 | return (powf(1 - t, 2) * a + 2.0f * t * (1 - t) * b + powf(t, 2) * c); 6 | } 7 | 8 | bool ArrowPath::initWithDuration(float t, Point ctlPoint, Point endPoint) { 9 | if (ActionInterval::initWithDuration(t)) { 10 | this->ctlPoint = ctlPoint; 11 | this->endPoint = endPoint; 12 | 13 | return true; 14 | } 15 | return false; 16 | } 17 | ArrowPath* ArrowPath::create(float time, Point ctlPoint, Point endPoint) { 18 | ArrowPath *path = new ArrowPath(); 19 | path->initWithDuration(time, ctlPoint, endPoint); 20 | path->autorelease(); 21 | 22 | return path; 23 | } 24 | 25 | void ArrowPath::update(float time) { 26 | if (_target) { 27 | float x = bezier1(0, ctlPoint.x, endPoint.x, time); 28 | float y = bezier1(0, ctlPoint.y, endPoint.y, time); 29 | 30 | Vec2 curPos = _target->getPosition(); 31 | Vec2 diff = _previousPosition - curPos; 32 | _startPosition = _startPosition + diff; 33 | 34 | Vec2 newPos = _startPosition + Vec2(x, y); 35 | _target->setPosition(newPos); 36 | _previousPosition = newPos; 37 | 38 | if (time != 0) { 39 | Vec2 vec = curPos - newPos; 40 | float radians = vec.getAngle(); 41 | float degree = CC_RADIANS_TO_DEGREES(-1 * radians); 42 | _target->setRotation(degree - 180); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/GameScene.cpp: -------------------------------------------------------------------------------- 1 | #include "GameScene.h" 2 | 3 | bool GameScene::init() { 4 | if (!Layer::init()) { 5 | return false; 6 | } 7 | visible = Director::getInstance()->getVisibleSize(); 8 | origin = Director::getInstance()->getVisibleOrigin(); 9 | 10 | auto bgSprite = Sprite::create("bg.png"); 11 | bgSprite->setAnchorPoint(Vec2(0, 0)); 12 | bgSprite->setPosition(Vec2(origin.x, origin.y)); 13 | addChild(bgSprite, -1); 14 | 15 | auto map = TMXTiledMap::create("bg.tmx"); 16 | map->setAnchorPoint(Vec2(0, 0)); 17 | map->setPosition(Vec2(origin.x, origin.y)); 18 | addChild(map, -1); 19 | 20 | player = Player::create(Vec2(origin.x + 40, origin.y + 70)); 21 | addChild(player, 1); 22 | 23 | log("SpriteFrameCache"); 24 | auto spriteCache = SpriteFrameCache::getInstance(); 25 | spriteCache->addSpriteFramesWithFile("phoenix.plist", "phoenix.png"); 26 | 27 | Vector vec; 28 | for (int i = 0; i < 16; i++) { 29 | 30 | log(i); 31 | char tmp[16]; 32 | sprintf(tmp, "f-%d.png", i); 33 | auto frame = spriteCache->getSpriteFrameByName(tmp); 34 | vec.pushBack(frame); 35 | if (i % 4 == 3) { 36 | auto animation = Animation::createWithSpriteFrames(vec, 0.3f, -1); 37 | auto animate = Animate::create(animation); 38 | sprintf(tmp, "f-%d.png", i - 3); 39 | auto sprite = Sprite::createWithSpriteFrameName(tmp); 40 | sprite->setAnchorPoint(Vec2(0, 0)); 41 | sprite->setPosition(Vec2(86 * (i / 4), 200)); 42 | sprite->runAction(animate); 43 | addChild(sprite); 44 | vec.clear(); 45 | } 46 | } 47 | 48 | TTFConfig config("fonts/Marker Felt.ttf", 30); 49 | auto label = Label::createWithTTF(config, "createLabelByTTF", TextHAlignment::CENTER); 50 | label->setString("Example!"); 51 | label->enableGlow(Color4B::ORANGE); 52 | label->setAnchorPoint(Vec2(0.5, 0.5)); 53 | label->setPosition(150, 100); 54 | addChild(label); 55 | 56 | 57 | 58 | auto listen = EventListenerTouchOneByOne::create(); 59 | listen->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); 60 | listen->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved, this); 61 | listen->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this); 62 | 63 | auto dispatch = Director::getInstance()->getEventDispatcher(); 64 | dispatch->addEventListenerWithSceneGraphPriority(listen, this); 65 | return true; 66 | } 67 | 68 | bool GameScene::onTouchBegan(Touch *touch, Event *unused_event) { 69 | onTouchMoved(touch, unused_event); 70 | return true; 71 | } 72 | 73 | void GameScene::onTouchMoved(Touch *touch, Event *unused_event) { 74 | curPos = touch->getLocation(); 75 | if (!curPos.equals(prePos)) { 76 | player->rotateArrow(curPos); 77 | } 78 | prePos = curPos; 79 | } 80 | 81 | void GameScene::onTouchEnded(Touch *touch, Event *unused_event) { 82 | if (!player->getIsRunAction()) { 83 | player->shootArrow(touch->getLocation()); 84 | } 85 | } 86 | 87 | Scene *GameScene::createScene() { 88 | auto scene = Scene::create(); 89 | auto gameLayer = GameScene::create(); 90 | scene->addChild(gameLayer); 91 | 92 | return scene; 93 | } -------------------------------------------------------------------------------- /brick/Arrow/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "Player.h" 4 | USING_NS_CC; 5 | 6 | class GameScene : public Layer { 7 | public: 8 | static Scene* createScene(); 9 | virtual bool init(); 10 | virtual bool onTouchBegan(Touch *touch, Event *unused_event); 11 | virtual void onTouchMoved(Touch *touch, Event *unused_event); 12 | virtual void onTouchEnded(Touch *touch, Event *unused_event); 13 | 14 | CREATE_FUNC(GameScene); 15 | 16 | Size visible; 17 | Point origin; 18 | Point prePos; 19 | Point curPos; 20 | 21 | Player* player; 22 | Array* monsterArr; 23 | }; 24 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | 3 | USING_NS_CC; 4 | 5 | Scene* HelloWorld::createScene() 6 | { 7 | // 'scene' is an autorelease object 8 | auto scene = Scene::create(); 9 | 10 | // 'layer' is an autorelease object 11 | auto layer = HelloWorld::create(); 12 | 13 | // add layer as a child to scene 14 | scene->addChild(layer); 15 | 16 | // return the scene 17 | return scene; 18 | } 19 | 20 | // on "init" you need to initialize your instance 21 | bool HelloWorld::init() 22 | { 23 | ////////////////////////////// 24 | // 1. super init first 25 | if ( !Layer::init() ) 26 | { 27 | return false; 28 | } 29 | 30 | Size visibleSize = Director::getInstance()->getVisibleSize(); 31 | Vec2 origin = Director::getInstance()->getVisibleOrigin(); 32 | 33 | ///////////////////////////// 34 | // 2. add a menu item with "X" image, which is clicked to quit the program 35 | // you may modify it. 36 | 37 | // add a "close" icon to exit the progress. it's an autorelease object 38 | auto closeItem = MenuItemImage::create( 39 | "CloseNormal.png", 40 | "CloseSelected.png", 41 | CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 42 | 43 | closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 44 | origin.y + closeItem->getContentSize().height/2)); 45 | 46 | // create menu, it's an autorelease object 47 | auto menu = Menu::create(closeItem, NULL); 48 | menu->setPosition(Vec2::ZERO); 49 | this->addChild(menu, 1); 50 | 51 | ///////////////////////////// 52 | // 3. add your codes below... 53 | 54 | // add a label shows "Hello World" 55 | // create and initialize a label 56 | 57 | auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 58 | 59 | // position the label on the center of the screen 60 | label->setPosition(Vec2(origin.x + visibleSize.width/2, 61 | origin.y + visibleSize.height - label->getContentSize().height)); 62 | 63 | // add the label as a child to this layer 64 | this->addChild(label, 1); 65 | 66 | // add "HelloWorld" splash screen" 67 | auto sprite = Sprite::create("HelloWorld.png"); 68 | 69 | // position the sprite on the center of the screen 70 | sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 71 | 72 | // add the sprite as a child to this layer 73 | this->addChild(sprite, 0); 74 | 75 | return true; 76 | } 77 | 78 | 79 | void HelloWorld::menuCloseCallback(Ref* pSender) 80 | { 81 | Director::getInstance()->end(); 82 | 83 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 84 | exit(0); 85 | #endif 86 | } 87 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class HelloWorld : public cocos2d::Layer 7 | { 8 | public: 9 | static cocos2d::Scene* createScene(); 10 | 11 | virtual bool init(); 12 | 13 | // a selector callback 14 | void menuCloseCallback(cocos2d::Ref* pSender); 15 | 16 | // implement the "static create()" method manually 17 | CREATE_FUNC(HelloWorld); 18 | }; 19 | 20 | #endif // __HELLOWORLD_SCENE_H__ 21 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/Player.cpp: -------------------------------------------------------------------------------- 1 | #include "Player.h" 2 | 3 | Player::Player() { 4 | } 5 | 6 | Player* Player::create(Vec2 pos) { 7 | auto player = new Player(); 8 | if (player && player->init(pos)) { 9 | player->autorelease(); 10 | return player; 11 | } 12 | delete player; 13 | player = NULL; 14 | return NULL; 15 | } 16 | 17 | bool Player::init(Vec2 pos) { 18 | if (!Sprite::init()) { 19 | return false; 20 | } 21 | this->playerPos = pos; 22 | isRunAction = false; 23 | arrowArr = Array::create(); 24 | arrowArr->retain(); 25 | createPlayer(); 26 | createPlayerHP(); 27 | scheduleUpdate(); 28 | return true; 29 | } 30 | 31 | void Player::createPlayer() { 32 | playerBodySprite = Sprite::create("player.png"); 33 | playerBodySprite->setAnchorPoint(Vec2(0.5, 0.5)); 34 | playerBodySprite->setPosition(playerPos); 35 | addChild(playerBodySprite); 36 | 37 | arrowSprite = Sprite::create("arrow.png"); 38 | arrowSprite->setAnchorPoint(Vec2(0, 0.5)); 39 | arrowSprite->setPosition(Vec2(playerBodySprite->getContentSize().width - 7, 40 | playerBodySprite->getContentSize().height / 2)); 41 | playerBodySprite->addChild(arrowSprite); 42 | } 43 | 44 | void Player::createPlayerHP() { 45 | hpSprite = Sprite::create("xuetiao_empty.png"); 46 | hpSprite->setAnchorPoint(Vec2(0.5, 0.5)); 47 | hpSprite->setPosition(Vec2(playerBodySprite->getContentSize().width / 2, playerBodySprite->getContentSize().height + 10)); 48 | playerBodySprite->addChild(hpSprite); 49 | 50 | hpBar = ProgressTimer::create(Sprite::create("xuetiao_full.png")); 51 | hpBar->setType(ProgressTimer::Type::BAR); 52 | hpBar->setMidpoint(Vec2(0, 0.5)); 53 | hpBar->setPercentage(100); 54 | hpBar->setBarChangeRate(Vec2(1, 0)); 55 | hpBar->setPosition(Vec2(0, 0)); 56 | hpBar->setAnchorPoint(Vec2(0, 0)); 57 | hpSprite->addChild(hpBar); 58 | 59 | } 60 | 61 | void Player::rotateArrow(Point touch) { 62 | Vec2 diff = touch - playerBodySprite->getPosition(); 63 | auto angle = diff.getAngle(); 64 | auto degree = CC_RADIANS_TO_DEGREES(-1 * angle); 65 | 66 | if (degree >= 90 && degree <= 180) { 67 | degree = 90; 68 | } 69 | if (degree >= -180 && degree <= -90) { 70 | degree = -90; 71 | } 72 | auto speed = 0.5 / M_PI; 73 | auto duration = fabs(angle * speed); 74 | 75 | arrowSprite->runAction(RotateTo::create(duration, degree)); 76 | } 77 | 78 | void Player::finishShoot(Sprite *arrow) { 79 | //arrow->removeFromParentAndCleanup(true); 80 | log("Y: %f", arrow->getPositionX()); 81 | isRunAction = false; 82 | } 83 | /* 84 | void Player::createAndShootArrow(Point point) { 85 | isRunAction = true; 86 | auto animation = AnimationCache::getInstance()->getAnimation("player"); 87 | auto animate = Animate::create(animation); 88 | 89 | auto funcall1 = CallFunc::create(CC_CALLBACK_0(Player::shootArrow, this)); 90 | auto delay = DelayTime::create(0.5f); 91 | auto funcall2 = CallFunc::create(CC_CALLBACK_0(Player::finishShoot, this)); 92 | playerBodySprite->runAction(Sequence::create(animate, funcall1, delay, funcall2, NULL)); 93 | } 94 | */ 95 | void Player::shootArrow(Point touch) { 96 | isRunAction = true; 97 | outArrow = Sprite::create("arrow1.png"); 98 | outArrow->setPosition(Vec2(arrowSprite->getPositionX(), 99 | arrowSprite->getPositionY())); 100 | playerBodySprite->addChild(outArrow); 101 | 102 | ps = ParticleMeteor::createWithTotalParticles(100); 103 | ps->setTexture(Director::getInstance()->getTextureCache()->addImage("arrow.png")); 104 | ps->setPosition(Vec2(0, 0)); 105 | ps->setScale(0.1); 106 | addChild(ps); 107 | 108 | Point world = outArrow->convertToWorldSpace(Vec2(0, 0)); 109 | auto path = ArrowPath::create(2.0f, touch, Vec2(touch.x * 2 - world.x, 0)); 110 | outArrow->runAction(Sequence::create(path, CallFunc::create([=]() 111 | { 112 | isRunAction = false; 113 | outArrow->removeFromParentAndCleanup(true); 114 | ps->removeFromParentAndCleanup(true); 115 | }), NULL)); 116 | //arrowArr->addObject(arrow); 117 | //isRunAction = false; 118 | } 119 | 120 | void Player::update(float delat) { 121 | if (isRunAction) { 122 | ps->setPosition(outArrow->convertToWorldSpace(Vec2(0, 0))); 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /brick/Arrow/Classes/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | 5 | class Player : public Sprite { 6 | public: 7 | Player(); 8 | bool init(Vec2 pos); 9 | static Player* create(Vec2 pos); 10 | 11 | void createPlayer(); 12 | void createPlayerHP(); 13 | void rotateArrow(Point touch); 14 | void shootArrow(Point point); 15 | void finishShoot(Sprite *sprite); 16 | void createAndShootArrow(Point point); 17 | void update(float delat); 18 | 19 | CC_SYNTHESIZE(int, playerHP, PlayerHP); 20 | CC_SYNTHESIZE(bool, isRunAction, IsRunAction); 21 | CC_SYNTHESIZE(bool, startDraw, StartDraw); 22 | 23 | private: 24 | Vec2 playerPos; 25 | Sprite *playerBodySprite; 26 | Sprite *arrowSprite; 27 | Sprite *outArrow; 28 | Sprite *hpSprite; 29 | Array *arrowArr; 30 | ProgressTimer *hpBar; 31 | DrawNode* drawNode; 32 | ParticleMeteor* ps; 33 | }; 34 | 35 | class ArrowPath : public BezierBy{ 36 | public: 37 | //ArrowPath(); 38 | static ArrowPath* create(float time, Point ctlPoint, Point endPoint); 39 | bool initWithDuration(float t, Point ctlPoint, Point endPoint); 40 | virtual void update(float time); 41 | 42 | Point ctlPoint; 43 | Point endPoint; 44 | }; 45 | -------------------------------------------------------------------------------- /brick/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "MainScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); 7 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 8 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 9 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 10 | 11 | AppDelegate::AppDelegate() { 12 | 13 | } 14 | 15 | AppDelegate::~AppDelegate() 16 | { 17 | } 18 | 19 | //if you want a different context,just modify the value of glContextAttrs 20 | //it will takes effect on all platforms 21 | void AppDelegate::initGLContextAttrs() 22 | { 23 | //set OpenGL context attributions,now can only set six attributions: 24 | //red,green,blue,alpha,depth,stencil 25 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 26 | 27 | GLView::setGLContextAttrs(glContextAttrs); 28 | } 29 | 30 | // If you want to use packages manager to install more packages, 31 | // don't modify or remove this function 32 | static int register_all_packages() 33 | { 34 | return 0; //flag for packages manager 35 | } 36 | 37 | bool AppDelegate::applicationDidFinishLaunching() { 38 | // initialize director 39 | auto director = Director::getInstance(); 40 | auto glview = director->getOpenGLView(); 41 | if(!glview) { 42 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 43 | glview = GLViewImpl::createWithRect("brick", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 44 | #else 45 | glview = GLViewImpl::create("brick"); 46 | #endif 47 | director->setOpenGLView(glview); 48 | } 49 | 50 | // turn on display FPS 51 | director->setDisplayStats(true); 52 | 53 | // set FPS. the default value is 1.0/60 if you don't call this 54 | director->setAnimationInterval(1.0 / 60); 55 | 56 | glview->setFrameSize(960, 640); 57 | // Set the design resolution 58 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); 59 | Size frameSize = glview->getFrameSize(); 60 | // if the frame's height is larger than the height of medium size. 61 | if (frameSize.height > mediumResolutionSize.height) 62 | { 63 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 64 | } 65 | // if the frame's height is larger than the height of small size. 66 | else if (frameSize.height > smallResolutionSize.height) 67 | { 68 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 69 | } 70 | // if the frame's height is smaller than the height of medium size. 71 | else 72 | { 73 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 74 | } 75 | 76 | register_all_packages(); 77 | 78 | // create a scene. it's an autorelease object 79 | auto scene = MainScene::createScene(); 80 | 81 | // run 82 | director->runWithScene(scene); 83 | 84 | return true; 85 | } 86 | 87 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 88 | void AppDelegate::applicationDidEnterBackground() { 89 | Director::getInstance()->stopAnimation(); 90 | 91 | // if you use SimpleAudioEngine, it must be pause 92 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 93 | } 94 | 95 | // this function will be called when the app is active again 96 | void AppDelegate::applicationWillEnterForeground() { 97 | Director::getInstance()->startAnimation(); 98 | 99 | // if you use SimpleAudioEngine, it must resume here 100 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 101 | } 102 | -------------------------------------------------------------------------------- /brick/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/Classes/GameScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/Classes/GameScene.cpp -------------------------------------------------------------------------------- /brick/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include 4 | USING_NS_CC; 5 | using namespace std; 6 | 7 | class GameScene : public Layer { 8 | public : 9 | virtual bool init(); 10 | static Scene* createScene(); 11 | void initGame(); 12 | virtual void update(float time); 13 | 14 | bool onTouchBegan(Touch *touch, Event *unused_event); 15 | void onTouchMoved(Touch *touch, Event *unused_event); 16 | void onTouchEnded(Touch *touch, Event *unused_event); 17 | 18 | void collisionTop(); 19 | void collisionButtom(bool base); 20 | void collisionLeft(); 21 | void collisionRight(); 22 | 23 | void changeDirection(int direction, bool base); 24 | void moveBall(Vec2 direction); 25 | CREATE_FUNC(GameScene); 26 | 27 | private: 28 | int totalBrick; 29 | int preFlashCount; 30 | int curFlashCount; 31 | bool isStart; 32 | float preX; 33 | float horAcceration; 34 | float ballSpeed; 35 | float scale_x; 36 | Size visible; 37 | Point origin; 38 | vector> bricks; 39 | Sprite *ball; 40 | Sprite *bottom; 41 | Vec2 preDirection; 42 | 43 | }; 44 | -------------------------------------------------------------------------------- /brick/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | 3 | USING_NS_CC; 4 | 5 | Scene* HelloWorld::createScene() 6 | { 7 | // 'scene' is an autorelease object 8 | auto scene = Scene::create(); 9 | 10 | // 'layer' is an autorelease object 11 | auto layer = HelloWorld::create(); 12 | 13 | // add layer as a child to scene 14 | scene->addChild(layer); 15 | 16 | // return the scene 17 | return scene; 18 | } 19 | 20 | // on "init" you need to initialize your instance 21 | bool HelloWorld::init() 22 | { 23 | ////////////////////////////// 24 | // 1. super init first 25 | if ( !Layer::init() ) 26 | { 27 | return false; 28 | } 29 | 30 | Size visibleSize = Director::getInstance()->getVisibleSize(); 31 | Vec2 origin = Director::getInstance()->getVisibleOrigin(); 32 | 33 | ///////////////////////////// 34 | // 2. add a menu item with "X" image, which is clicked to quit the program 35 | // you may modify it. 36 | 37 | // add a "close" icon to exit the progress. it's an autorelease object 38 | auto closeItem = MenuItemImage::create( 39 | "CloseNormal.png", 40 | "CloseSelected.png", 41 | CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 42 | 43 | closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 44 | origin.y + closeItem->getContentSize().height/2)); 45 | 46 | // create menu, it's an autorelease object 47 | auto menu = Menu::create(closeItem, NULL); 48 | menu->setPosition(Vec2::ZERO); 49 | this->addChild(menu, 1); 50 | 51 | ///////////////////////////// 52 | // 3. add your codes below... 53 | 54 | // add a label shows "Hello World" 55 | // create and initialize a label 56 | 57 | auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 58 | 59 | // position the label on the center of the screen 60 | label->setPosition(Vec2(origin.x + visibleSize.width/2, 61 | origin.y + visibleSize.height - label->getContentSize().height)); 62 | 63 | // add the label as a child to this layer 64 | this->addChild(label, 1); 65 | 66 | // add "HelloWorld" splash screen" 67 | auto sprite = Sprite::create("HelloWorld.png"); 68 | 69 | // position the sprite on the center of the screen 70 | sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 71 | 72 | // add the sprite as a child to this layer 73 | this->addChild(sprite, 0); 74 | 75 | return true; 76 | } 77 | 78 | 79 | void HelloWorld::menuCloseCallback(Ref* pSender) 80 | { 81 | Director::getInstance()->end(); 82 | 83 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 84 | exit(0); 85 | #endif 86 | } 87 | -------------------------------------------------------------------------------- /brick/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class HelloWorld : public cocos2d::Layer 7 | { 8 | public: 9 | static cocos2d::Scene* createScene(); 10 | 11 | virtual bool init(); 12 | 13 | // a selector callback 14 | void menuCloseCallback(cocos2d::Ref* pSender); 15 | 16 | // implement the "static create()" method manually 17 | CREATE_FUNC(HelloWorld); 18 | }; 19 | 20 | #endif // __HELLOWORLD_SCENE_H__ 21 | -------------------------------------------------------------------------------- /brick/Classes/MainScene.cpp: -------------------------------------------------------------------------------- 1 | #include "MainScene.h" 2 | #include "GameScene.h" 3 | void MainScene::startGame(Ref *ref) { 4 | Director::getInstance()->replaceScene(GameScene::createScene()); 5 | } 6 | void MainScene::exitGame(Ref *ref) { 7 | 8 | } 9 | Scene* MainScene::createScene() { 10 | auto scene = Scene::create(); 11 | auto main = MainScene::create(); 12 | scene->addChild(main); 13 | 14 | return scene; 15 | } 16 | bool MainScene::init() { 17 | if (!Layer::init()) { 18 | return false; 19 | } 20 | 21 | visible = Director::getInstance()->getVisibleSize(); 22 | origin = Director::getInstance()->getVisibleOrigin(); 23 | 24 | auto bgLayer = LayerColor::create(Color4B(126, 50, 157, 10), origin.x + visible.width, origin.y + visible.height); 25 | bgLayer->setAnchorPoint(Vec2(0, 0)); 26 | bgLayer->setPosition(0, 0); 27 | addChild(bgLayer, -1); 28 | 29 | TTFConfig config("fonts/Marker Felt.ttf", 50); 30 | auto startLabel = Label::createWithTTF(config, "Start", TextHAlignment::CENTER); 31 | startBtn = MenuItemLabel::create(startLabel, CC_CALLBACK_1(MainScene::startGame, this)); 32 | //startBtn->setAnchorPoint(Vec2(0, 0)); 33 | //startBtn->setPosition(0, 80); 34 | 35 | auto exitLabel = Label::createWithTTF(config, "Exit", TextHAlignment::CENTER); 36 | exitBtn = MenuItemLabel::create(exitLabel, CC_CALLBACK_1(MainScene::exitGame, this)); 37 | //exitBtn->setAnchorPoint(Vec2(0, 0)); 38 | //exitBtn->setPosition(0, 0); 39 | 40 | mainMenu = Menu::create(startBtn, exitBtn, NULL); 41 | mainMenu->setAnchorPoint(Vec2(0.5, 0.5)); 42 | mainMenu->setPosition(origin.x + visible.width / 2, origin.y + visible.height / 2); 43 | mainMenu->alignItemsVertically(); 44 | addChild(mainMenu, 0); 45 | 46 | return true; 47 | } -------------------------------------------------------------------------------- /brick/Classes/MainScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | class MainScene : public Layer { 5 | public: 6 | virtual bool init(); 7 | static Scene* createScene(); 8 | 9 | void startGame(Ref *ref); 10 | void exitGame(Ref *ref); 11 | 12 | CREATE_FUNC(MainScene); 13 | 14 | private: 15 | Menu *mainMenu; 16 | MenuItemLabel* startBtn; 17 | MenuItemLabel* exitBtn; 18 | 19 | Size visible; 20 | Point origin; 21 | }; 22 | -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "MenuScene.h" 3 | USING_NS_CC; 4 | 5 | static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); 6 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 7 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 8 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 9 | 10 | AppDelegate::AppDelegate() { 11 | 12 | } 13 | 14 | AppDelegate::~AppDelegate() 15 | { 16 | } 17 | 18 | //if you want a different context,just modify the value of glContextAttrs 19 | //it will takes effect on all platforms 20 | void AppDelegate::initGLContextAttrs() 21 | { 22 | //set OpenGL context attributions,now can only set six attributions: 23 | //red,green,blue,alpha,depth,stencil 24 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 25 | 26 | GLView::setGLContextAttrs(glContextAttrs); 27 | } 28 | 29 | // If you want to use packages manager to install more packages, 30 | // don't modify or remove this function 31 | static int register_all_packages() 32 | { 33 | return 0; //flag for packages manager 34 | } 35 | 36 | bool AppDelegate::applicationDidFinishLaunching() { 37 | // initialize director 38 | auto director = Director::getInstance(); 39 | auto glview = director->getOpenGLView(); 40 | if(!glview) { 41 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 42 | glview = GLViewImpl::createWithRect("FlippyBird", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 43 | #else 44 | glview = GLViewImpl::create("FlippyBird"); 45 | #endif 46 | director->setOpenGLView(glview); 47 | } 48 | 49 | // turn on display FPS 50 | director->setDisplayStats(true); 51 | 52 | // set FPS. the default value is 1.0/60 if you don't call this 53 | director->setAnimationInterval(1.0 / 60); 54 | 55 | // Set the design resolution 56 | glview->setFrameSize(960, 640); 57 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT); 58 | director->setContentScaleFactor(480.0f / 320.0f); 59 | Size frameSize = glview->getFrameSize(); 60 | // if the frame's height is larger than the height of medium size. 61 | if (frameSize.height > mediumResolutionSize.height) 62 | { 63 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 64 | } 65 | // if the frame's height is larger than the height of small size. 66 | else if (frameSize.height > smallResolutionSize.height) 67 | { 68 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 69 | } 70 | // if the frame's height is smaller than the height of medium size. 71 | else 72 | { 73 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 74 | } 75 | 76 | register_all_packages(); 77 | 78 | // create a scene. it's an autorelease object 79 | auto scene = MenuScene::createScene(); 80 | 81 | // run 82 | director->runWithScene(scene); 83 | 84 | return true; 85 | } 86 | 87 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 88 | void AppDelegate::applicationDidEnterBackground() { 89 | Director::getInstance()->stopAnimation(); 90 | 91 | // if you use SimpleAudioEngine, it must be pause 92 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 93 | } 94 | 95 | // this function will be called when the app is active again 96 | void AppDelegate::applicationWillEnterForeground() { 97 | Director::getInstance()->startAnimation(); 98 | 99 | // if you use SimpleAudioEngine, it must resume here 100 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 101 | } 102 | -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/GameScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/FlippyBird/Classes/GameScene.cpp -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | #define SPEED 50 5 | class GameScene : public Layer{ 6 | public : 7 | virtual bool init(); 8 | static Scene *createScene(); 9 | float PIPE_INTERVAL; 10 | void update(float time); 11 | void initVariable(); 12 | bool onTouchBegan(Touch *touch, Event *unused_event); 13 | void onTouchMoved(Touch *touch, Event *unused_event); 14 | void onTouchEnded(Touch *touch, Event *unused_event); 15 | void startGame(Ref* ref); 16 | void stopGame(Ref *ref); 17 | bool colllisionWithWall(); 18 | float getRandomY(float height, float prab); 19 | 20 | CREATE_FUNC(GameScene); 21 | private: 22 | int flashCount; 23 | int collisionIndex; 24 | int previousPipe; 25 | int score; 26 | float pipeBirdInterval; 27 | float lastTime; 28 | float runTime; 29 | float aggregation; 30 | float birdSpeed; 31 | Size visible; 32 | Point origin; 33 | Vector pipeVec; 34 | Sprite *birdSprite; 35 | Label *scoreLabel; 36 | Menu *menu; 37 | }; 38 | -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | 3 | USING_NS_CC; 4 | 5 | Scene* HelloWorld::createScene() 6 | { 7 | // 'scene' is an autorelease object 8 | auto scene = Scene::create(); 9 | 10 | // 'layer' is an autorelease object 11 | auto layer = HelloWorld::create(); 12 | 13 | // add layer as a child to scene 14 | scene->addChild(layer); 15 | 16 | // return the scene 17 | return scene; 18 | } 19 | 20 | // on "init" you need to initialize your instance 21 | bool HelloWorld::init() 22 | { 23 | ////////////////////////////// 24 | // 1. super init first 25 | if ( !Layer::init() ) 26 | { 27 | return false; 28 | } 29 | 30 | Size visibleSize = Director::getInstance()->getVisibleSize(); 31 | Vec2 origin = Director::getInstance()->getVisibleOrigin(); 32 | 33 | ///////////////////////////// 34 | // 2. add a menu item with "X" image, which is clicked to quit the program 35 | // you may modify it. 36 | 37 | // add a "close" icon to exit the progress. it's an autorelease object 38 | auto closeItem = MenuItemImage::create( 39 | "CloseNormal.png", 40 | "CloseSelected.png", 41 | CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 42 | 43 | closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 44 | origin.y + closeItem->getContentSize().height/2)); 45 | 46 | // create menu, it's an autorelease object 47 | auto menu = Menu::create(closeItem, NULL); 48 | menu->setPosition(Vec2::ZERO); 49 | this->addChild(menu, 1); 50 | 51 | ///////////////////////////// 52 | // 3. add your codes below... 53 | 54 | // add a label shows "Hello World" 55 | // create and initialize a label 56 | 57 | auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 58 | 59 | // position the label on the center of the screen 60 | label->setPosition(Vec2(origin.x + visibleSize.width/2, 61 | origin.y + visibleSize.height - label->getContentSize().height)); 62 | 63 | // add the label as a child to this layer 64 | this->addChild(label, 1); 65 | 66 | // add "HelloWorld" splash screen" 67 | auto sprite = Sprite::create("HelloWorld.png"); 68 | 69 | // position the sprite on the center of the screen 70 | sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 71 | 72 | // add the sprite as a child to this layer 73 | this->addChild(sprite, 0); 74 | 75 | return true; 76 | } 77 | 78 | 79 | void HelloWorld::menuCloseCallback(Ref* pSender) 80 | { 81 | Director::getInstance()->end(); 82 | 83 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 84 | exit(0); 85 | #endif 86 | } 87 | -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class HelloWorld : public cocos2d::Layer 7 | { 8 | public: 9 | static cocos2d::Scene* createScene(); 10 | 11 | virtual bool init(); 12 | 13 | // a selector callback 14 | void menuCloseCallback(cocos2d::Ref* pSender); 15 | 16 | // implement the "static create()" method manually 17 | CREATE_FUNC(HelloWorld); 18 | }; 19 | 20 | #endif // __HELLOWORLD_SCENE_H__ 21 | -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/MenuScene.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuScene.h" 2 | #include "GameScene.h" 3 | void MenuScene::startGame(Ref* ref) { 4 | log("start game"); 5 | Director::getInstance()->replaceScene(TransitionFade::create(0.2f, GameScene::createScene())); 6 | } 7 | 8 | void MenuScene::endGame(Ref* ref) { 9 | log("end game"); 10 | Director::getInstance()->end(); 11 | } 12 | 13 | bool MenuScene::init() { 14 | if (!Layer::init()) { 15 | return false; 16 | } 17 | visible = Director::getInstance()->getVisibleSize(); 18 | origin = Director::getInstance()->getVisibleOrigin(); 19 | 20 | auto beginItem = MenuItemFont::create("Begin", this, menu_selector(MenuScene::startGame)); 21 | beginItem->setAnchorPoint(Vec2(0.5f, 0.5f)); 22 | beginItem->setPosition(origin.x + visible.width / 2, origin.y + visible.height / 3 * 2); 23 | 24 | auto exitItem = MenuItemFont::create("Exit", this, menu_selector(MenuScene::endGame)); 25 | exitItem->setAnchorPoint(Vec2(0.5f, 0.5f)); 26 | exitItem->setPosition(origin.x + visible.width / 2, origin.y + visible.height / 3); 27 | auto menu = Menu::create(beginItem, exitItem, NULL); 28 | menu->setPosition(0, 0); 29 | menu->setAnchorPoint(Vec2(0.5f, 0.5f)); 30 | addChild(menu); 31 | 32 | return true; 33 | } 34 | 35 | Scene* MenuScene::createScene() { 36 | auto scene = Scene::create(); 37 | auto menu = MenuScene::create(); 38 | scene->addChild(menu); 39 | return scene; 40 | } -------------------------------------------------------------------------------- /brick/FlippyBird/Classes/MenuScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | 5 | class MenuScene : public Layer { 6 | public: 7 | virtual bool init(); 8 | static Scene* createScene(); 9 | void startGame(Ref *); 10 | void endGame(Ref *); 11 | 12 | CREATE_FUNC(MenuScene); 13 | private: 14 | Size visible; 15 | Point origin; 16 | }; 17 | -------------------------------------------------------------------------------- /brick/Plane/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "MainScene.h" 3 | #include "HelloWorldScene.h" 4 | USING_NS_CC; 5 | 6 | static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); 7 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 8 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 9 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 10 | 11 | AppDelegate::AppDelegate() { 12 | 13 | } 14 | 15 | AppDelegate::~AppDelegate() 16 | { 17 | } 18 | 19 | //if you want a different context,just modify the value of glContextAttrs 20 | //it will takes effect on all platforms 21 | void AppDelegate::initGLContextAttrs() 22 | { 23 | //set OpenGL context attributions,now can only set six attributions: 24 | //red,green,blue,alpha,depth,stencil 25 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 26 | 27 | GLView::setGLContextAttrs(glContextAttrs); 28 | } 29 | 30 | // If you want to use packages manager to install more packages, 31 | // don't modify or remove this function 32 | static int register_all_packages() 33 | { 34 | return 0; //flag for packages manager 35 | } 36 | 37 | bool AppDelegate::applicationDidFinishLaunching() { 38 | // initialize director 39 | auto director = Director::getInstance(); 40 | auto glview = director->getOpenGLView(); 41 | if(!glview) { 42 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 43 | glview = GLViewImpl::createWithRect("slg", Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 44 | #else 45 | glview = GLViewImpl::create("slg"); 46 | #endif 47 | director->setOpenGLView(glview); 48 | } 49 | 50 | glview->setFrameSize(960, 640); 51 | // turn on display FPS 52 | director->setDisplayStats(true); 53 | 54 | // set FPS. the default value is 1.0/60 if you don't call this 55 | director->setAnimationInterval(1.0 / 60); 56 | 57 | // Set the design resolution 58 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); 59 | Size frameSize = glview->getFrameSize(); 60 | // if the frame's height is larger than the height of medium size. 61 | if (frameSize.height > mediumResolutionSize.height) 62 | { 63 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 64 | } 65 | // if the frame's height is larger than the height of small size. 66 | else if (frameSize.height > smallResolutionSize.height) 67 | { 68 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 69 | } 70 | // if the frame's height is smaller than the height of medium size. 71 | else 72 | { 73 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 74 | } 75 | 76 | register_all_packages(); 77 | 78 | // create a scene. it's an autorelease object 79 | auto scene = HelloWorld::createScene(); 80 | 81 | // run 82 | director->runWithScene(scene); 83 | 84 | return true; 85 | } 86 | 87 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 88 | void AppDelegate::applicationDidEnterBackground() { 89 | Director::getInstance()->stopAnimation(); 90 | 91 | // if you use SimpleAudioEngine, it must be pause 92 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 93 | } 94 | 95 | // this function will be called when the app is active again 96 | void AppDelegate::applicationWillEnterForeground() { 97 | Director::getInstance()->startAnimation(); 98 | 99 | // if you use SimpleAudioEngine, it must resume here 100 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 101 | } 102 | -------------------------------------------------------------------------------- /brick/Plane/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/Plane/Classes/EnemyPlane.cpp: -------------------------------------------------------------------------------- 1 | #include "EnemyPlane.h" 2 | 3 | 4 | EnemyPlane* EnemyPlane::createEnemy(ENEMY_TYPE enemyType) { 5 | EnemyPlane* sprite = new EnemyPlane(); 6 | if (enemyType == BIG) { 7 | sprite->initWithFile("big.png"); 8 | sprite->score = 10; 9 | sprite->blood = 3; 10 | } 11 | else if (enemyType == MEDIUM) { 12 | sprite->initWithFile("medium.png"); 13 | sprite->score = 5; 14 | sprite->blood = 2; 15 | } 16 | else { 17 | sprite->initWithFile("small.png"); 18 | sprite->score = 2; 19 | sprite->blood = 1; 20 | } 21 | sprite->autorelease(); 22 | sprite->type = enemyType; 23 | sprite->setAnchorPoint(Vec2(0.5, 1)); 24 | return sprite; 25 | } -------------------------------------------------------------------------------- /brick/Plane/Classes/EnemyPlane.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | 5 | enum ENEMY_TYPE {SMALL, MEDIUM, BIG}; 6 | class EnemyPlane : public Sprite { 7 | 8 | public: 9 | static EnemyPlane* createEnemy(ENEMY_TYPE enemyType); 10 | int blood; 11 | int score; 12 | ENEMY_TYPE type; 13 | }; 14 | -------------------------------------------------------------------------------- /brick/Plane/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/Plane/Classes/HelloWorldScene.cpp -------------------------------------------------------------------------------- /brick/Plane/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class HelloWorld : public cocos2d::Layer 7 | { 8 | public: 9 | static cocos2d::Scene* createScene(); 10 | 11 | virtual bool init(); 12 | 13 | // a selector callback 14 | void menuCloseCallback(cocos2d::Ref* pSender); 15 | void startClicked(Ref* pSender); 16 | // implement the "static create()" method manually 17 | CREATE_FUNC(HelloWorld); 18 | }; 19 | 20 | #endif // __HELLOWORLD_SCENE_H__ 21 | -------------------------------------------------------------------------------- /brick/Plane/Classes/MainScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/Plane/Classes/MainScene.cpp -------------------------------------------------------------------------------- /brick/Plane/Classes/MainScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | 5 | #define BULLET_SIZE 5 6 | #define BULLET_INTERVAL 70 7 | #define ENEMY_INTERVAL 20 8 | #define ENEMY_SPEED 3 9 | class MainScene : public Layer { 10 | public: 11 | static Scene* createScene(); 12 | bool init(); 13 | void update(float delat); 14 | CREATE_FUNC(MainScene); 15 | 16 | void pauseGame(Ref *); 17 | bool onTouchBegan(Touch *touch, Event *unused_event); 18 | void onTouchMoved(Touch *touch, Event *unused_event); 19 | void onTouchEnded(Touch *touch, Event *unused_event); 20 | 21 | //void onTouchesBegan(const std::vector& touches, Event *unused_event); 22 | //void onTouchesEnded(const std::vector& touches, Event *unused_event); 23 | private: 24 | LayerColor* uiLayer; 25 | Layer* bgLayer; 26 | Layer* logicLayer; 27 | 28 | void initUILayer(); 29 | void initBgLayer(); 30 | void initLogicLayer(); 31 | void rollBg(); 32 | void launchBullet(); 33 | void createEnemy(); 34 | void flashEnemy(); 35 | void cleanAll(); 36 | 37 | void startGame(Ref*); 38 | void restartGame(Ref*); 39 | void exitGame(Ref*); 40 | 41 | int bgHeight; 42 | Sprite* bgSprite1, *bgSprite2; 43 | 44 | MenuItemImage *pauseMenuItem; 45 | MenuItemFont *scoreMenuFont; 46 | Menu *mainMenu; 47 | SpriteFrame* pauseImageSprite; 48 | SpriteFrame* continueImageSprite; 49 | Sprite* planeSprite; 50 | Sprite* bullet[BULLET_SIZE]; 51 | Array* enemyArr; 52 | Point curPoint; 53 | bool isPause; 54 | int score; 55 | int flashCount; 56 | int bigCount; 57 | int mediumCount; 58 | int smallCount; 59 | LayerColor *logicalLayer; 60 | }; 61 | -------------------------------------------------------------------------------- /brick/Plane/Classes/Plane.cpp: -------------------------------------------------------------------------------- 1 | #include "Plane.h" 2 | 3 | void AirPlane::onTouchMoved(Point *point, Event* event) { 4 | auto origin = Director::getInstance()->getVisibleOrigin(); 5 | auto visible = Director::getInstance()->getVisibleSize(); 6 | 7 | auto diff = point->x - curPoint.x; 8 | if (getPositionX() + diff >= origin.x + visible.width) { 9 | setPosition(Vec2(origin.x + visible.width, origin.y)); 10 | } 11 | else if (getPositionX() + diff <= origin.x) { 12 | setPosition(Vec2(origin.x, origin.y)); 13 | } 14 | else { 15 | setPosition((Vec2(origin.x + (diff + getPositionX()), origin.y)); 16 | } 17 | curPoint = *point; 18 | } 19 | bool AirPlane::onTouchBegan(Point *point, Event* event) { 20 | curPoint = *point; 21 | } 22 | void AirPlane::onTouchEnded(Point *point, Event* event) { 23 | 24 | } 25 | 26 | AirPlane* AirPlane::create(char *name) { 27 | auto visible = Director::getInstance()->getVisibleSize(); 28 | auto origin = Director::getInstance()->getVisibleOrigin(); 29 | AirPlane *plane = new AirPlane(); 30 | if (plane && plane->initWithFile("")) { 31 | plane->autorelease(); 32 | plane->setAnchorPoint(Vec2(0.5, 0)); 33 | plane->setPosition(Vec2(origin.x + visible.width/ 2, origin.y)); 34 | return plane; 35 | } 36 | return NULL; 37 | } -------------------------------------------------------------------------------- /brick/Plane/Classes/Plane.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | class AirPlane : public Sprite { 5 | public: 6 | static AirPlane* create(char *name); 7 | private: 8 | virtual bool onTouchBegan(Point *point, Event* event); 9 | virtual void onTouchMoved(Point *, Event*); 10 | virtual void onTouchEnded(Point *, Event*); 11 | 12 | Point curPoint; 13 | }; 14 | -------------------------------------------------------------------------------- /brick/carrot/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | #include "GameScene.h" 4 | USING_NS_CC; 5 | 6 | static cocos2d::Size designResolutionSize = cocos2d::Size(960, 640); 7 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 8 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 9 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 10 | 11 | AppDelegate::AppDelegate() { 12 | 13 | } 14 | 15 | AppDelegate::~AppDelegate() 16 | { 17 | } 18 | 19 | //if you want a different context,just modify the value of glContextAttrs 20 | //it will takes effect on all platforms 21 | void AppDelegate::initGLContextAttrs() 22 | { 23 | //set OpenGL context attributions,now can only set six attributions: 24 | //red,green,blue,alpha,depth,stencil 25 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 26 | 27 | GLView::setGLContextAttrs(glContextAttrs); 28 | } 29 | 30 | // If you want to use packages manager to install more packages, 31 | // don't modify or remove this function 32 | static int register_all_packages() 33 | { 34 | return 0; //flag for packages manager 35 | } 36 | 37 | bool AppDelegate::applicationDidFinishLaunching() { 38 | // initialize director 39 | auto director = Director::getInstance(); 40 | auto glview = director->getOpenGLView(); 41 | if(!glview) { 42 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 43 | glview = GLViewImpl::createWithRect("carrot", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 44 | #else 45 | glview = GLViewImpl::create("carrot"); 46 | #endif 47 | director->setOpenGLView(glview); 48 | } 49 | 50 | // turn on display FPS 51 | director->setDisplayStats(true); 52 | 53 | // set FPS. the default value is 1.0/60 if you don't call this 54 | director->setAnimationInterval(1.0 / 60); 55 | 56 | // Set the design resolution 57 | glview->setFrameSize(designResolutionSize.width, designResolutionSize.height); 58 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT); 59 | auto frameSize = glview->getFrameSize(); 60 | // if the frame's height is larger than the height of medium size. 61 | if (frameSize.height > mediumResolutionSize.height) 62 | { 63 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 64 | } 65 | // if the frame's height is larger than the height of small size. 66 | else if (frameSize.height > smallResolutionSize.height) 67 | { 68 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 69 | } 70 | // if the frame's height is smaller than the height of medium size. 71 | else 72 | { 73 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 74 | } 75 | director->setContentScaleFactor(480 / designResolutionSize.height); 76 | 77 | register_all_packages(); 78 | 79 | // create a scene. it's an autorelease object 80 | //auto scene = GameScene::createScene("bg2.tmx"); 81 | auto scene = HelloWorld::createScene(); 82 | // run 83 | director->runWithScene(scene); 84 | 85 | return true; 86 | } 87 | 88 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 89 | void AppDelegate::applicationDidEnterBackground() { 90 | Director::getInstance()->stopAnimation(); 91 | 92 | // if you use SimpleAudioEngine, it must be pause 93 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 94 | } 95 | 96 | // this function will be called when the app is active again 97 | void AppDelegate::applicationWillEnterForeground() { 98 | Director::getInstance()->startAnimation(); 99 | 100 | // if you use SimpleAudioEngine, it must resume here 101 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 102 | } 103 | -------------------------------------------------------------------------------- /brick/carrot/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/carrot/Classes/GameManager.cpp: -------------------------------------------------------------------------------- 1 | #include "GameManager.h" 2 | 3 | GameManager* GameManager::_gameManager = nullptr; 4 | GameManager* GameManager::getInstance() 5 | { 6 | if (!_gameManager) 7 | { 8 | _gameManager = new GameManager(); 9 | } 10 | return _gameManager; 11 | }; -------------------------------------------------------------------------------- /brick/carrot/Classes/GameManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/carrot/Classes/GameManager.h -------------------------------------------------------------------------------- /brick/carrot/Classes/GameScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/carrot/Classes/GameScene.cpp -------------------------------------------------------------------------------- /brick/carrot/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "Monster.h" 4 | #include "Tower.h" 5 | #include "GameManager.h" 6 | USING_NS_CC; 7 | class GameScene : public Layer { 8 | public: 9 | virtual bool init(); 10 | virtual void update(float dt); 11 | virtual bool onTouchBegan(Touch *touch, Event *unused_event); 12 | virtual void onTouchMoved(Touch *touch, Event *unused_event); 13 | virtual void onTouchEnded(Touch *touch, Event *unused_event); 14 | static Scene* createScene(std::string); 15 | static GameScene* create(std::string); 16 | 17 | GameScene(std::string); 18 | GameScene(); 19 | 20 | void initResource(Ref *ref); 21 | void tryAddTower(Point coorPoint, Point tiledPoint); 22 | Point tiledToOpenglCoord(Point position); 23 | Point openglToTiledCoord(Point position); 24 | CREATE_FUNC(GameScene); 25 | void addTower(); 26 | 27 | CC_SYNTHESIZE(std::string, sceneName, SceneName); 28 | private: 29 | Vector positions; 30 | Vector towers; 31 | std::vector> mapFlag; 32 | TMXTiledMap* tmx; 33 | Layer* towerAvailiable; 34 | GameManager* gameManager; 35 | Size visible; 36 | Point origin; 37 | Point prePoint; 38 | float offX; 39 | float monsterInterval; 40 | int flashCount; 41 | }; 42 | 43 | -------------------------------------------------------------------------------- /brick/carrot/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | #include "SimpleAudioEngine.h" 3 | 4 | Scene* HelloWorld::createScene() 5 | { 6 | // 'scene' is an autorelease object 7 | auto scene = Scene::create(); 8 | 9 | // 'layer' is an autorelease object 10 | auto layer = HelloWorld::create(); 11 | 12 | // add layer as a child to scene 13 | scene->addChild(layer); 14 | 15 | // return the scene 16 | return scene; 17 | } 18 | 19 | // on "init" you need to initialize your instance 20 | bool HelloWorld::init() 21 | { 22 | ////////////////////////////// 23 | // 1. super init first 24 | if ( !Layer::init() ) 25 | { 26 | return false; 27 | } 28 | 29 | auto visibleSize = Director::getInstance()->getVisibleSize(); 30 | Vec2 origin = Director::getInstance()->getVisibleOrigin(); 31 | 32 | ///////////////////////////// 33 | // 2. add a menu item with "X" image, which is clicked to quit the program 34 | // you may modify it. 35 | 36 | // add a "close" icon to exit the progress. it's an autorelease object 37 | auto closeItem = MenuItemImage::create( 38 | "CloseNormal.png", 39 | "CloseSelected.png", 40 | CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 41 | 42 | closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 43 | origin.y + closeItem->getContentSize().height/2)); 44 | 45 | // create menu, it's an autorelease object 46 | auto menu = Menu::create(closeItem, NULL); 47 | menu->setPosition(Vec2::ZERO); 48 | this->addChild(menu, 1); 49 | 50 | ///////////////////////////// 51 | // 3. add your codes below... 52 | 53 | // add a label shows "Hello World" 54 | // create and initialize a label 55 | 56 | auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 57 | 58 | // position the label on the center of the screen 59 | label->setPosition(Vec2(origin.x + visibleSize.width/2, 60 | origin.y + visibleSize.height - label->getContentSize().height)); 61 | 62 | // add the label as a child to this layer 63 | this->addChild(label, 1); 64 | 65 | // add "HelloWorld" splash screen" 66 | auto sprite = Sprite::create("HelloWorld.png"); 67 | 68 | // position the sprite on the center of the screen 69 | sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 70 | 71 | // add the sprite as a child to this layer 72 | this->addChild(sprite, 0); 73 | 74 | auto tableView = TableView::create(this, Size(100, 600)); 75 | tableView->setDirection(ScrollView::Direction::VERTICAL); 76 | tableView->setPosition(0, 0); 77 | tableView->setDelegate(this); 78 | tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN); 79 | //addChild(tableView); 80 | tableView->reloadData(); 81 | sprite = Sprite::create("Fan2.png"); 82 | sprite->setPosition(200, 0); 83 | sprite->runAction(Sequence::create(DelayTime::create(1.0f), JumpBy::create(5.0f, Point(0, 600), -150, 6), nullptr)); 84 | addChild(sprite); 85 | return true; 86 | } 87 | 88 | 89 | void HelloWorld::menuCloseCallback(Ref* pSender) 90 | { 91 | Director::getInstance()->end(); 92 | 93 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 94 | exit(0); 95 | #endif 96 | } 97 | 98 | void HelloWorld::tableCellTouched(TableView* table, TableViewCell* cell) 99 | { 100 | log("Touch at index %d", cell->getIdx()); 101 | } 102 | 103 | TableViewCell* HelloWorld::tableCellAtIndex(TableView *table, ssize_t idx) 104 | { 105 | static int oldCount = 0; 106 | static int newCount = 0; 107 | auto cell = table->dequeueCell(); 108 | if (!cell) 109 | { 110 | cell = TableViewCell::create(); 111 | auto sprite = Sprite::create("Fan2.png"); 112 | sprite->setAnchorPoint(Vec2::ZERO); 113 | sprite->setPosition(0, 0); 114 | auto label = LabelTTF::create("test", "fonts/arial.ttf", 10); 115 | label->setAnchorPoint(Vec2::ZERO); 116 | label->setPosition(0, 0); 117 | label->setTag(100); 118 | cell->addChild(label); 119 | cell->addChild(sprite); 120 | newCount++; 121 | log("old %d new %d", oldCount, newCount); 122 | return cell; 123 | } 124 | auto label = (LabelTTF *)cell->getChildByTag(100); 125 | label->setString(label->getString() + "1"); 126 | oldCount++; 127 | log("old %d new %d", oldCount, newCount); 128 | return cell; 129 | } 130 | -------------------------------------------------------------------------------- /brick/carrot/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | #include "cocos-ext.h" 6 | USING_NS_CC; 7 | USING_NS_CC_EXT; 8 | class HelloWorld : public cocos2d::Layer, public TableViewDataSource, public TableViewDelegate 9 | { 10 | public: 11 | static cocos2d::Scene* createScene(); 12 | 13 | virtual bool init(); 14 | virtual void scrollViewDidScroll(ScrollView* view) {}; 15 | virtual void scrollViewDidZoom(ScrollView* view) {}; 16 | virtual void tableCellTouched(TableView* table, TableViewCell* cell); 17 | virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx) { return Size(100, 60); }; 18 | virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx); 19 | virtual ssize_t numberOfCellsInTableView(TableView *table) { return 20; }; 20 | // a selector callback 21 | void menuCloseCallback(cocos2d::Ref* pSender); 22 | 23 | // implement the "static create()" method manually 24 | CREATE_FUNC(HelloWorld); 25 | }; 26 | 27 | #endif // __HELLOWORLD_SCENE_H__ 28 | -------------------------------------------------------------------------------- /brick/carrot/Classes/Monster.cpp: -------------------------------------------------------------------------------- 1 | #include "Monster.h" 2 | Monster *Monster::create(std::string name, float speed, float blood, Vector nodes) 3 | { 4 | auto monster = new Monster(); 5 | monster->currentIndex = 0; 6 | monster->totalBlood = blood; 7 | monster->initWithSpriteFrameName(name); 8 | monster->setSpeed(speed); 9 | monster->setBlood(blood); 10 | monster->setIsDead(false); 11 | monster->setOpacity(255); 12 | monster->autorelease(); 13 | monster->setAnchorPoint(Vec2(0.5, 0.5)); 14 | monster->setCascadeOpacityEnabled(true); 15 | for (size_t i = 0; i < nodes.size(); i++) 16 | { 17 | monster->path.pushBack(nodes.at(i)); 18 | } 19 | monster->setPosition(monster->path.at(0)->getPosition()); 20 | 21 | monster->bloodBar = Sprite::createWithSpriteFrameName("xuetiao_empty.png"); 22 | monster->bloodBar->setAnchorPoint(Vec2(0.5, 0)); 23 | monster->bloodBar->setPosition(monster->getContentSize().width / 2, monster->getContentSize().height); 24 | monster->addChild(monster->bloodBar); 25 | 26 | monster->progressBar = ProgressTimer::create(Sprite::createWithSpriteFrameName("xuetiao_full.png")); 27 | monster->progressBar->setType(ProgressTimer::Type::BAR); 28 | monster->progressBar->setMidpoint(Vec2(0, 0.5)); 29 | monster->progressBar->setBarChangeRate(Vec2(1, 0)); 30 | monster->progressBar->setPercentage(100); 31 | monster->progressBar->setPosition(monster->bloodBar->getContentSize().width / 2, monster->bloodBar->getContentSize().height / 2); 32 | monster->bloodBar->addChild(monster->progressBar); 33 | 34 | 35 | return monster; 36 | } 37 | 38 | void Monster::forwad() 39 | { 40 | currentIndex++; 41 | if (currentIndex < path.size()) 42 | { 43 | direction = (path.at(currentIndex)->getPosition() - path.at(currentIndex - 1)->getPosition()).getNormalized(); 44 | this->runAction(Sequence::create(MoveTo::create(getRunTime(), path.at(currentIndex)->getPosition()), 45 | CallFunc::create(CC_CALLBACK_0(Monster::forwad, this)), NULL)); 46 | } 47 | else { 48 | currentIndex = path.size(); 49 | direction.x = 0; 50 | direction.y = 0; 51 | } 52 | } 53 | float Monster::getRunTime() 54 | { 55 | Point p1 = path.at(currentIndex - 1)->getPosition(); 56 | Point p2 = path.at(currentIndex)->getPosition(); 57 | 58 | auto diff = p1.distance(p2); 59 | return diff / this->getSpeed(); 60 | } -------------------------------------------------------------------------------- /brick/carrot/Classes/Monster.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | class Monster : public Sprite { 5 | public: 6 | static Monster *create(std::string name, float speed, float blood, Vector); 7 | void forwad(); 8 | float getRunTime(); 9 | 10 | float totalBlood; 11 | Point direction; 12 | Sprite* bloodBar; 13 | ProgressTimer* progressBar; 14 | 15 | CC_SYNTHESIZE(float, speed, Speed); 16 | CC_SYNTHESIZE(float, blood, Blood); 17 | CC_SYNTHESIZE(int, currentIndex, CurrentIndex); 18 | CC_SYNTHESIZE(bool, isDead, IsDead); 19 | private: 20 | Vector path; 21 | }; -------------------------------------------------------------------------------- /brick/carrot/Classes/Tower.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/carrot/Classes/Tower.cpp -------------------------------------------------------------------------------- /brick/carrot/Classes/Tower.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/carrot/Classes/Tower.h -------------------------------------------------------------------------------- /brick/doudizhu/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "LoginScene.h" 3 | #include "GameManager.h" 4 | 5 | USING_NS_CC; 6 | 7 | static cocos2d::Size designResolutionSize = cocos2d::Size(960, 640); 8 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 9 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 10 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 11 | 12 | AppDelegate::AppDelegate() { 13 | 14 | } 15 | 16 | AppDelegate::~AppDelegate() 17 | { 18 | } 19 | 20 | //if you want a different context,just modify the value of glContextAttrs 21 | //it will takes effect on all platforms 22 | void AppDelegate::initGLContextAttrs() 23 | { 24 | //set OpenGL context attributions,now can only set six attributions: 25 | //red,green,blue,alpha,depth,stencil 26 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 27 | 28 | GLView::setGLContextAttrs(glContextAttrs); 29 | } 30 | 31 | // If you want to use packages manager to install more packages, 32 | // don't modify or remove this function 33 | static int register_all_packages() 34 | { 35 | return 0; //flag for packages manager 36 | } 37 | 38 | bool AppDelegate::applicationDidFinishLaunching() { 39 | // initialize director 40 | auto director = Director::getInstance(); 41 | auto glview = director->getOpenGLView(); 42 | if(!glview) { 43 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 44 | glview = GLViewImpl::createWithRect("doudizhu", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 45 | #else 46 | glview = GLViewImpl::create("doudizhu"); 47 | #endif 48 | director->setOpenGLView(glview); 49 | } 50 | 51 | // turn on display FPS 52 | director->setDisplayStats(true); 53 | 54 | // set FPS. the default value is 1.0/60 if you don't call this 55 | director->setAnimationInterval(1.0 / 60); 56 | 57 | // Set the design resolution 58 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::FIXED_HEIGHT); 59 | auto frameSize = glview->getFrameSize(); 60 | // if the frame's height is larger than the height of medium size. 61 | if (frameSize.height > mediumResolutionSize.height) 62 | { 63 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 64 | } 65 | // if the frame's height is larger than the height of small size. 66 | else if (frameSize.height > smallResolutionSize.height) 67 | { 68 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 69 | } 70 | // if the frame's height is smaller than the height of medium size. 71 | else 72 | { 73 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 74 | } 75 | 76 | register_all_packages(); 77 | 78 | // create a scene. it's an autorelease object 79 | GameManager::getInstance(); 80 | auto scene = LoginScene::createScene(); 81 | director->runWithScene(scene); 82 | 83 | return true; 84 | } 85 | 86 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 87 | void AppDelegate::applicationDidEnterBackground() { 88 | Director::getInstance()->stopAnimation(); 89 | 90 | // if you use SimpleAudioEngine, it must be pause 91 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 92 | } 93 | 94 | // this function will be called when the app is active again 95 | void AppDelegate::applicationWillEnterForeground() { 96 | Director::getInstance()->startAnimation(); 97 | 98 | // if you use SimpleAudioEngine, it must resume here 99 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 100 | } 101 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/DeckScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/DeckScene.cpp -------------------------------------------------------------------------------- /brick/doudizhu/Classes/DeckScene.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/DeckScene.h -------------------------------------------------------------------------------- /brick/doudizhu/Classes/GameManager.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/GameManager.cpp -------------------------------------------------------------------------------- /brick/doudizhu/Classes/GameManager.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/GameManager.h -------------------------------------------------------------------------------- /brick/doudizhu/Classes/GameRoomList.cpp: -------------------------------------------------------------------------------- 1 | #include "GameRoomList.h" 2 | 3 | 4 | bool GameRoomList::init() 5 | { 6 | if (Layer::init()) 7 | { 8 | return true; 9 | } 10 | return false; 11 | } 12 | 13 | TableViewCell * GameRoomList::tableCellAtIndex(TableView * table, ssize_t idx) 14 | { 15 | auto cell = table->dequeueCell(); 16 | if (!cell) 17 | { 18 | cell = TableViewCell::create(); 19 | auto label = Label::createWithTTF(TTFConfig("fonts/arial.ttf", 20), "Test", TextHAlignment::CENTER); 20 | label->ignoreAnchorPointForPosition(false); 21 | label->setAnchorPoint(Vec2::ZERO); 22 | label->setPosition(0, 0); 23 | 24 | cell->addChild(label); 25 | } 26 | return cell; 27 | } 28 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/GameRoomList.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "cocos-ext.h" 4 | USING_NS_CC; 5 | USING_NS_CC_EXT; 6 | class GameRoomList : public Layer, public TableViewDataSource, public TableViewDelegate 7 | { 8 | public: 9 | virtual bool init(); 10 | virtual void scrollViewDidScroll(ScrollView* view) {}; 11 | virtual void scrollViewDidZoom(ScrollView* view) {}; 12 | virtual void tableCellTouched(TableView* table, TableViewCell* cell) {}; 13 | virtual Size tableCellSizeForIndex(TableView *table, ssize_t idx) { return Size(300, 50); }; 14 | virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx); 15 | virtual ssize_t numberOfCellsInTableView(TableView *table) { return 20; }; 16 | 17 | CREATE_FUNC(GameRoomList); 18 | }; 19 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/GameRoomScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/GameRoomScene.cpp -------------------------------------------------------------------------------- /brick/doudizhu/Classes/GameRoomScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "cocos-ext.h" 4 | #include "ui/CocosGUI.h" 5 | USING_NS_CC; 6 | USING_NS_CC_EXT; 7 | 8 | class GameRoomScene : public Layer 9 | { 10 | public: 11 | 12 | enum LabelTag 13 | { 14 | ROOM_BACKGROUND = 100, 15 | ROOM_NAME, 16 | NUMBER_OF_PLAYERS, 17 | }; 18 | 19 | CREATE_FUNC(GameRoomScene); 20 | static Scene* createScene(); 21 | 22 | void enterRoom(); 23 | void createRoom(); 24 | void refreshRoom(); 25 | void refreshListView(); 26 | 27 | void roomListSelected(Ref* ref, ui::ListView::EventType type); 28 | void roomListScrolled(Ref* ref, ui::ScrollView::EventType type); 29 | 30 | virtual bool init(); 31 | virtual void update(float delta); 32 | 33 | int selectIndex; 34 | int listFlashCount; 35 | int enterFlashCount; 36 | bool isWaitingList; 37 | bool isWaitingEnter; 38 | Color3B selectColor; 39 | Size visible; 40 | Point origin; 41 | TableView* roomTable; 42 | Menu* mainMenu; 43 | ui::ListView* listView; 44 | }; 45 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/LoginScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/LoginScene.cpp -------------------------------------------------------------------------------- /brick/doudizhu/Classes/LoginScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | 5 | class LoginScene : public Layer 6 | { 7 | public: 8 | virtual bool init(); 9 | static Scene* createScene(); 10 | CREATE_FUNC(LoginScene); 11 | 12 | virtual bool onTouchBegan(Touch *touch, Event *event); 13 | virtual void onTouchMoved(Touch *touch, Event *event); 14 | virtual void onTouchEnded(Touch *touch, Event *event); 15 | 16 | void checkStatus(float time); 17 | 18 | TextFieldTTF *inputText; 19 | Sprite* loginBtn; 20 | int checkCount; 21 | bool isConnecting; 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/MainScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/MainScene.cpp -------------------------------------------------------------------------------- /brick/doudizhu/Classes/MainScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #ifndef __COCOS_2D_H__ 3 | #define __COCOS_2D_H__ 4 | #include "cocos2d.h" 5 | USING_NS_CC; 6 | #endif 7 | 8 | #include "cocos-ext.h" 9 | USING_NS_CC_EXT; 10 | class MainScene : public cocos2d::Layer 11 | { 12 | public: 13 | static Scene* createScene(); 14 | virtual bool init(); 15 | CREATE_FUNC(MainScene); 16 | 17 | void startGame(); 18 | void displayHelp(); 19 | void exitGame(); 20 | void backFromHelp(); 21 | 22 | protected: 23 | private: 24 | Size visible; 25 | Point origin; 26 | Sprite* helpSprite; 27 | Menu* mainMenu; 28 | 29 | }; 30 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/Player.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/Player.cpp -------------------------------------------------------------------------------- /brick/doudizhu/Classes/Player.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "PokerCard.h" 3 | #ifndef __COCOS_2D_H__ 4 | #define __COCOS_2D_H__ 5 | #include "cocos2d.h" 6 | USING_NS_CC; 7 | #endif 8 | class Player : public Sprite 9 | { 10 | public: 11 | enum PlayerType 12 | { 13 | ILLEGAL, 14 | DIZHU = 1, 15 | NONGMING, 16 | }; 17 | static Player* create(std::string, int roomPos); 18 | 19 | void setAttribute(int serverPos, std::vector &); 20 | void sortCard(); 21 | CC_SYNTHESIZE(int, serverPos, serverPos); 22 | CC_SYNTHESIZE(int, roomPos, RoomPos); //position in the room 23 | CC_SYNTHESIZE(PlayerType, ident, Ident); 24 | CC_SYNTHESIZE(std::string, playerName, PlayerName); 25 | CC_SYNTHESIZE(bool, ready, Ready); 26 | std::vector leftCard; 27 | std::vector outCard; 28 | protected: 29 | private: 30 | }; 31 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/PokerCard.cpp: -------------------------------------------------------------------------------- 1 | #include "PokerCard.h" 2 | 3 | PokerArrayUtil *PokerArrayUtil::instance = nullptr; 4 | std::map PokerArrayUtil::funMap = std::map(); //easy to get the counterpart function 5 | std::vector PokerArrayUtil::funList = std::vector(); //card type priority 6 | PokerCard * PokerCard::create(SpriteFrame* frame) 7 | { 8 | auto card = new PokerCard(); 9 | card->setSelect(false); 10 | card->initWithSpriteFrame(frame); 11 | card->autorelease(); 12 | return card; 13 | } 14 | 15 | 16 | /** count the length of each value, and sort them by the length 17 | values of the same length will be further sorted by their value **/ 18 | void PokerArray::sortByLength() 19 | { 20 | if (cardArray.size() <= 1) 21 | { 22 | return; 23 | } 24 | //sort the value firstly for secondary sort 25 | sortByValue(); 26 | 27 | //count the number of each value 28 | std::map map; 29 | for (auto card : cardArray) 30 | { 31 | int value = card->getValueInType(); 32 | if (map.find(value) == map.end()) 33 | { 34 | map[value] = 0; 35 | } 36 | map[value] = map[value] + 1; 37 | } 38 | 39 | //get all the key 40 | std::vector keyVector; 41 | for (auto element : map) 42 | { 43 | keyVector.push_back(element.first); 44 | } 45 | 46 | //sort the key and insert it into the vector 47 | int count = keyVector.size(); 48 | std::vector sortedKey; 49 | for (int i = 0; i < count; i++) 50 | { 51 | int max = 0; 52 | int maxIndex = 0; 53 | for (int j = 0; j < keyVector.size(); j++) 54 | { 55 | int key = keyVector.at(j); 56 | int value = map[key]; 57 | if (value > max) 58 | { 59 | max = value; 60 | maxIndex = j; 61 | } 62 | if (value == max) 63 | { 64 | if (key > keyVector.at(maxIndex)) 65 | { 66 | max = value; 67 | maxIndex = j; 68 | } 69 | } 70 | } 71 | int maxKey = keyVector.at(maxIndex); 72 | sortedKey.push_back(maxKey); 73 | keyVector.erase(keyVector.begin() + maxIndex); 74 | } 75 | 76 | //resort the card 77 | std::vector tmpCards(cardArray); 78 | cardArray.clear(); 79 | for (auto element : sortedKey) 80 | { 81 | for (auto card : tmpCards) 82 | { 83 | if (card->getValueInType() == element) 84 | { 85 | cardArray.push_back(card); 86 | } 87 | } 88 | } 89 | } 90 | 91 | void PokerArray::sortByValue() 92 | { 93 | sort(cardArray.begin(), cardArray.end(), PokerArrayUtil::compareByLocalValue); 94 | } 95 | 96 | PokerArray::PokerArray() 97 | { 98 | this->type = ILLEGAL; 99 | } 100 | 101 | PokerArray::PokerArray(std::vector cards) 102 | { 103 | this->type = ILLEGAL; 104 | for (auto card : cards) 105 | { 106 | cardArray.push_back(card); 107 | } 108 | PokerArrayUtil::judgeTypeOfCards(this); 109 | } 110 | 111 | 112 | void PokerArrayUtil::judgeTypeOfCards(PokerArray *pokerArray) 113 | { 114 | pokerArray->sortByValue(); 115 | 116 | for (int i = 0; i < funList.size(); i++) 117 | { 118 | auto fun = funList[i]; 119 | if (fun == PokerArrayType::PLANE_WITH_PAIR) 120 | { 121 | log("Sort the card by length!"); 122 | pokerArray->sortByLength(); 123 | } 124 | if (funMap[fun](pokerArray)) 125 | { 126 | break; 127 | } 128 | } 129 | } 130 | 131 | void PokerArrayUtil::initFunction() 132 | { 133 | funList.clear(); 134 | funMap.clear(); 135 | 136 | //function for sortByValue 137 | funList.push_back(PokerArrayType::QUEEN_BOMB); // == 2 138 | funList.push_back(PokerArrayType::MULTIPLE_PAIR); // >= 6 139 | funList.push_back(PokerArrayType::SEQUENCE); // >= 5 140 | funList.push_back(PokerArrayType::BOMB); // == 4 141 | funList.push_back(PokerArrayType::THREE_WITH_NONE); // == 3 142 | funList.push_back(PokerArrayType::PAIR); // == 2 143 | funList.push_back(PokerArrayType::SINGLE); // == 1 144 | 145 | //function for sortByLength 146 | funList.push_back(PokerArrayType::PLANE_WITH_PAIR); // >= 10 147 | funList.push_back(PokerArrayType::PLANE_WITH_SINGLE); // >= 8 148 | funList.push_back(PokerArrayType::BOMB_WITH_PAIR); // == 8 149 | funList.push_back(PokerArrayType::PLANE_WITH_NONE); // >= 6 150 | funList.push_back(PokerArrayType::BOMB_WITH_SINGLE); // == 6 151 | funList.push_back(PokerArrayType::THREE_WITH_PAIR); // ==5 152 | funList.push_back(PokerArrayType::THREE_WITH_SINGLE); // == 4 153 | 154 | 155 | funMap[PokerArrayType::SINGLE] = isSingle; 156 | funMap[PokerArrayType::QUEEN_BOMB] = isQueenBomb; 157 | funMap[PokerArrayType::PAIR] = isPair; 158 | funMap[PokerArrayType::THREE_WITH_NONE] = isThreeWithNone; 159 | funMap[PokerArrayType::BOMB] = isBomb; 160 | funMap[PokerArrayType::THREE_WITH_SINGLE] = isThreeWithSingle; 161 | funMap[PokerArrayType::THREE_WITH_PAIR] = isThreeWithPair; 162 | funMap[PokerArrayType::SEQUENCE] = isSequence; 163 | funMap[PokerArrayType::BOMB_WITH_SINGLE] = isBombWithSingle; 164 | funMap[PokerArrayType::MULTIPLE_PAIR] = isMultiplePair; 165 | funMap[PokerArrayType::PLANE_WITH_NONE] = isPlaneWithNone; 166 | funMap[PokerArrayType::BOMB_WITH_PAIR] = isBombWithPair; 167 | funMap[PokerArrayType::PLANE_WITH_SINGLE] = isPlaneWithSingle; 168 | funMap[PokerArrayType::PLANE_WITH_PAIR] = isPlaneWithPair; 169 | } 170 | 171 | bool PokerArrayUtil::isSingle(PokerArray *pokerArray) 172 | { 173 | if (pokerArray->cardArray.size() == 1) 174 | { 175 | pokerArray->type = PokerArrayType::SINGLE; 176 | pokerArray->lengthOfType = 1; 177 | return true; 178 | } 179 | return false; 180 | } 181 | 182 | bool PokerArrayUtil::isPair(PokerArray *pokerArray) 183 | { 184 | //two card with no queen 185 | if (pokerArray->cardArray.size() == 2) 186 | { 187 | if (pokerArray->cardArray.at(0)->getValueInType() == 188 | pokerArray->cardArray.at(1)->getValueInType() && 189 | !pokerArray->cardArray.at(0)->isQueen()) 190 | { 191 | pokerArray->type = PokerArrayType::PAIR; 192 | pokerArray->lengthOfType = 1; 193 | return true; 194 | } 195 | } 196 | return false; 197 | } 198 | 199 | bool PokerArrayUtil::isSequence(PokerArray *pokerArray) 200 | { 201 | if (pokerArray->cardArray.size() < 5) 202 | { 203 | return false; 204 | } 205 | //the first card should not be the queen 206 | if (!pokerArray->cardArray.at(0)->isQueen()) 207 | { 208 | int value = pokerArray->cardArray.at(0)->getValueInType(); 209 | for (int i = 1; i < pokerArray->cardArray.size(); i++) 210 | { 211 | if (value == 15) 212 | { 213 | return false; 214 | } 215 | auto nextValue = pokerArray->cardArray.at(i)->getValueInType(); 216 | if (value - nextValue != 1) 217 | { 218 | return false; 219 | } 220 | value = nextValue; 221 | } 222 | pokerArray->type = PokerArrayType::SEQUENCE; 223 | pokerArray->lengthOfType = pokerArray->cardArray.size(); 224 | return true; 225 | } 226 | return false; 227 | } 228 | 229 | bool PokerArrayUtil::isMultiplePair(PokerArray *pokerArray) 230 | { 231 | if (pokerArray->cardArray.size() >= 6 && pokerArray->cardArray.size() % 2 == 0) 232 | { 233 | if (!pokerArray->cardArray.at(0)->isQueen()) 234 | { 235 | int value = pokerArray->cardArray.at(0)->getValueInType(); 236 | for (int i = 0; i < pokerArray->cardArray.size(); i = i + 2) 237 | { 238 | int first = pokerArray->cardArray.at(i)->getValueInType(); 239 | int second = pokerArray->cardArray.at(i + 1)->getValueInType(); 240 | if (first != second) 241 | { 242 | return false; 243 | } 244 | if (i > 0) 245 | { 246 | if (value - first != 1) 247 | { 248 | return false; 249 | } 250 | value = first; 251 | } 252 | } 253 | pokerArray->type = PokerArrayType::MULTIPLE_PAIR; 254 | pokerArray->lengthOfType = pokerArray->cardArray.size() / 2; 255 | return true; 256 | } 257 | } 258 | return false; 259 | } 260 | 261 | /**Three card should be the same**/ 262 | bool PokerArrayUtil::isThreeWithNone(PokerArray *pokerArray) 263 | { 264 | if (pokerArray->cardArray.size() == 3) 265 | { 266 | if (pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(1)->getValueInType() && 267 | pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(2)->getValueInType()) 268 | { 269 | pokerArray->type = PokerArrayType::THREE_WITH_NONE; 270 | pokerArray->lengthOfType = 1; 271 | return true; 272 | } 273 | } 274 | return false; 275 | } 276 | 277 | /** call sortByLength first **/ 278 | bool PokerArrayUtil::isThreeWithSingle(PokerArray *pokerArray) 279 | { 280 | if (pokerArray->cardArray.size() == 4) 281 | { 282 | if (pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(1)->getValueInType() && 283 | pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(2)->getValueInType()) 284 | { 285 | pokerArray->type = PokerArrayType::THREE_WITH_SINGLE; 286 | pokerArray->lengthOfType = 1; 287 | return true; 288 | } 289 | } 290 | return false; 291 | } 292 | 293 | /** call sortByLength first **/ 294 | bool PokerArrayUtil::isThreeWithPair(PokerArray *pokerArray) 295 | { 296 | if (pokerArray->cardArray.size() == 5) 297 | { 298 | std::vector first(pokerArray->cardArray.begin(), pokerArray->cardArray.begin() + 3); 299 | std::vector second(pokerArray->cardArray.begin() + 3, pokerArray->cardArray.end()); 300 | PokerArray arry1(first); 301 | PokerArray arry2(second); 302 | if (isThreeWithNone(&arry1) && isPair(&arry2)) 303 | { 304 | pokerArray->type = PokerArrayType::THREE_WITH_PAIR; 305 | pokerArray->lengthOfType = 1; 306 | return true; 307 | } 308 | } 309 | return false; 310 | } 311 | 312 | /** call sortByLength first **/ 313 | bool PokerArrayUtil::isPlaneWithNone(PokerArray *pokerArray) 314 | { 315 | if (pokerArray->cardArray.size() >= 6 && pokerArray->cardArray.size() % 3 == 0) 316 | { 317 | int prevValue = pokerArray->cardArray.at(0)->getValueInType(); 318 | for (int i = 0; i < pokerArray->cardArray.size(); i += 3) 319 | { 320 | std::vector cards(pokerArray->cardArray.begin() + i, pokerArray->cardArray.begin() + i + 3); 321 | PokerArray array(cards); 322 | if (!isThreeWithNone(&array)) 323 | { 324 | return false; 325 | } 326 | if (i > 0) 327 | { 328 | int nextValue = pokerArray->cardArray.at(i)->getValueInType(); 329 | if (prevValue - nextValue != 1) 330 | { 331 | return false; 332 | } 333 | prevValue = nextValue; 334 | } 335 | 336 | } 337 | pokerArray->type = PokerArrayType::PLANE_WITH_NONE; 338 | pokerArray->lengthOfType = pokerArray->cardArray.size() / 3; 339 | return true; 340 | } 341 | return false; 342 | } 343 | 344 | /** call sortByLength first **/ 345 | bool PokerArrayUtil::isPlaneWithSingle(PokerArray *pokerArray) 346 | { 347 | if (pokerArray->cardArray.size() >= 8 && pokerArray->cardArray.size() % 4 == 0) 348 | { 349 | int prevValue = pokerArray->cardArray.at(0)->getValueInType(); 350 | int length = pokerArray->cardArray.size() / 4; 351 | for (int i = 0; i < length; i++) 352 | { 353 | std::vector cards(pokerArray->cardArray.begin() + i * 3, pokerArray->cardArray.begin() + i * 3 + 3); 354 | PokerArray array(cards); 355 | if (!isThreeWithNone(&array)) 356 | { 357 | return false; 358 | } 359 | if (i > 0) 360 | { 361 | int nextValue = pokerArray->cardArray.at(i * 3)->getValueInType(); 362 | if (prevValue - nextValue != 1) 363 | { 364 | return false; 365 | } 366 | prevValue = nextValue; 367 | } 368 | } 369 | pokerArray->type = PokerArrayType::PLANE_WITH_SINGLE; 370 | pokerArray->lengthOfType = length; 371 | return true; 372 | } 373 | return false; 374 | } 375 | 376 | /** call sortByLength first **/ 377 | bool PokerArrayUtil::isPlaneWithPair(PokerArray *pokerArray) 378 | { 379 | if (pokerArray->cardArray.size() >= 10 && pokerArray->cardArray.size() % 5 == 0) 380 | { 381 | int prevValue = pokerArray->cardArray.at(0)->getValueInType(); 382 | int length = pokerArray->cardArray.size() / 5; 383 | for (int i = 0; i < length; i++) 384 | { 385 | std::vector cards(pokerArray->cardArray.begin() + i * 3, pokerArray->cardArray.begin() + i * 3 + 3); 386 | PokerArray array(cards); 387 | if (!isThreeWithNone(&array)) 388 | { 389 | return false; 390 | } 391 | if (i > 0) 392 | { 393 | int nextValue = pokerArray->cardArray.at(i * 3)->getValueInType(); 394 | if (prevValue - nextValue != 1) 395 | { 396 | return false; 397 | } 398 | prevValue = nextValue; 399 | } 400 | } 401 | //pair 402 | int beginIndex = length * 3; 403 | for (int i = 0; i < length; i++) 404 | { 405 | std::vector cards(pokerArray->cardArray.begin() + beginIndex + i * 2, pokerArray->cardArray.begin() + beginIndex + i * 2 + 2); 406 | PokerArray array(cards); 407 | if (!isPair(&array)) 408 | { 409 | return false; 410 | } 411 | } 412 | pokerArray->type = PokerArrayType::PLANE_WITH_PAIR; 413 | pokerArray->lengthOfType = length; 414 | return true; 415 | } 416 | return false; 417 | } 418 | 419 | bool PokerArrayUtil::isBomb(PokerArray *pokerArray) 420 | { 421 | if (pokerArray->cardArray.size() == 4) 422 | { 423 | if (pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(1)->getValueInType() && 424 | pokerArray->cardArray.at(1)->getValueInType() == pokerArray->cardArray.at(2)->getValueInType() && 425 | pokerArray->cardArray.at(2)->getValueInType() == pokerArray->cardArray.at(3)->getValueInType()) 426 | { 427 | pokerArray->type = PokerArrayType::BOMB; 428 | pokerArray->lengthOfType = 1; 429 | return true; 430 | } 431 | } 432 | return false; 433 | } 434 | 435 | bool PokerArrayUtil::isBombWithSingle(PokerArray *pokerArray) 436 | { 437 | if (pokerArray->cardArray.size() == 6) 438 | { 439 | if (pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(1)->getValueInType() && 440 | pokerArray->cardArray.at(1)->getValueInType() == pokerArray->cardArray.at(2)->getValueInType() && 441 | pokerArray->cardArray.at(2)->getValueInType() == pokerArray->cardArray.at(3)->getValueInType()) 442 | { 443 | pokerArray->type = PokerArrayType::BOMB_WITH_SINGLE; 444 | pokerArray->lengthOfType = 1; 445 | return true; 446 | } 447 | } 448 | return false; 449 | } 450 | 451 | bool PokerArrayUtil::isBombWithPair(PokerArray *pokerArray) 452 | { 453 | if (pokerArray->cardArray.size() == 8) 454 | { 455 | if (pokerArray->cardArray.at(0)->getValueInType() == pokerArray->cardArray.at(1)->getValueInType() && 456 | pokerArray->cardArray.at(1)->getValueInType() == pokerArray->cardArray.at(2)->getValueInType() && 457 | pokerArray->cardArray.at(2)->getValueInType() == pokerArray->cardArray.at(3)->getValueInType() && 458 | pokerArray->cardArray.at(4)->getValueInType() == pokerArray->cardArray.at(5)->getValueInType() && 459 | pokerArray->cardArray.at(6)->getValueInType() == pokerArray->cardArray.at(7)->getValueInType()) 460 | { 461 | pokerArray->type = PokerArrayType::BOMB_WITH_PAIR; 462 | pokerArray->lengthOfType = 1; 463 | return true; 464 | } 465 | } 466 | return false; 467 | } 468 | 469 | bool PokerArrayUtil::isQueenBomb(PokerArray *pokerArray) 470 | { 471 | if (pokerArray->cardArray.size() == 2) 472 | { 473 | if (pokerArray->cardArray.at(1)->getValueInAll() >= 52) 474 | { 475 | pokerArray->type = PokerArrayType::QUEEN_BOMB; 476 | pokerArray->lengthOfType = 1; 477 | return true; 478 | } 479 | } 480 | return false; 481 | } 482 | 483 | bool PokerArrayUtil::compareByLocalValue(PokerCard *card1, PokerCard *card2) 484 | { 485 | if (card2->getValueInType() == card1->getValueInType()) 486 | { 487 | return card2->getType() < card1->getType(); 488 | } 489 | return card2->getValueInType() < card1->getValueInType(); 490 | } 491 | -------------------------------------------------------------------------------- /brick/doudizhu/Classes/PokerCard.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/Classes/PokerCard.h -------------------------------------------------------------------------------- /brick/doudizhu/server/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void root_handle(struct evhttp_request *req, void *arg) 9 | { 10 | printf("Access root!"); 11 | struct evbuffer *buf = evbuffer_new(); 12 | if(buf == NULL) 13 | { 14 | perror("Failed to create response"); 15 | } 16 | evbuffer_add_printf(buf, "Hello World!\n"); 17 | evhttp_send_reply(req, HTTP_OK, "Ok", buf); 18 | } 19 | 20 | void other_handle(struct evhttp_request *req, void *arg) 21 | { 22 | printf("Access other!"); 23 | char tmp[1024]; 24 | char output[2048] = "\0"; 25 | const char *uri = evhttp_request_uri(req); 26 | sprintf(tmp, "uri=%s\n", uri); 27 | strcat(output, tmp); 28 | 29 | char *decode_uri = evhttp_decode_uri(uri); 30 | sprintf(tmp, "decode_uri=%s\n", decode_uri); 31 | strcat(output, tmp); 32 | 33 | struct evkeyvalq params; 34 | evhttp_parse_query(decode_uri, ¶ms); 35 | sprintf(tmp, "q=%s\n", evhttp_find_header(¶ms, "q")); 36 | strcat(output, tmp); 37 | sprintf(tmp, "s=%s\n", evhttp_find_header(¶ms, "s")); 38 | strcat(output, tmp); 39 | free(decode_uri); 40 | 41 | char *post_data = (char *)EVBUFFER_DATA(req->input_buffer); 42 | sprintf(tmp, "post_data=%s\n", post_data); 43 | strcat(output, tmp); 44 | 45 | evhttp_add_header(req->output_headers, "Server", "localhost"); 46 | evhttp_add_header(req->output_headers, "Content-Type", "text/plain; charset=UTF-8"); 47 | evhttp_add_header(req->output_headers, "Connection", "close"); 48 | 49 | struct evbuffer *buf = evbuffer_new(); 50 | evbuffer_add_printf(buf, "Hello World\n%s\n", output); 51 | evhttp_send_reply(req, HTTP_OK, "Ok", buf); 52 | evbuffer_free(buf); 53 | 54 | } 55 | int main() 56 | { 57 | struct evhttp *httpd; 58 | event_init(); 59 | httpd = evhttp_start("0.0.0.0", 8989); 60 | evhttp_set_cb(httpd, "/", root_handle, NULL); 61 | evhttp_set_gencb(httpd, other_handle, NULL); 62 | 63 | event_dispatch(); 64 | evhttp_free(httpd); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /brick/doudizhu/server/head.cpp: -------------------------------------------------------------------------------- 1 | #include "head.h" 2 | #include 3 | 4 | map roomMap; 5 | map playerMap; 6 | map socketMap; 7 | GameRoom::GameRoom() 8 | { 9 | roomName = ""; 10 | currentPlayers = 0; 11 | isRunning = 0; 12 | memset(players, 0, sizeof(Player *) * ROOM_SIZE); 13 | for(int i = 1; i <= 54; i++) 14 | { 15 | randCards.push_back(i); 16 | } 17 | } 18 | GameRoom::GameRoom(string name) 19 | { 20 | roomName = name; 21 | currentPlayers = 0; 22 | isRunning = 0; 23 | memset(players, 0, sizeof(Player *) * ROOM_SIZE); 24 | for(int i = 1; i <= 54; i++) 25 | { 26 | randCards.push_back(i); 27 | } 28 | } 29 | 30 | Player::Player() 31 | { 32 | playerName = ""; 33 | room = NULL; 34 | identity = UNDEFINED; 35 | readBufLen = 0; 36 | remain_length = 0; 37 | fd = 0; 38 | position = -1; 39 | ready = false; 40 | called = robbed = false; 41 | } 42 | 43 | /** 加入房间 **/ 44 | GameRoom *Player::joinGame(string roomName) 45 | { 46 | if(roomMap.find(roomName) == roomMap.end()) 47 | { 48 | printf("没有房间%s!\n", roomName.data()); 49 | return NULL; 50 | } 51 | 52 | GameRoom *room = roomMap[roomName]; 53 | if(room->currentPlayers >= ROOM_SIZE) 54 | { 55 | printf("房间%s已经满员!\n", roomName.data()); 56 | return NULL; 57 | } 58 | for(int i = 0; i < ROOM_SIZE; i++) 59 | { 60 | if(room->players[i] == NULL) 61 | { 62 | printf("玩家%s加入房间%s\n", playerName.data(), roomName.data()); 63 | this->room = room; 64 | this->position = i; 65 | room->players[i] = this; 66 | room->currentPlayers = room->currentPlayers + 1; 67 | return room; 68 | } 69 | } 70 | return NULL; 71 | } 72 | 73 | /** 退出房间 **/ 74 | int Player::exitGame() 75 | { 76 | if(!this->room) 77 | { 78 | return NONE; 79 | } 80 | GameRoom *room = this->room; 81 | room->isRunning = false; 82 | 83 | //没人的话删除房间 84 | room->currentPlayers--; 85 | if(room->currentPlayers <= 0) 86 | { 87 | printf("删除房间%s\n", room->roomName.data()); 88 | roomMap.erase(room->roomName); 89 | delete room; 90 | } 91 | this->room = NULL; 92 | this->position = -1; 93 | this->ready = false; 94 | } 95 | -------------------------------------------------------------------------------- /brick/doudizhu/server/head.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define SERVER_HOST "0.0.0.0" 11 | #define SERVER_PORT 7777 12 | #define BUFFER_SIZE 2048 13 | #define ROOM_SIZE 3 14 | #define MAX_CALL_COUNT 4 15 | class GameRoom; 16 | class Player; 17 | extern map roomMap; 18 | extern map playerMap; 19 | extern map socketMap; 20 | enum RoomFlag 21 | { 22 | LEGAL = 0, 23 | NONE, 24 | EMPTY, 25 | FULL, 26 | }; 27 | enum PokerType 28 | { 29 | FANGKUAI = 1, 30 | MEIHUA, 31 | HONGXIN, 32 | HEITAO, 33 | DAXIAOWANG 34 | }; 35 | enum PlayerType 36 | { 37 | UNDEFINED, 38 | DIZHU = 1, 39 | NONGMING, 40 | }; 41 | enum MessageType 42 | { 43 | ILLEGAL = 0, 44 | GET_ROOM_LIST = 1, //获取房间列表 45 | GET_ROOM_LIST_RESULT, 46 | CREATE_PLAYER, //创建玩家 47 | CREATE_PLAYER_RESULT, 48 | CREATE_ROOM, //创建房间 49 | CREATE_ROOM_RESULT, 50 | ENTER_ROOM, //进入房间 51 | ENTER_ROOM_RESULT, 52 | ENTER_ROOM_OTHERS, //别人进入房间 53 | READY, //准备游戏 54 | READY_RESULT, 55 | READY_OTHERS, //别人准备 56 | CALL_DIZHU, //叫地主 57 | CALL_DIZHU_RESULT, 58 | CALL_DIZHU_OTHERS, //别人叫地主 59 | ROB_DIZHU, //抢地主 60 | ROB_DIZHU_RESULT, 61 | ROB_DIZHU_OTHERS, //别人抢地主 62 | OUT_CARD, //自己出牌 63 | OUT_CARD_RESULT, 64 | OUT_CARD_OTHERS, //别人出牌 65 | START_OUT_CARD, //叫完地主开始出牌 66 | START_GAME, //开始游戏只能够从服务器发出 67 | END_GAME, //游戏结束 68 | 69 | }; 70 | 71 | /** 客户端发送的消息格式 72 | * 长度 1字节 73 | * 类型 1字节 74 | * 获取房间列表: 75 | * NONE 76 | * 创建玩家 77 | * 玩家名字 78 | * 创建房间 79 | * 房间名称 80 | * 进入房间: 81 | * 房间名称 82 | * 准备游戏: 83 | * NONE 84 | * 叫地主: 85 | * 叫OR不叫 86 | * 抢地主: 87 | * 抢OR不抢 88 | * 出牌: 89 | * 牌列表,每张牌占用一个字节 90 | * 91 | * 92 | **/ 93 | 94 | /** 服务器发送的消息格式 95 | * 长度 1字节 96 | * 类型 1字节 97 | * 获取房间列表: 98 | * 房间名称 空格分割 99 | * 创建玩家 100 | * 失败OR成功 101 | * 创建房间: 102 | * 失败OR成功 103 | * 进入房间: 104 | * 其它玩家的名称 空格 准备状态 换行符 105 | * ... 106 | * 别人进入房间: 107 | * 玩家名称 空格 玩家位置 空格 准备状态 108 | * 准备游戏: 109 | * NONE 110 | * 别人准备游戏: 111 | * 玩家名称 112 | * 叫地主: 113 | * 失败OR成功 114 | * 别人叫地主: 115 | * 玩家名称 116 | * 出牌: 117 | * 成功或是失败 空格 牌列表:每张占用一个字节 118 | * 别人出牌: 119 | * 玩家名称 空格 牌列表 120 | * 开始出牌: 121 | * 玩家名称 122 | * 开始游戏: 123 | * 牌列表 124 | * 游戏结束: 125 | * 玩家 是否是赢家 剩余的牌列表 换行符 126 | * ... 127 | **/ 128 | 129 | class PokerCard 130 | { 131 | public: 132 | bool isQueen() 133 | { 134 | if(this->valueInAll >= 52) 135 | { 136 | return true; 137 | } 138 | return false; 139 | } 140 | int type; 141 | int valueInType; 142 | int valueInAll; 143 | }; 144 | class Player 145 | { 146 | public: 147 | int identity; //身份 148 | int readBufLen; 149 | int fd; //对应的socket描述符 150 | int remain_length; 151 | int position; 152 | bool ready; //是否准备 153 | bool called; //是否叫过地主 154 | bool robbed; //是否抢过地主 155 | char readBuf[BUFFER_SIZE]; //每个玩家都有一个消息缓冲区 156 | string playerName; 157 | GameRoom* room; 158 | 159 | Player(); 160 | GameRoom* joinGame(string roomName); 161 | int exitGame(); 162 | }; 163 | 164 | class GameRoom 165 | { 166 | public: 167 | string roomName; 168 | Player* players[3]; 169 | int currentPlayers; 170 | int isRunning; 171 | int currentCallIndex; //哪个人叫地主 172 | int currentRobIndex; //哪个人抢地主 173 | int currentRobCount; //目前抢了多少次地主 174 | int currentOutIndex; //哪个人出牌 175 | int currentOutType; //这一轮的出牌类型 176 | int currentRoundCount; //本轮的出牌次数 177 | int indexOfDizhu; //地主的下标 178 | vector randCards; 179 | GameRoom(); 180 | GameRoom(string name); 181 | }; 182 | 183 | Player* addPlayer(string name); 184 | void deletePlayer(Player* player); 185 | void sendRoomList(int fd); 186 | void handle_message(int fd); //处理用户消息 187 | void handle_get_room_list(Player *player); //获取房间列表处理函数 188 | void handle_create_player(Player* player); //玩家注册处理函数 189 | void handle_create_room(Player* player); //创建房间处理函数 190 | void handle_enter_room(Player *player); //进入房间处理函数 191 | void handle_ready(Player *player); //玩家准备处理函数 192 | void handle_call_dizhu(Player* player); //玩家叫地主 193 | void handle_rob_dizhu(Player* player); //玩家抢地主 194 | void handle_out_card(Player *player); //玩家出牌处理函数 195 | -------------------------------------------------------------------------------- /brick/doudizhu/server/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/doudizhu/server/main -------------------------------------------------------------------------------- /brick/doudizhu/server/main.cpp: -------------------------------------------------------------------------------- 1 | #include "head.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | void handle_accept(struct epoll_event, int epoll_fd); 15 | void handle_others(struct epoll_event, int epoll_fd); 16 | void sendMessage(int fd, int type, string message); 17 | void releasePlayer(int fd); 18 | void* handleInput(void *); 19 | 20 | int main() 21 | { 22 | struct sockaddr_in server; 23 | int listenfd; 24 | int epoll_fd; 25 | bool running = true; 26 | pthread_t pid; 27 | 28 | //设置监听套接字 29 | listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 30 | if(listenfd <= 0) 31 | { 32 | perror("Failed to create socket!"); 33 | return -1; 34 | } 35 | memset((char *)&server, 0 , sizeof(server)); 36 | server.sin_family = AF_INET; 37 | server.sin_addr.s_addr = inet_addr(SERVER_HOST); 38 | server.sin_port = htons(SERVER_PORT); 39 | 40 | if(bind(listenfd, (struct sockaddr*)&server, sizeof(struct sockaddr)) != 0) 41 | { 42 | printf("Failed to bind in %s:%d", SERVER_HOST, SERVER_PORT); 43 | return -1; 44 | } 45 | 46 | listen(listenfd, 10); 47 | 48 | roomMap["test"] = new GameRoom("test"); 49 | Player *play = new Player(); 50 | play->fd = -1; 51 | play->playerName = "testUser"; 52 | play->joinGame("test"); 53 | socketMap[-1] = play; 54 | playerMap["testUser"] = play; 55 | 56 | //初始化epoll 57 | srand(time(NULL)); 58 | epoll_fd = epoll_create(100); 59 | if(epoll_fd <= 0) 60 | { 61 | perror("Falied to create epoll fd!"); 62 | close(listenfd); 63 | return -1; 64 | } 65 | struct epoll_event ev; 66 | ev.data.fd = listenfd; 67 | ev.events = EPOLLIN; 68 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listenfd, &ev); 69 | 70 | //创建后台进程监听用户输入 71 | pthread_create(&pid, NULL, handleInput, NULL); 72 | //循环监听事件 73 | struct epoll_event events[100]; 74 | while(running) 75 | { 76 | int numbers = epoll_wait(epoll_fd, events, 100, -1); 77 | int i = 0; 78 | for(; i < numbers; i++) 79 | { 80 | if(events[i].data.fd == listenfd) 81 | { 82 | //新的连接 83 | handle_accept(events[i], epoll_fd); 84 | } 85 | else 86 | { 87 | //接收数据 88 | handle_others(events[i], epoll_fd); 89 | } 90 | } 91 | } 92 | } 93 | 94 | void* handleInput(void *arg) 95 | { 96 | char cmd[128]; 97 | pthread_detach(pthread_self()); 98 | while(1) 99 | { 100 | memset(cmd, 0, sizeof(cmd)); 101 | scanf("%s", cmd); 102 | if(0 == strcmp(cmd, "room")) 103 | { 104 | printf("当前的房间列表如下:\n"); 105 | printf("%10s|%10s|%10s\n", "Name", "Count", "Running"); 106 | map::iterator ite = roomMap.begin(); 107 | while(ite != roomMap.end()) 108 | { 109 | printf("%10s|%10d|%10d\n", ite->second->roomName.data(), 110 | ite->second->currentPlayers, 111 | ite->second->isRunning); 112 | ite++; 113 | } 114 | } 115 | else if(0 == strcmp(cmd, "player")) 116 | { 117 | printf("当前的玩家列表如下:\n"); 118 | printf("%15s|%10s|%10s|%10s\n", "Description", "Name", "Room", "Identity"); 119 | map::iterator ite = socketMap.begin(); 120 | while(ite != socketMap.end()) 121 | { 122 | Player* player = ite->second; 123 | printf("%15d|%10s|%10s|%10d\n", player->fd, 124 | player->playerName.data(), player->room->roomName.data(), 125 | player->identity); 126 | ite++; 127 | } 128 | } 129 | else if(0 == strcmp(cmd, "clear")) 130 | { 131 | system("reset"); 132 | } 133 | } 134 | } 135 | void sendMessage(int fd, int type, string message) 136 | { 137 | int length = message.size() + 2; 138 | if(length >= 255) 139 | { 140 | printf("发给%d的消息过长:%s\n", fd, message.c_str()); 141 | return; 142 | } 143 | message = (char)type + message; 144 | message = (char)length + message; 145 | 146 | printf("准备发送类型为%d的消息给%d,长度%ld\n", type, fd, message.size()); 147 | // printf("消息内容:"); 148 | // for(unsigned long i = 0; i < message.size(); i++) 149 | // { 150 | // printf("%d ", (int)message.at(i)); 151 | // } 152 | // printf("\n"); 153 | length = 0; 154 | int res = 0; 155 | while(length < (int)message.size()) 156 | { 157 | res = send(fd, message.data() + length, message.size() - length, 0); 158 | if(res < 0) 159 | { 160 | perror(""); 161 | //断开连接 删除用户和对应的资源 162 | Player* player = socketMap[fd]; 163 | if(!player) 164 | { 165 | printf("描述符%d不合法\n", fd); 166 | } 167 | else 168 | { 169 | printf("玩家:%d名称:%s断开连接\n", fd, player->playerName.data()); 170 | releasePlayer(fd); 171 | } 172 | return; 173 | } 174 | length += res; 175 | } 176 | // printf("消息长度%d已经发送\n", (int)message.size()); 177 | } 178 | 179 | void sendRoomList(int fd) 180 | { 181 | string message = ""; 182 | map::iterator ite = roomMap.begin(); 183 | while(ite != roomMap.end()) 184 | { 185 | stringstream ss; 186 | string str; 187 | message.append(ite->first); 188 | message.append(" "); 189 | 190 | ss << ite->second->currentPlayers; 191 | ss >> str; 192 | message.append(str); 193 | message.append(" "); 194 | 195 | ss.clear(); 196 | ss << ite->second->isRunning; 197 | ss >> str; 198 | message.append(str); 199 | message.append("\n"); 200 | ite++; 201 | } 202 | sendMessage(fd, GET_ROOM_LIST_RESULT, message); 203 | } 204 | 205 | void handle_message(int fd) 206 | { 207 | if(socketMap.find(fd) == socketMap.end()) 208 | { 209 | printf("没有对应的玩家%d\n", fd); 210 | return; 211 | } 212 | 213 | Player *player = socketMap[fd]; 214 | int messageType = player->readBuf[0]; 215 | printf("收到来自玩家%s类型为%d的消息\n", player->playerName.data(), messageType); 216 | switch(messageType){ 217 | case GET_ROOM_LIST: 218 | handle_get_room_list(player); 219 | break; 220 | case ENTER_ROOM: 221 | handle_enter_room(player); 222 | break; 223 | case CREATE_PLAYER: 224 | handle_create_player(player); 225 | break; 226 | case CREATE_ROOM: 227 | handle_create_room(player); 228 | break; 229 | case READY: 230 | handle_ready(player); 231 | break; 232 | case CALL_DIZHU: 233 | handle_call_dizhu(player); 234 | break; 235 | case ROB_DIZHU: 236 | handle_rob_dizhu(player); 237 | break; 238 | case OUT_CARD: 239 | //handle_out_card(player); 240 | break; 241 | } 242 | //清空玩家的缓冲区 243 | player->readBufLen = 0; 244 | 245 | } 246 | 247 | void handle_get_room_list(Player* player) 248 | { 249 | sendRoomList(player->fd); 250 | } 251 | 252 | /** 253 | * 成功返回1 失败返回0 254 | **/ 255 | void handle_create_player(Player* player) 256 | { 257 | string name = player->readBuf + 1; 258 | //如果存在相同名字的玩家 259 | if(playerMap.find(name) != playerMap.end()) 260 | { 261 | printf("玩家名字与%d重复\n", playerMap[name]->fd); 262 | sendMessage(player->fd, CREATE_PLAYER_RESULT, 263 | "0"); 264 | } 265 | else 266 | { 267 | printf("玩家%s创建成功\n", name.data()); 268 | playerMap[name] = player; 269 | player->playerName = name; 270 | sendMessage(player->fd, CREATE_PLAYER_RESULT, 271 | "1"); 272 | } 273 | 274 | // printf("当前的房间有%ld个", roomMap.size()); 275 | // printf("当前的用户有%ld个:", socketMap.size()); 276 | // map::iterator ite = socketMap.begin(); 277 | // while(ite != socketMap.end()) 278 | // { 279 | // string name = ite->second->playerName; 280 | // printf("%s ", name.data()); 281 | // ite++; 282 | // } 283 | // printf("\n"); 284 | } 285 | void handle_create_room(Player* player) 286 | { 287 | //缓冲区的第一个字节是消息类型 288 | string name = player->readBuf + 1; 289 | if(roomMap.find(name) != roomMap.end()) 290 | { 291 | printf("创建房间失败,房间%s已经存在\n", name.data()); 292 | sendMessage(player->fd, CREATE_ROOM_RESULT, "0"); 293 | return; 294 | } 295 | GameRoom *room = new GameRoom(name); 296 | roomMap[name] = room; 297 | room = player->joinGame(name); 298 | 299 | //成功的话返回:房间名称 换行 在房间中的位置 300 | //否则返回:-1 301 | string message; 302 | if(room) 303 | { 304 | stringstream ss; 305 | ss << player->position; 306 | ss >> message; 307 | message = name + "\n" + message; 308 | printf("用户%s创建房间%s成功\n", player->playerName.data(), name.data()); 309 | } 310 | else 311 | { 312 | message = "-1"; 313 | printf("用户%s创建房间%s失败\n", player->playerName.data(), name.data()); 314 | player->exitGame(); 315 | } 316 | sendMessage(player->fd, CREATE_ROOM_RESULT, message); 317 | 318 | } 319 | void handle_enter_room(Player* player) 320 | { 321 | //缓冲区的第一个字节是消息类型 322 | string name = player->readBuf + 1; 323 | GameRoom *room = player->joinGame(name); 324 | if(room) 325 | { 326 | //要发送两类信息 一个是给当前用户 另一个提醒其他用户有人进入 327 | //message2 只有一行 玩家名称 空格 位置 空格 是否准备 328 | //message1 第一行返回房间名称 第二行返回在房间中的位置 然后每一行一个用户 329 | string message1 = player->room->roomName + "\n"; 330 | stringstream ss; 331 | string str; 332 | ss << player->position; 333 | ss >> str; 334 | message1.append(str + "\n"); 335 | 336 | string message2 = player->playerName + " "; 337 | str.clear(); 338 | ss.clear(); 339 | ss << player->position; 340 | ss >> str; 341 | message2.append(" " + str + " 0\n"); 342 | for(int i = 0; i < ROOM_SIZE; i++) 343 | { 344 | Player* tmp = room->players[i]; 345 | if(tmp) 346 | { 347 | if(tmp->fd != player->fd) 348 | { 349 | //先给每个小伙伴发送一条进入房间的消息 350 | sendMessage(tmp->fd, ENTER_ROOM_OTHERS, message2); 351 | } 352 | //然后增加其它玩家名称 位置 是否准备 353 | message1.append(tmp->playerName + " "); 354 | ss.clear(); 355 | str.clear(); 356 | ss << tmp->position; 357 | ss >> str; 358 | message1.append(str + " "); 359 | if(tmp->ready) 360 | { 361 | message1.append("1\n"); 362 | } 363 | else 364 | { 365 | message1.append("0\n"); 366 | } 367 | } 368 | } 369 | printf("玩家%s加入房间%s成功\n", 370 | player->playerName.data(), name.data()); 371 | sendMessage(player->fd, ENTER_ROOM_RESULT, message1); 372 | } 373 | else 374 | { 375 | printf("玩家%s加入房间%s失败\n", 376 | player->playerName.data(), name.data()); 377 | sendMessage(player->fd, ENTER_ROOM_RESULT, ""); 378 | } 379 | } 380 | 381 | void handle_ready(Player* player) 382 | { 383 | if(!player->room) 384 | { 385 | player->exitGame(); 386 | printf("用户%s不在房间%s\n", player->playerName.data(), 387 | player->room->roomName.data()); 388 | sendMessage(player->fd, READY_RESULT, "0"); 389 | return; 390 | } 391 | 392 | player->ready = true; 393 | sendMessage(player->fd, READY_RESULT, "1"); 394 | GameRoom* room = player->room; 395 | int readyCount = 0; 396 | for(int i = 0; i < ROOM_SIZE; i++) 397 | { 398 | Player* play = room->players[i]; 399 | if(play && play->fd != player->fd) 400 | { 401 | sendMessage(play->fd, READY_OTHERS, player->playerName); 402 | } 403 | if(play && play->ready) 404 | { 405 | readyCount++; 406 | } 407 | } 408 | //如果所有人都准备了 开始游戏 409 | if(readyCount == ROOM_SIZE) 410 | { 411 | //因为牌的下标有0 会截断 所以牌是从1开始 412 | string cards = ""; 413 | random_shuffle(room->randCards.begin(), room->randCards.end()); 414 | for(int i = 0; i < 54; i++) 415 | { 416 | cards.push_back((char)room->randCards.at(i)); 417 | } 418 | //给每个玩家发送开始游戏消息 419 | for(int i = 0; i < ROOM_SIZE; i++) 420 | { 421 | Player* play = room->players[i]; 422 | if(play) 423 | { 424 | sendMessage(play->fd, START_GAME, cards); } 425 | } 426 | room->currentPlayers = 0; 427 | room->currentOutType = 0; 428 | room->currentOutIndex = 0; 429 | room->currentRobCount = 0; 430 | room->currentRobIndex = 0; 431 | room->currentCallIndex = 0; 432 | room->currentRoundCount = 0; 433 | } 434 | } 435 | 436 | /** 437 | * 每次接收一个socket,创建一个临时Player 438 | **/ 439 | void handle_accept(struct epoll_event ev, int epoll_fd) 440 | { 441 | int listen_fd = ev.data.fd; 442 | if(ev.events == EPOLLIN) 443 | { 444 | //接收并发送房间列表 445 | struct sockaddr_in client; 446 | socklen_t len = 0; 447 | memset((char *)&client, 0, sizeof(client)); 448 | int fd = accept(listen_fd, (struct sockaddr*)&client, &len); 449 | if(fd > 0) 450 | { 451 | printf("新的客户连接 %s\n", inet_ntoa(client.sin_addr)); 452 | } 453 | struct epoll_event new_event; 454 | new_event.data.fd = fd; 455 | new_event.events = EPOLLIN; 456 | epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &new_event); 457 | 458 | //创建一个没有名字的玩家,并放入map中 459 | Player* player = new Player(); 460 | socketMap[fd] = player; 461 | player->fd = fd; 462 | } 463 | } 464 | 465 | void handle_others(struct epoll_event ev, int epoll_fd) 466 | { 467 | int fd = ev.data.fd; 468 | printf("处理客户%d的消息\n", fd); 469 | if(socketMap.find(fd) == socketMap.end()) 470 | { 471 | printf("无效的客户端%d\n", fd); 472 | releasePlayer(fd); 473 | return; 474 | } 475 | Player* player = socketMap[fd]; 476 | if(ev.events == EPOLLIN) 477 | { 478 | //读取新的消息 479 | if(player->remain_length == 0) 480 | { 481 | char buf[1]; 482 | int len = recv(fd, buf, 1, 0); 483 | if(len == 0) 484 | { 485 | printf("玩家%s好像失联\n", player->playerName.data()); 486 | ev.events = EPOLLIN; 487 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); 488 | releasePlayer(fd); 489 | return; 490 | } 491 | if(len < 0) 492 | { 493 | ev.events = EPOLLIN; 494 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); 495 | printf("玩家%s断开!\n", player->playerName.data()); 496 | releasePlayer(fd); 497 | return; 498 | } 499 | //将数据读取到玩家的缓冲区中,并更新相应的计数 500 | else 501 | { 502 | player->remain_length = buf[0]; 503 | len = read(fd, player->readBuf, player->remain_length); 504 | player->remain_length -= len; 505 | player->readBufLen += len; 506 | } 507 | 508 | } 509 | //否则继续读取上一次的数据 510 | else 511 | { 512 | int len = read(fd, player->readBuf + player->readBufLen, player->remain_length); 513 | if(len <= 0) 514 | { 515 | printf("玩家%s好像失联\n", player->playerName.data()); 516 | ev.events = EPOLLIN; 517 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); 518 | releasePlayer(fd); 519 | return; 520 | } 521 | player->remain_length -= len; 522 | player->readBufLen += len; 523 | } 524 | 525 | //读完了进行处理 526 | if(player->remain_length == 0) 527 | { 528 | player->readBuf[player->readBufLen] = '\0'; 529 | if(player->readBufLen > 0) 530 | { 531 | handle_message(fd); 532 | } 533 | player->readBufLen = 0; 534 | } 535 | } 536 | else 537 | { 538 | printf("不同的事件类型%d\n", ev.events); 539 | ev.events = EPOLLIN; 540 | epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, &ev); 541 | releasePlayer(fd); 542 | } 543 | } 544 | 545 | void releasePlayer(int fd) 546 | { 547 | close(fd); 548 | if(socketMap.find(fd) == socketMap.end()) 549 | { 550 | printf("玩家已经退出,%d\n", fd); 551 | return; 552 | } 553 | Player *player = socketMap[fd]; 554 | if(player) 555 | { 556 | player->exitGame(); 557 | socketMap.erase(fd); 558 | playerMap.erase(player->playerName); 559 | delete player; 560 | } 561 | } 562 | 563 | void handle_call_dizhu(Player* player) 564 | { 565 | string msg(player->readBuf + 1); 566 | GameRoom *room = player->room; 567 | if(player->position == room->currentCallIndex) 568 | { 569 | //叫了地主 首先给其他人发送提示 570 | //然后提醒下一个人抢地主 571 | //回复叫地主成功或是失败 572 | sendMessage(player->fd, CALL_DIZHU_RESULT, msg); 573 | for(int i = 0; i < ROOM_SIZE; i++) 574 | { 575 | if(!room->players[i]) 576 | { 577 | printf("ERROR:房间里玩家不够\n"); 578 | break; 579 | } 580 | if(player != room->players[i]) 581 | { 582 | //提醒其他玩家 格式为:叫或是不叫 玩家名称 583 | sendMessage(room->players[i]->fd, 584 | CALL_DIZHU_OTHERS, msg + player->playerName); 585 | } 586 | } 587 | if(msg == "1") 588 | { 589 | room->currentOutIndex = player->position; 590 | } 591 | 592 | room->currentRobCount++; 593 | //还可以继续叫地主 594 | if(room->currentRobCount < MAX_CALL_COUNT) 595 | { 596 | room->currentRobIndex = (room->currentCallIndex + 1) % ROOM_SIZE; 597 | room->currentCallIndex = -1; 598 | } 599 | //如果叫地主的次数够了 开始出牌 600 | else 601 | { 602 | room->currentRobIndex = -1; 603 | for(int i = 0; i < ROOM_SIZE; i++) 604 | { 605 | sendMessage(room->players[i]->fd, START_OUT_CARD, 606 | room->players[room->currentOutIndex]->playerName); 607 | } 608 | } 609 | } 610 | else 611 | { 612 | printf("Error:玩家%s不能叫地主\n", player->playerName.data()); 613 | sendMessage(player->fd, CALL_DIZHU_RESULT, "0"); 614 | } 615 | } 616 | 617 | void handle_rob_dizhu(Player* player) 618 | { 619 | string msg(player->readBuf + 1); 620 | GameRoom* room = player->room; 621 | if(room->currentRobIndex == player->position) 622 | { 623 | //回复抢地主成功或是失败 624 | sendMessage(player->fd, CALL_DIZHU_RESULT, msg); 625 | for(int i = 0; i < ROOM_SIZE; i++) 626 | { 627 | if(!room->players[i]) 628 | { 629 | printf("ERROR:房间里玩家不够\n"); 630 | break; 631 | } 632 | if(player != room->players[i]) 633 | { 634 | //提醒其他玩家 格式为:叫或是不叫 玩家名称 635 | sendMessage(room->players[i]->fd, 636 | CALL_DIZHU_OTHERS, msg + player->playerName); 637 | } 638 | } 639 | if(msg == "1") 640 | { 641 | room->currentOutIndex = player->position; 642 | } 643 | 644 | room->currentRobCount++; 645 | //还可以继续叫地主 646 | if(room->currentRobCount < MAX_CALL_COUNT) 647 | { 648 | room->currentRobIndex = (room->currentCallIndex + 1) % ROOM_SIZE; 649 | room->currentCallIndex = -1; 650 | } 651 | //如果叫地主的次数够了 开始出牌 652 | else 653 | { 654 | room->currentRobIndex = -1; 655 | for(int i = 0; i < ROOM_SIZE; i++) 656 | { 657 | sendMessage(room->players[i]->fd, START_OUT_CARD, 658 | room->players[room->currentOutIndex]->playerName); 659 | } 660 | } 661 | } 662 | else 663 | { 664 | printf("Error:玩家%s不能抢地主\n", player->playerName.data()); 665 | sendMessage(player->fd, CALL_DIZHU_RESULT, "0"); 666 | } 667 | } 668 | -------------------------------------------------------------------------------- /brick/doudizhu/server/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.9~svn20110310 // 7 | BUFFER_SIZE head.h 11;" d 8 | CREATE_PLAYER head.h /^ CREATE_PLAYER, \/\/创建玩家$/;" e enum:MessageType 9 | CREATE_PLAYER_RESULT head.h /^ CREATE_PLAYER_RESULT,$/;" e enum:MessageType 10 | CREATE_ROOM head.h /^ CREATE_ROOM, \/\/创建房间$/;" e enum:MessageType 11 | CREATE_ROOM_RESULT head.h /^ CREATE_ROOM_RESULT,$/;" e enum:MessageType 12 | DAXIAOWANG head.h /^ DAXIAOWANG$/;" e enum:PokerType 13 | DIZHU head.h /^ DIZHU = 1,$/;" e enum:PlayerType 14 | EMPTY head.h /^ EMPTY,$/;" e enum:RoomFlag 15 | END_GAME head.h /^ END_GAME, \/\/游戏结束$/;" e enum:MessageType 16 | ENTER_ROOM head.h /^ ENTER_ROOM, \/\/进入房间$/;" e enum:MessageType 17 | ENTER_ROOM_OTHERS head.h /^ ENTER_ROOM_OTHERS, \/\/别人进入房间$/;" e enum:MessageType 18 | ENTER_ROOM_RESULT head.h /^ ENTER_ROOM_RESULT,$/;" e enum:MessageType 19 | FANGKUAI head.h /^ FANGKUAI = 1,$/;" e enum:PokerType 20 | FULL head.h /^ FULL,$/;" e enum:RoomFlag 21 | GET_ROOM_LIST head.h /^ GET_ROOM_LIST = 1, \/\/获取房间列表$/;" e enum:MessageType 22 | GET_ROOM_LIST_RESULT head.h /^ GET_ROOM_LIST_RESULT,$/;" e enum:MessageType 23 | GameRoom head.cpp /^GameRoom::GameRoom()$/;" f class:GameRoom 24 | GameRoom head.cpp /^GameRoom::GameRoom(string name)$/;" f class:GameRoom 25 | GameRoom head.h /^class GameRoom$/;" c 26 | HEITAO head.h /^ HEITAO,$/;" e enum:PokerType 27 | HONGXIN head.h /^ HONGXIN,$/;" e enum:PokerType 28 | ILLEGAL head.h /^ ILLEGAL = 0,$/;" e enum:MessageType 29 | LEGAL head.h /^ LEGAL = 0,$/;" e enum:RoomFlag 30 | MEIHUA head.h /^ MEIHUA,$/;" e enum:PokerType 31 | MessageType head.h /^enum MessageType$/;" g 32 | NONE head.h /^ NONE,$/;" e enum:RoomFlag 33 | NONGMING head.h /^ NONGMING,$/;" e enum:PlayerType 34 | OUT_CARD head.h /^ OUT_CARD, \/\/自己出牌$/;" e enum:MessageType 35 | OUT_CARD_OTHERS head.h /^ OUT_CARD_OTHERS, \/\/别人出牌$/;" e enum:MessageType 36 | OUT_CARD_RESULT head.h /^ OUT_CARD_RESULT,$/;" e enum:MessageType 37 | Player head.cpp /^Player::Player()$/;" f class:Player 38 | Player head.h /^class Player$/;" c 39 | PlayerType head.h /^enum PlayerType$/;" g 40 | PokerCard head.h /^class PokerCard$/;" c 41 | PokerType head.h /^enum PokerType$/;" g 42 | READY head.h /^ READY, \/\/准备游戏$/;" e enum:MessageType 43 | READY_OTHERS head.h /^ READY_OTHERS, \/\/别人准备$/;" e enum:MessageType 44 | READY_RESULT head.h /^ READY_RESULT,$/;" e enum:MessageType 45 | RoomFlag head.h /^enum RoomFlag$/;" g 46 | SERVER_HOST head.h 9;" d 47 | SERVER_PORT head.h 10;" d 48 | START_GAME head.h /^ START_GAME, \/\/开始游戏只能够从服务器发出$/;" e enum:MessageType 49 | UNDEFINED head.h /^ UNDEFINED,$/;" e enum:PlayerType 50 | addPlayer head.cpp /^Player* addPlayer(string name)$/;" f 51 | currentPlayers head.h /^ int currentPlayers;$/;" m class:GameRoom 52 | exitGame head.cpp /^int Player::exitGame()$/;" f class:Player 53 | fd head.h /^ int fd; \/\/对应的socket描述符$/;" m class:Player 54 | handle_accept main.cpp /^void handle_accept(struct epoll_event ev, int epoll_fd)$/;" f 55 | handle_create_player main.cpp /^void handle_create_player(Player* player)$/;" f 56 | handle_create_room main.cpp /^void handle_create_room(Player* player)$/;" f 57 | handle_enter_room main.cpp /^void handle_enter_room(Player* player)$/;" f 58 | handle_get_room_list main.cpp /^void handle_get_room_list(Player* player)$/;" f 59 | handle_message main.cpp /^void handle_message(int fd)$/;" f 60 | handle_others main.cpp /^void handle_others(struct epoll_event ev, int epoll_fd)$/;" f 61 | handle_ready main.cpp /^void handle_ready(Player* player)$/;" f 62 | identity head.h /^ int identity; \/\/身份$/;" m class:Player 63 | isQueen head.h /^ bool isQueen()$/;" f class:PokerCard 64 | isRunning head.h /^ int isRunning;$/;" m class:GameRoom 65 | joinGame head.cpp /^GameRoom *Player::joinGame(string roomName)$/;" f class:Player 66 | main a.c /^int main()$/;" f 67 | main main.cpp /^int main()$/;" f 68 | other_handle a.c /^void other_handle(struct evhttp_request *req, void *arg)$/;" f 69 | playerMap head.cpp /^map playerMap;$/;" v 70 | playerName head.h /^ string playerName;$/;" m class:Player 71 | players head.h /^ Player* players[3];$/;" m class:GameRoom 72 | readBuf head.h /^ char readBuf[BUFFER_SIZE]; \/\/每个玩家都有一个消息缓冲区$/;" m class:Player 73 | readBufLen head.h /^ int readBufLen;$/;" m class:Player 74 | ready head.h /^ int ready; \/\/是否准备$/;" m class:Player 75 | remain_length head.h /^ int remain_length;$/;" m class:Player 76 | roomMap head.cpp /^map roomMap;$/;" v 77 | roomName head.h /^ string roomName;$/;" m class:Player 78 | roomName head.h /^ string roomName;$/;" m class:GameRoom 79 | root_handle a.c /^void root_handle(struct evhttp_request *req, void *arg)$/;" f 80 | sendMessage main.cpp /^void sendMessage(int fd, int type, string message)$/;" f 81 | sendRoomList main.cpp /^void sendRoomList(int fd)$/;" f 82 | socketMap head.cpp /^map socketMap;$/;" v 83 | type head.h /^ int type;$/;" m class:PokerCard 84 | valueInAll head.h /^ int valueInAll;$/;" m class:PokerCard 85 | valueInType head.h /^ int valueInType;$/;" m class:PokerCard 86 | -------------------------------------------------------------------------------- /brick/sanxiao/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "GameScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | static cocos2d::Size designResolutionSize = cocos2d::Size(480, 640); 7 | static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320); 8 | static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768); 9 | static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536); 10 | 11 | AppDelegate::AppDelegate() { 12 | 13 | } 14 | 15 | AppDelegate::~AppDelegate() 16 | { 17 | } 18 | 19 | //if you want a different context,just modify the value of glContextAttrs 20 | //it will takes effect on all platforms 21 | void AppDelegate::initGLContextAttrs() 22 | { 23 | //set OpenGL context attributions,now can only set six attributions: 24 | //red,green,blue,alpha,depth,stencil 25 | GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; 26 | 27 | GLView::setGLContextAttrs(glContextAttrs); 28 | } 29 | 30 | // If you want to use packages manager to install more packages, 31 | // don't modify or remove this function 32 | static int register_all_packages() 33 | { 34 | return 0; //flag for packages manager 35 | } 36 | 37 | bool AppDelegate::applicationDidFinishLaunching() { 38 | // initialize director 39 | auto director = Director::getInstance(); 40 | auto glview = director->getOpenGLView(); 41 | if(!glview) { 42 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) 43 | glview = GLViewImpl::createWithRect("sanxiao", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); 44 | #else 45 | glview = GLViewImpl::create("sanxiao"); 46 | #endif 47 | director->setOpenGLView(glview); 48 | } 49 | 50 | // turn on display FPS 51 | director->setDisplayStats(true); 52 | 53 | // set FPS. the default value is 1.0/60 if you don't call this 54 | director->setAnimationInterval(1.0 / 60); 55 | 56 | glview->setFrameSize(480, 640); 57 | // Set the design resolution 58 | glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); 59 | auto frameSize = glview->getFrameSize(); 60 | // if the frame's height is larger than the height of medium size. 61 | if (frameSize.height > mediumResolutionSize.height) 62 | { 63 | director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width)); 64 | } 65 | // if the frame's height is larger than the height of small size. 66 | else if (frameSize.height > smallResolutionSize.height) 67 | { 68 | director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width)); 69 | } 70 | // if the frame's height is smaller than the height of medium size. 71 | else 72 | { 73 | director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width)); 74 | } 75 | 76 | register_all_packages(); 77 | 78 | // create a scene. it's an autorelease object 79 | auto scene = GameScene::createScene(); 80 | 81 | // run 82 | director->runWithScene(scene); 83 | 84 | return true; 85 | } 86 | 87 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 88 | void AppDelegate::applicationDidEnterBackground() { 89 | Director::getInstance()->stopAnimation(); 90 | 91 | // if you use SimpleAudioEngine, it must be pause 92 | // SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); 93 | } 94 | 95 | // this function will be called when the app is active again 96 | void AppDelegate::applicationWillEnterForeground() { 97 | Director::getInstance()->startAnimation(); 98 | 99 | // if you use SimpleAudioEngine, it must resume here 100 | // SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); 101 | } 102 | -------------------------------------------------------------------------------- /brick/sanxiao/Classes/AppDelegate.h: -------------------------------------------------------------------------------- 1 | #ifndef _APP_DELEGATE_H_ 2 | #define _APP_DELEGATE_H_ 3 | 4 | #include "cocos2d.h" 5 | 6 | /** 7 | @brief The cocos2d Application. 8 | 9 | The reason for implement as private inheritance is to hide some interface call by Director. 10 | */ 11 | class AppDelegate : private cocos2d::Application 12 | { 13 | public: 14 | AppDelegate(); 15 | virtual ~AppDelegate(); 16 | 17 | virtual void initGLContextAttrs(); 18 | 19 | /** 20 | @brief Implement Director and Scene init code here. 21 | @return true Initialize success, app continue. 22 | @return false Initialize failed, app terminate. 23 | */ 24 | virtual bool applicationDidFinishLaunching(); 25 | 26 | /** 27 | @brief The function be called when the application enter background 28 | @param the pointer of the application 29 | */ 30 | virtual void applicationDidEnterBackground(); 31 | 32 | /** 33 | @brief The function be called when the application enter foreground 34 | @param the pointer of the application 35 | */ 36 | virtual void applicationWillEnterForeground(); 37 | }; 38 | 39 | #endif // _APP_DELEGATE_H_ 40 | 41 | -------------------------------------------------------------------------------- /brick/sanxiao/Classes/FruitSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "FruitSprite.h" 2 | 3 | std::vector FruitSprite::nameArray = {"h0", "h1", "h2", "h3", "h4"}; 4 | 5 | 6 | FruitSprite* FruitSprite::create(int row, int col, int type) { 7 | auto visible = Director::getInstance()->getVisibleSize(); 8 | auto origin = Director::getInstance()->getVisibleOrigin(); 9 | auto sprite = new FruitSprite(); 10 | sprite->setRow(row); 11 | sprite->setCol(col); 12 | if (type < 0 || type >= nameArray.size()) { 13 | type = rand() % (nameArray.size()); 14 | } 15 | auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(nameArray[type]); 16 | sprite->setType(type); 17 | sprite->initWithSpriteFrame(frame); 18 | sprite->setLegal(true); 19 | sprite->setAnchorPoint(Vec2(0, 0)); 20 | sprite->autorelease(); 21 | 22 | int count = visible.width / sprite->getContentSize().width; 23 | auto rate = visible.width / count / sprite->getContentSize().width; 24 | sprite->setScale(rate); 25 | sprite->setContentSize(Size(visible.width / count, sprite->getContentSize().height * rate)); 26 | return sprite; 27 | } -------------------------------------------------------------------------------- /brick/sanxiao/Classes/FruitSprite.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | 5 | class FruitSprite : public Sprite { 6 | 7 | public: 8 | static FruitSprite* create(int row, int col, int type); 9 | static std::vector nameArray; 10 | 11 | CC_SYNTHESIZE(int, row, Row); 12 | CC_SYNTHESIZE(int, col, Col); 13 | CC_SYNTHESIZE(int, type, Type); 14 | CC_SYNTHESIZE(bool, legal, Legal); 15 | 16 | }; 17 | -------------------------------------------------------------------------------- /brick/sanxiao/Classes/GameScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/a364572/cocos2dx/3d6d796b287bb6dc5fe5440a38e677024e6a899c/brick/sanxiao/Classes/GameScene.cpp -------------------------------------------------------------------------------- /brick/sanxiao/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "FruitSprite.h" 4 | USING_NS_CC; 5 | using namespace std; 6 | class GameLine; 7 | class GameScene : public Layer { 8 | public: 9 | static Scene* createScene(); 10 | virtual bool init(); 11 | virtual void onEnterTransitionDidFinish(); 12 | void restartGame(Ref* ref); 13 | void initFruit(); 14 | void download(); 15 | float swapPosition(FruitSprite*, FruitSprite*); 16 | bool judgeAll(); 17 | int judgeInPosition(int row, int col); 18 | 19 | bool onTouchBegan(Touch *touch, Event *unused_event); 20 | void onTouchMoved(Touch *touch, Event *unused_event); 21 | void onTouchEnded(Touch *touch, Event *unused_event); 22 | FruitSprite *getSpriteFromPosition(Point); 23 | 24 | 25 | Size visible; 26 | Point origin; 27 | 28 | CREATE_FUNC(GameScene); 29 | 30 | 31 | 32 | private: 33 | int reference; 34 | int flashCount; 35 | bool isDown; 36 | FruitSprite *preFruit; 37 | EventListenerTouchOneByOne *listener; 38 | 39 | vector> fruits; 40 | }; -------------------------------------------------------------------------------- /brick/sanxiao/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | #include "SimpleAudioEngine.h" 3 | 4 | USING_NS_CC; 5 | 6 | Scene* HelloWorld::createScene() 7 | { 8 | // 'scene' is an autorelease object 9 | auto scene = Scene::create(); 10 | 11 | // 'layer' is an autorelease object 12 | auto layer = HelloWorld::create(); 13 | 14 | // add layer as a child to scene 15 | scene->addChild(layer); 16 | 17 | // return the scene 18 | return scene; 19 | } 20 | 21 | // on "init" you need to initialize your instance 22 | bool HelloWorld::init() 23 | { 24 | ////////////////////////////// 25 | // 1. super init first 26 | if ( !Layer::init() ) 27 | { 28 | return false; 29 | } 30 | 31 | auto visibleSize = Director::getInstance()->getVisibleSize(); 32 | Vec2 origin = Director::getInstance()->getVisibleOrigin(); 33 | 34 | ///////////////////////////// 35 | // 2. add a menu item with "X" image, which is clicked to quit the program 36 | // you may modify it. 37 | 38 | // add a "close" icon to exit the progress. it's an autorelease object 39 | auto closeItem = MenuItemImage::create( 40 | "CloseNormal.png", 41 | "CloseSelected.png", 42 | CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 43 | 44 | closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 45 | origin.y + closeItem->getContentSize().height/2)); 46 | 47 | // create menu, it's an autorelease object 48 | auto menu = Menu::create(closeItem, NULL); 49 | menu->setPosition(Vec2::ZERO); 50 | this->addChild(menu, 1); 51 | 52 | ///////////////////////////// 53 | // 3. add your codes below... 54 | 55 | // add a label shows "Hello World" 56 | // create and initialize a label 57 | 58 | auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); 59 | 60 | // position the label on the center of the screen 61 | label->setPosition(Vec2(origin.x + visibleSize.width/2, 62 | origin.y + visibleSize.height - label->getContentSize().height)); 63 | 64 | // add the label as a child to this layer 65 | this->addChild(label, 1); 66 | 67 | // add "HelloWorld" splash screen" 68 | auto sprite = Sprite::create("HelloWorld.png"); 69 | 70 | // position the sprite on the center of the screen 71 | sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 72 | 73 | // add the sprite as a child to this layer 74 | this->addChild(sprite, 0); 75 | 76 | return true; 77 | } 78 | 79 | 80 | void HelloWorld::menuCloseCallback(Ref* pSender) 81 | { 82 | Director::getInstance()->end(); 83 | 84 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 85 | exit(0); 86 | #endif 87 | } 88 | -------------------------------------------------------------------------------- /brick/sanxiao/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class HelloWorld : public cocos2d::Layer 7 | { 8 | public: 9 | static cocos2d::Scene* createScene(); 10 | 11 | virtual bool init(); 12 | 13 | // a selector callback 14 | void menuCloseCallback(cocos2d::Ref* pSender); 15 | 16 | // implement the "static create()" method manually 17 | CREATE_FUNC(HelloWorld); 18 | }; 19 | 20 | #endif // __HELLOWORLD_SCENE_H__ 21 | --------------------------------------------------------------------------------