├── README.md ├── daguye ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── HelloWorldScene.cpp │ └── HelloWorldScene.h └── Resources │ ├── CloseNormal.png │ ├── CloseSelected.png │ ├── HelloWorld.png │ ├── btn_exit.png │ ├── btn_gomain.png │ ├── btn_gotogame.png │ ├── btn_help.png │ ├── btn_start.png │ └── fonts │ └── Marker Felt.ttf ├── dz_yang ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── HelloWorldScene.cpp │ └── HelloWorldScene.h └── Resources │ ├── CloseNormal.png │ ├── CloseSelected.png │ ├── HelloWorld.png │ ├── bo.png │ └── fonts │ └── Marker Felt.ttf ├── fireblackill └── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── GameHelp.cpp │ ├── GameHelp.h │ ├── GameLayer.cpp │ ├── GameLayer.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ └── SnakeNode.h ├── koyoter ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── GameScene.cpp │ ├── GameScene.h │ ├── MenuScene.cpp │ ├── MenuScene.h │ ├── SnakeSprite.cpp │ └── SnakeSprite.h └── Resources │ ├── eat.wav │ ├── fonts │ └── Marker Felt.ttf │ ├── image.plist │ └── image.png ├── ldk1119 ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── BackgroundLayer.cpp │ ├── BackgroundLayer.h │ ├── GameLayer.cpp │ ├── GameLayer.h │ ├── GameScene.cpp │ ├── GameScene.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ ├── LoadingScene.cpp │ ├── LoadingScene.h │ ├── Number.cpp │ ├── Number.h │ ├── Snake.cpp │ ├── Snake.h │ ├── StatusLayer.cpp │ ├── StatusLayer.h │ ├── WelcomeLayer.cpp │ ├── WelcomeLayer.h │ ├── WelcomeScene.cpp │ └── WelcomeScene.h └── Resources │ ├── CloseNormal.png │ ├── CloseSelected.png │ ├── HelloWorld.png │ ├── fonts │ └── Marker Felt.ttf │ └── image │ ├── logo.png │ ├── ui.plist │ └── ui.png ├── pashan ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── HelloWorldScene.cpp │ └── HelloWorldScene.h └── Resources │ ├── HelloWorld.png │ ├── background.wav │ ├── effect.wav │ ├── egg.png │ ├── gameover.wav │ └── snake.png ├── small └── Classes │ ├── HelloWorldScene.cpp │ └── HelloWorldScene.h ├── start530 └── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── HelloWorldScene.cpp │ └── HelloWorldScene.h ├── waitingfy.com └── github.md ├── wind_zero ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── HelloWorldScene.cpp │ └── HelloWorldScene.h └── Resources │ ├── CloseNormal.png │ ├── CloseSelected.png │ ├── fonts │ └── Marker Felt.ttf │ ├── node_food.png │ └── node_snake.png ├── zhubingyi ├── Classes │ ├── AppDelegate.cpp │ ├── AppDelegate.h │ ├── File.cpp │ ├── File.h │ ├── GameLayer.cpp │ ├── GameLayer.h │ ├── GameScene.cpp │ ├── GameScene.h │ ├── HelloWorldScene.cpp │ ├── HelloWorldScene.h │ ├── Snake.cpp │ ├── Snake.h │ ├── SnakeHead.cpp │ └── SnakeHead.h └── Resources │ ├── BackGround.png │ ├── BackGround.psd │ ├── Button.png │ ├── Button.psd │ ├── Button2.png │ ├── CloseNormal.png │ ├── CloseSelected.png │ ├── Food.png │ ├── HelloWorld.png │ ├── Marker Felt.ttf │ ├── PauseButton.png │ ├── Play.png │ ├── Play.psd │ ├── PlayButton.png │ ├── PlayButton.psd │ ├── PlayButton1.png │ ├── PlayButton2.png │ ├── Snake.png │ ├── SnakeHead.png │ ├── StartScene.png │ ├── StartScene.psd │ ├── icon-114n.png │ ├── icon-120.png │ ├── icon-120n.png │ ├── icon-120n.psd │ ├── rePlayButton1.png │ └── rePlayButton2.png └── zuoeryuner └── Classes ├── AppDelegate.cpp ├── AppDelegate.h ├── Game.cpp ├── Game.h ├── GameHome.cpp └── GameHome.h /README.md: -------------------------------------------------------------------------------- 1 | ##CocoaChina 编程大战 贪吃蛇源码汇总 2 | --- 3 | ###活动地址: 4 | 5 | [Cocos2d-x版第一届在线编程大战——"进击的贪吃蛇"火爆开启!不服来战!!](http://www.cocoachina.com/bbs/read.php?tid=197370) 6 | 7 | ###获奖地址: 8 | 9 | [Cocos2d-x版第一届在线编程大战——"进击的贪吃蛇"获奖名单 ](http://www.cocoachina.com/bbs/read.php?tid=198951) 10 | 11 | ###感谢他们的分享(以下排名不分先后) 12 | 13 | **1.fireblackill** 14 | 15 | First blood!第一个参与的活动的童鞋。 16 | 17 | [http://www.cocoachina.com/bbs/read.php?tid=197630](http://www.cocoachina.com/bbs/read.php?tid=197630) 18 | 19 | **2.佐耳云儿** 20 | 21 | 没有简单地移植,而是加入了诸如“Label”的3.0的新特性。 22 | 23 | [http://www.cocoachina.com/bbs/read.php?tid=197615](http://www.cocoachina.com/bbs/read.php?tid=197615) 24 | 25 | **3. wind_zero** 26 | 27 | 很有逻辑的教程,很适合指导新手一步步独立完成贪吃蛇这个游戏。 28 | 29 | [http://www.cocoachina.com/bbs/read.php?tid=197882](http://www.cocoachina.com/bbs/read.php?tid=197882) 30 | 31 | **4. dz_yang** 32 | 33 | 热心的dz老师还改进了用户体验,实现了跨平台。另外给DZ老师的文采跪了! 34 | 35 | [http://www.cocoachina.com/bbs/read.php?tid=197950](http://www.cocoachina.com/bbs/read.php?tid=197950) 36 | 37 | **5.waitingfy.com** 38 | 39 | h5的贪吃蛇开源了!!!当然最牛x的是它的AI,算法! 40 | 41 | [http://www.cocoachina.com/bbs/read.php?tid=198038](http://www.cocoachina.com/bbs/read.php?tid=198038) 42 | 43 | **6.ldk1119** 44 | 45 | 黑白格什么的弱爆了,这自带美工瞬间叼炸天! 46 | 47 | [http://www.cocoachina.com/bbs/read.php?tid=198160](http://www.cocoachina.com/bbs/read.php?tid=198160) 48 | 49 | **7.star特530** 50 | 51 | 你还在用触摸么?呵呵。star哥哥教你怎么玩重力感应贪吃蛇。 52 | 53 | [http://www.cocoachina.com/bbs/read.php?tid=198178](http://www.cocoachina.com/bbs/read.php?tid=198178) 54 | 55 | **8.small** 56 | 57 | 言简意赅地抓出了2.x和3.0的差别。 58 | 59 | [http://www.cocoachina.com/bbs/read.php?tid=198626](http://www.cocoachina.com/bbs/read.php?tid=198626) 60 | 61 | **9.zhubingyi** 62 | 63 | 像素风来袭,从logo到主页面到游戏页面再到游戏结束画面,可以看出作者很用心在做这个游戏,同时也期待区分N/S级的贪吃蛇出炉。 64 | 65 | [http://www.cocoachina.com/bbs/read.php?tid=198698](http://www.cocoachina.com/bbs/read.php?tid=198698) 66 | 67 | **10.大姑爷** 68 | 69 | WP8的贪吃蛇,更可贵的是附上了比较少见WP8开发环境的搭建。 70 | 71 | [http://www.cocoachina.com/bbs/read.php?tid=198724](http://www.cocoachina.com/bbs/read.php?tid=198724) 72 | 73 | **11.koyoter** 74 | 75 | 采用了不一样的实现方式,有了眼睛和音效的贪吃蛇更萌了。 76 | 77 | [http://www.cocoachina.com/bbs/read.php?tid=198743](http://www.cocoachina.com/bbs/read.php?tid=198743) 78 | 79 | **12.pashan** 80 | 81 | 实现了电脑键盘控制贪吃蛇,哇咔咔,新技能get√。 82 | 83 | [http://www.cocoachina.com/bbs/read.php?tid=198902](http://www.cocoachina.com/bbs/read.php?tid=198902) 84 | -------------------------------------------------------------------------------- /daguye/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("hello"); 20 | director->setOpenGLView(glview); 21 | } 22 | 23 | // turn on display FPS 24 | director->setDisplayStats(true); 25 | 26 | // set FPS. the default value is 1.0/60 if you don't call this 27 | director->setAnimationInterval(1.0 / 60); 28 | 29 | // create a scene. it's an autorelease object 30 | auto scene = HelloWorld::createScene(); 31 | 32 | // run 33 | director->runWithScene(scene); 34 | 35 | return true; 36 | } 37 | 38 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 39 | void AppDelegate::applicationDidEnterBackground() { 40 | Director::getInstance()->stopAnimation(); 41 | 42 | // if you use SimpleAudioEngine, it must be pause 43 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 44 | } 45 | 46 | // this function will be called when the app is active again 47 | void AppDelegate::applicationWillEnterForeground() { 48 | Director::getInstance()->startAnimation(); 49 | 50 | // if you use SimpleAudioEngine, it must resume here 51 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 52 | } 53 | -------------------------------------------------------------------------------- /daguye/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /daguye/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Classes/HelloWorldScene.cpp -------------------------------------------------------------------------------- /daguye/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Classes/HelloWorldScene.h -------------------------------------------------------------------------------- /daguye/Resources/CloseNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/CloseNormal.png -------------------------------------------------------------------------------- /daguye/Resources/CloseSelected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/CloseSelected.png -------------------------------------------------------------------------------- /daguye/Resources/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/HelloWorld.png -------------------------------------------------------------------------------- /daguye/Resources/btn_exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/btn_exit.png -------------------------------------------------------------------------------- /daguye/Resources/btn_gomain.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/btn_gomain.png -------------------------------------------------------------------------------- /daguye/Resources/btn_gotogame.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/btn_gotogame.png -------------------------------------------------------------------------------- /daguye/Resources/btn_help.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/btn_help.png -------------------------------------------------------------------------------- /daguye/Resources/btn_start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/btn_start.png -------------------------------------------------------------------------------- /daguye/Resources/fonts/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/daguye/Resources/fonts/Marker Felt.ttf -------------------------------------------------------------------------------- /dz_yang/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("snake"); 20 | director->setOpenGLView(glview); 21 | } 22 | glview->setDesignResolutionSize(720, 480, ResolutionPolicy::SHOW_ALL); 23 | 24 | // turn on display FPS 25 | director->setDisplayStats(true); 26 | 27 | // set FPS. the default value is 1.0/60 if you don't call this 28 | director->setAnimationInterval(1.0 / 60); 29 | 30 | // create a scene. it's an autorelease object 31 | auto scene = HelloWorld::createScene(); 32 | 33 | // run 34 | director->runWithScene(scene); 35 | 36 | return true; 37 | } 38 | 39 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 40 | void AppDelegate::applicationDidEnterBackground() { 41 | Director::getInstance()->stopAnimation(); 42 | 43 | // if you use SimpleAudioEngine, it must be pause 44 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 45 | } 46 | 47 | // this function will be called when the app is active again 48 | void AppDelegate::applicationWillEnterForeground() { 49 | Director::getInstance()->startAnimation(); 50 | 51 | // if you use SimpleAudioEngine, it must resume here 52 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 53 | } 54 | -------------------------------------------------------------------------------- /dz_yang/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /dz_yang/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/dz_yang/Classes/HelloWorldScene.cpp -------------------------------------------------------------------------------- /dz_yang/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | #include 6 | 7 | class HelloWorld : public cocos2d::Layer 8 | { 9 | cocos2d::Vector snake; 10 | int direction; 11 | float speed; 12 | cocos2d::Sprite* apple; 13 | public: 14 | // there's no 'id' in cpp, so we recommend returning the class instance pointer 15 | static cocos2d::Scene* createScene(); 16 | 17 | // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 18 | virtual bool init(); 19 | 20 | // a selector callback 21 | void menuCloseCallback(cocos2d::Ref* pSender); 22 | 23 | // implement the "static create()" method manually 24 | 25 | void up_date(float dt); 26 | 27 | void move(int direction,int snake_ind); 28 | 29 | void testGameisOver(bool b); 30 | 31 | void testEat(); 32 | 33 | void reSch(); 34 | 35 | virtual void draw(cocos2d::Renderer* renderer, const kmMat4 &transform, bool transformUpdated) override; 36 | 37 | CREATE_FUNC(HelloWorld); 38 | }; 39 | 40 | #endif // __HELLOWORLD_SCENE_H__ 41 | -------------------------------------------------------------------------------- /dz_yang/Resources/CloseNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/dz_yang/Resources/CloseNormal.png -------------------------------------------------------------------------------- /dz_yang/Resources/CloseSelected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/dz_yang/Resources/CloseSelected.png -------------------------------------------------------------------------------- /dz_yang/Resources/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/dz_yang/Resources/HelloWorld.png -------------------------------------------------------------------------------- /dz_yang/Resources/bo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/dz_yang/Resources/bo.png -------------------------------------------------------------------------------- /dz_yang/Resources/fonts/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/dz_yang/Resources/fonts/Marker Felt.ttf -------------------------------------------------------------------------------- /fireblackill/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("My Game"); 20 | director->setOpenGLView(glview); 21 | } 22 | 23 | glview->setDesignResolutionSize(960, 640, ResolutionPolicy::SHOW_ALL); 24 | 25 | // turn on display FPS 26 | //director->setDisplayStats(true); 27 | director->setDisplayStats(false); 28 | 29 | // set FPS. the default value is 1.0/60 if you don't call this 30 | director->setAnimationInterval(1.0 / 60); 31 | 32 | // create a scene. it's an autorelease object 33 | auto scene = HelloWorld::createScene(); 34 | 35 | // run 36 | director->runWithScene(scene); 37 | 38 | return true; 39 | } 40 | 41 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 42 | void AppDelegate::applicationDidEnterBackground() { 43 | Director::getInstance()->stopAnimation(); 44 | 45 | // if you use SimpleAudioEngine, it must be pause 46 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 47 | } 48 | 49 | // this function will be called when the app is active again 50 | void AppDelegate::applicationWillEnterForeground() { 51 | Director::getInstance()->startAnimation(); 52 | 53 | // if you use SimpleAudioEngine, it must resume here 54 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 55 | } 56 | -------------------------------------------------------------------------------- /fireblackill/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /fireblackill/Classes/GameHelp.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GameHelp.cpp 3 | // Snake 4 | // 5 | // Created by fireblackill on 14-4-10. 6 | // 7 | // 8 | 9 | #include "GameHelp.h" 10 | #include "HelloWorldScene.h" 11 | 12 | USING_NS_CC; 13 | 14 | Scene* GameHelp::createScene() 15 | { 16 | auto scene = Scene::create(); 17 | auto layer = GameHelp::create(); 18 | scene->addChild(layer); 19 | return scene; 20 | } 21 | 22 | bool GameHelp::init() 23 | { 24 | if(!Layer::init()) 25 | { 26 | return false; 27 | } 28 | 29 | //创建帮助文字 30 | auto labelHelp = Label::create("Please click screen to start the game!", "宋体", 24); 31 | labelHelp->setPosition(Point(480,320)); 32 | this->addChild(labelHelp); 33 | 34 | //创建返回按钮 35 | auto labelBack = Label::create("MainMenu", "宋体", 24); 36 | auto uiBack = MenuItemLabel::create(labelBack, CC_CALLBACK_1(GameHelp::menuBackToMain, this)); 37 | uiBack->setPosition(Point(100,50)); 38 | 39 | auto menu = Menu::create(uiBack,NULL); 40 | menu->setPosition(Point::ZERO); 41 | this->addChild(menu); 42 | 43 | return true; 44 | 45 | } 46 | 47 | void GameHelp::menuBackToMain(cocos2d::Ref *pSender) 48 | { 49 | Director::getInstance()->replaceScene(HelloWorld::createScene()); 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- /fireblackill/Classes/GameHelp.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameHelp.h 3 | // Snake 4 | // 5 | // Created by fireblackill on 14-4-10. 6 | // 7 | // 8 | 9 | #ifndef __Snake__GameHelp__ 10 | #define __Snake__GameHelp__ 11 | 12 | #include 13 | #include "cocos2d.h" 14 | 15 | class GameHelp : public cocos2d::Layer 16 | { 17 | public: 18 | static cocos2d::Scene* createScene(); 19 | virtual bool init(); 20 | CREATE_FUNC(GameHelp); 21 | void menuBackToMain(cocos2d::Ref *pSender); 22 | }; 23 | 24 | #endif /* defined(__Snake__GameHelp__) */ 25 | -------------------------------------------------------------------------------- /fireblackill/Classes/GameLayer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GameLayer.cpp 3 | // Snake 4 | // 5 | // Created by fireblackill on 14-4-10. 6 | // 7 | // 8 | 9 | #include "GameLayer.h" 10 | #include "HelloWorldScene.h" 11 | 12 | USING_NS_CC; 13 | 14 | Scene* GameLayer::createScene() 15 | { 16 | auto scene = Scene::create(); 17 | auto layer = GameLayer::create(); 18 | scene->addChild(layer); 19 | return scene; 20 | } 21 | 22 | bool GameLayer::init() 23 | { 24 | if(!Layer::init()) 25 | { 26 | return false; 27 | } 28 | 29 | auto labelBack = Label::create("MainMenu", "宋体", 24); 30 | auto uiBack = MenuItemLabel::create(labelBack, CC_CALLBACK_1(GameLayer::menuBackToMain, this)); 31 | uiBack->setPosition(Point(900,200)); 32 | 33 | auto menu = Menu::create(uiBack, NULL); 34 | menu->setPosition(Point::ZERO); 35 | this->addChild(menu); 36 | 37 | m_head = new SnakeNode(); 38 | m_head->row = arc4random()%10; 39 | m_head->col = arc4random()%10; 40 | 41 | m_food = new SnakeNode(); 42 | m_food->row = arc4random()%10; 43 | m_head->col = arc4random()%10; 44 | 45 | auto listener1 = EventListenerTouchOneByOne::create(); 46 | listener1->setSwallowTouches(true); 47 | listener1->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this); 48 | 49 | _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this); 50 | 51 | this->schedule(schedule_selector(GameLayer::update), 0.5); 52 | 53 | return true; 54 | } 55 | 56 | void GameLayer::draw(cocos2d::Renderer *renderer, const kmMat4 &transform, bool transformUpdated) 57 | { 58 | //绘制直线 59 | glLineWidth(4); 60 | for (int i = 0; i<11; i++) 61 | { 62 | DrawPrimitives::drawLine(Point(0,i*64), Point(640,i*64)); 63 | DrawPrimitives::drawLine(Point(i*64,0), Point(i*64,640)); 64 | } 65 | 66 | //绘制蛇头 67 | DrawPrimitives::drawSolidRect(Point(m_head->col * 64 , m_head->row * 64 ), 68 | Point(m_head->col * 64 +64,m_head->row * 64 +64), 69 | Color4F(1.0f,0,0,1.0f)); 70 | 71 | //绘制蛋 72 | DrawPrimitives::drawSolidRect(Point(m_food->col * 64 , m_food->row * 64 ), 73 | Point(m_food->col * 64 +64,m_food->row * 64 +64), 74 | Color4F(0,0,1.0f,1.0f)); 75 | 76 | //绘制蛇身 77 | for(auto &sn : m_body) 78 | { 79 | DrawPrimitives::drawSolidRect(Point(sn->col * 64, sn->row * 64), 80 | Point(sn->col * 64 + 64, sn->row * 64 +64), 81 | Color4F(0,0,1.0f,1.0f)); 82 | } 83 | 84 | Layer::draw(renderer, transform, transformUpdated); 85 | } 86 | 87 | void GameLayer::update(float dt) 88 | { 89 | //蛇身每一段跟随前一段移动 90 | for(int i = m_body.size() -1 ;i>=0;i--) 91 | { 92 | SnakeNode* sn = m_body.at(i); 93 | 94 | if(i!=0) 95 | { 96 | SnakeNode* pre = m_body.at(i-1); 97 | sn->dir = pre->dir; 98 | sn->col = pre->col; 99 | sn->row = pre->row; 100 | } 101 | else 102 | { 103 | sn->dir = m_head->dir; 104 | sn->col = m_head->col; 105 | sn->row = m_head->row; 106 | } 107 | } 108 | 109 | //根据方向来让蛇头移动 110 | switch (m_head->dir) 111 | { 112 | case UP: 113 | m_head->row++; 114 | log("up"); 115 | if(m_head->row >=10) m_head->row = 0; 116 | break; 117 | case DOWN: 118 | m_head->row--; 119 | log("down"); 120 | if(m_head->row <0) m_head->row = 9; 121 | break; 122 | case RIGHT: 123 | m_head->col++; 124 | if(m_head->col >=10) m_head->col = 0; 125 | break; 126 | case LEFT: 127 | m_head->col--; 128 | if(m_head->col < 0) m_head->col = 9; 129 | break; 130 | default: 131 | break; 132 | } 133 | 134 | //和蛋的碰撞检测 135 | if(m_head->row == m_food->row && m_head->col == m_food->col) 136 | { 137 | //刷新蛋 138 | m_food->row = arc4random()%10; 139 | m_food->col = arc4random()%10; 140 | 141 | //设置新的尾巴的参数 142 | SnakeNode* sn = new SnakeNode(); 143 | SnakeNode* last = NULL; 144 | if(m_body.size() > 0) 145 | { 146 | last = m_body.back(); 147 | } 148 | else 149 | { 150 | last = m_head; 151 | } 152 | 153 | switch (last->dir) 154 | { 155 | case UP: 156 | sn->row = last->row - 1; 157 | sn->col = last->col; 158 | break; 159 | case DOWN: 160 | sn->row = last->row + 1; 161 | sn->col = last->col; 162 | break; 163 | case LEFT: 164 | sn->row = last->row; 165 | sn->col = last->col + 1; 166 | break; 167 | case RIGHT: 168 | sn->row = last->row; 169 | sn->col = last->col - 1; 170 | break; 171 | 172 | default: 173 | break; 174 | } 175 | //添加进身体的Vector中 176 | m_body.pushBack(sn); 177 | } 178 | } 179 | 180 | void GameLayer::menuBackToMain(cocos2d::Ref *pSender) 181 | { 182 | Director::getInstance()->replaceScene(HelloWorld::createScene()); 183 | } 184 | 185 | bool GameLayer::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) 186 | { 187 | Point tPos = touch->getLocation(); 188 | int nowRow = ((int)tPos.y)/64; 189 | int nowCol = ((int)tPos.x)/64; 190 | 191 | if(abs(nowRow - m_head->row) > abs(nowCol - m_head->col)) 192 | { 193 | //上下移动 194 | if(nowRow > m_head->row) 195 | { 196 | m_head->dir = UP; 197 | } 198 | else 199 | { 200 | m_head->dir = DOWN; 201 | } 202 | } 203 | else 204 | { 205 | //左右移动 206 | if(nowCol > m_head->col) 207 | { 208 | m_head->dir = RIGHT; 209 | } 210 | else 211 | { 212 | m_head->dir = LEFT; 213 | } 214 | } 215 | 216 | return true; 217 | } -------------------------------------------------------------------------------- /fireblackill/Classes/GameLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameLayer.h 3 | // Snake 4 | // 5 | // Created by fireblackill on 14-4-10. 6 | // 7 | // 8 | 9 | #ifndef __Snake__GameLayer__ 10 | #define __Snake__GameLayer__ 11 | 12 | #include 13 | #include "SnakeNode.h" 14 | 15 | class GameLayer: public cocos2d::Layer 16 | { 17 | private: 18 | 19 | SnakeNode* m_head; 20 | SnakeNode* m_food; 21 | cocos2d::Vector m_body; 22 | 23 | public: 24 | virtual bool init(); 25 | virtual void draw(cocos2d::Renderer *renderer, const kmMat4 &transform, bool transformUpdated); 26 | void update(float dt); 27 | static cocos2d::Scene* createScene(); 28 | CREATE_FUNC(GameLayer); 29 | void menuBackToMain(cocos2d::Ref *pSender); 30 | 31 | virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); 32 | 33 | }; 34 | 35 | #endif /* defined(__Snake__GameLayer__) */ 36 | -------------------------------------------------------------------------------- /fireblackill/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | #include "GameHelp.h" 3 | #include "GameLayer.h" 4 | 5 | USING_NS_CC; 6 | 7 | Scene* HelloWorld::createScene() 8 | { 9 | auto scene = Scene::create(); 10 | auto layer = HelloWorld::create(); 11 | scene->addChild(layer); 12 | return scene; 13 | } 14 | 15 | // on "init" you need to initialize your instance 16 | bool HelloWorld::init() 17 | { 18 | if ( !Layer::init() ) 19 | { 20 | return false; 21 | } 22 | 23 | Size visibleSize = Director::getInstance()->getVisibleSize(); 24 | Point origin = Director::getInstance()->getVisibleOrigin(); 25 | 26 | //创建文字按钮 27 | auto labelStart = Label::create("Start Game", "宋体", 24); 28 | auto labelHelp = Label::create("Game Help", "宋体", 24); 29 | auto labelExit = Label::create("Exit Game", "宋体", 24); 30 | 31 | auto uiStart = MenuItemLabel::create(labelStart, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 32 | uiStart->setTag(1); 33 | uiStart->setPosition(Point(100,200)); 34 | 35 | auto uiHelp = MenuItemLabel::create(labelHelp, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 36 | uiHelp->setTag(2); 37 | uiHelp->setPosition(Point(100,150)); 38 | 39 | auto uiExit = MenuItemLabel::create(labelExit, CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 40 | uiExit->setTag(3); 41 | uiExit->setPosition(Point(100,50)); 42 | 43 | auto menu = Menu::create(uiStart,uiHelp, uiExit, NULL); 44 | menu->setPosition(Point::ZERO); 45 | this->addChild(menu, 1); 46 | 47 | auto label = LabelTTF::create("Hello World", "Arial", 24); 48 | 49 | // position the label on the center of the screen 50 | label->setPosition(Point(origin.x + visibleSize.width/2, 51 | origin.y + visibleSize.height - label->getContentSize().height)); 52 | 53 | // add the label as a child to this layer 54 | this->addChild(label, 1); 55 | 56 | // add "HelloWorld" splash screen" 57 | auto sprite = Sprite::create("HelloWorld.png"); 58 | 59 | // position the sprite on the center of the screen 60 | sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); 61 | 62 | // add the sprite as a child to this layer 63 | this->addChild(sprite, 0); 64 | 65 | return true; 66 | } 67 | 68 | 69 | void HelloWorld::menuCloseCallback(Ref* pSender) 70 | { 71 | int i = dynamic_cast(pSender)->getTag(); 72 | switch (i) 73 | { 74 | case 1: 75 | log("go to Game"); 76 | Director::getInstance()->replaceScene(GameLayer::createScene()); 77 | break; 78 | case 2: 79 | log("go to Help"); 80 | Director::getInstance()->replaceScene(GameHelp::createScene()); 81 | break; 82 | case 3: 83 | case 4: 84 | { 85 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) 86 | MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 87 | return; 88 | #endif 89 | 90 | Director::getInstance()->end(); 91 | 92 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 93 | exit(0); 94 | #endif 95 | } 96 | break; 97 | default: 98 | break; 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /fireblackill/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | USING_NS_CC; 7 | 8 | class HelloWorld : public cocos2d::Layer 9 | { 10 | public: 11 | static cocos2d::Scene* createScene(); 12 | virtual bool init(); 13 | CREATE_FUNC(HelloWorld); 14 | void menuCloseCallback(Ref* pSender); 15 | }; 16 | 17 | #endif // __HELLOWORLD_SCENE_H__ 18 | -------------------------------------------------------------------------------- /fireblackill/Classes/SnakeNode.h: -------------------------------------------------------------------------------- 1 | // 2 | // SnakeNode.h 3 | // Snake 4 | // 5 | // Created by 童翾 on 14-4-10. 6 | // 7 | // 8 | 9 | #ifndef __Snake__SnakeNode__ 10 | #define __Snake__SnakeNode__ 11 | 12 | #include 13 | #include "cocos2d.h" 14 | 15 | enum DIR_DEF 16 | { 17 | UP = 1, 18 | DOWN, 19 | LEFT, 20 | RIGHT 21 | }; 22 | class SnakeNode: public cocos2d::Ref 23 | { 24 | 25 | public: 26 | DIR_DEF dir; 27 | int row, col; 28 | }; 29 | 30 | #endif /* defined(__Snake__SnakeNode__) */ 31 | -------------------------------------------------------------------------------- /koyoter/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "MenuScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | auto director = Director::getInstance(); 16 | auto glview = director->getOpenGLView(); 17 | if(!glview) { 18 | glview = GLView::create("Snake Game"); 19 | log("Snake Game"); 20 | #if (CC_TARGET_PLATFORM == OS_WINDOWS) 21 | glview->setFrameSize( 960 , 640 ); 22 | #endif 23 | director->setOpenGLView(glview); 24 | } 25 | 26 | director->setDisplayStats(true); 27 | director->setAnimationInterval(1.0 / 60); 28 | Size frameSize = glview->getFrameSize(); 29 | float designWidth,designHeight; 30 | if (frameSize.width / frameSize.height > 960 / 640) 31 | { 32 | designHeight = 640; 33 | designWidth = frameSize.width / ( frameSize.height / designHeight); 34 | } 35 | else 36 | { 37 | designWidth = 960; 38 | designHeight = frameSize.height / (frameSize.width / designWidth); 39 | } 40 | glview->setDesignResolutionSize(designWidth , designHeight , ResolutionPolicy::NO_BORDER ); 41 | 42 | auto scene = MenuScene::createScene(); 43 | director->runWithScene(scene); 44 | 45 | return true; 46 | } 47 | 48 | void AppDelegate::applicationDidEnterBackground() { 49 | Director::getInstance()->stopAnimation(); 50 | } 51 | 52 | void AppDelegate::applicationWillEnterForeground() { 53 | Director::getInstance()->startAnimation(); 54 | } 55 | -------------------------------------------------------------------------------- /koyoter/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /koyoter/Classes/GameScene.cpp: -------------------------------------------------------------------------------- 1 | #include "GameScene.h" 2 | #include "SnakeSprite.h" 3 | #include "SimpleAudioEngine.h" 4 | #include "MenuScene.h" 5 | 6 | USING_NS_CC; 7 | using namespace CocosDenshion; 8 | 9 | Scene* GameScene::createScene() { 10 | auto scene = Scene::create(); 11 | auto layer = GameScene::create(); 12 | scene->addChild(layer); 13 | return scene; 14 | } 15 | 16 | 17 | bool GameScene::init() { 18 | if(!Layer::init()){ 19 | return false; 20 | } 21 | // 随机数因子 22 | srand((unsigned)time(NULL)); 23 | SpriteFrameCache::getInstance()->addSpriteFramesWithFile("image.plist"); 24 | 25 | auto visibleSize = Director::getInstance()->getVisibleSize(); 26 | 27 | auto layerMain = Layer::create(); 28 | layerMain->setContentSize(Size(960 , 640)); 29 | layerMain->ignoreAnchorPointForPosition(false); 30 | layerMain->setPosition( visibleSize.width / 2 , visibleSize.height / 2 ); 31 | this->addChild(layerMain); 32 | 33 | // 游戏背景图(算什么背景图 不都是黑色么。) 34 | auto spriteBackground = Sprite::createWithSpriteFrameName("background.png"); 35 | spriteBackground->setPosition( 480 , 320 ); 36 | layerMain->addChild(spriteBackground); 37 | 38 | // 游戏地图 39 | layerGameMap = Layer::create(); 40 | layerGameMap->setContentSize(Size( 760 , 560 )); 41 | layerGameMap->setAnchorPoint(Point(0 ,0)); 42 | layerGameMap->setPosition( 100 , 19.5f ); 43 | spriteBackground->addChild(layerGameMap); 44 | 45 | // 添加食物 46 | spriteFood = Sprite::createWithSpriteFrameName("food.png"); 47 | spriteFood->setAnchorPoint(Point(0,0)); 48 | layerGameMap->addChild(spriteFood); 49 | 50 | // 创建默认蛇头部 51 | spriteHeader = SnakeSprite::create("snake-head-up.png"); 52 | spriteHeader->setTag(0); 53 | spriteHeader->setPosition( 40 , 0 ); 54 | layerGameMap->addChild(spriteHeader , 2); 55 | 56 | // 给蛇添加个尾巴 57 | spriteFooter = SnakeSprite::create(); 58 | spriteFooter->setPosition( 0 , 0 ); 59 | spriteFooter->setTag(1); 60 | layerGameMap->addChild(spriteFooter); 61 | 62 | // 时间分数 63 | labelScore = Label::create("Score: 0" , "" , 40); 64 | labelScore->setAnchorPoint(Point( 0 , .5f )); 65 | labelScore->setPosition(Point( 100 , 610 )); 66 | layerMain->addChild(labelScore); 67 | 68 | labelTime = Label::create("Time: 00:00:00" , "" , 35); 69 | labelTime->setAnchorPoint(Point( 0 , .5f )); 70 | labelTime->setPosition(Point( 620 , 610 )); 71 | layerMain->addChild(labelTime); 72 | 73 | // 各种菜单item初始化 代码恶心 请勿细看 74 | auto menuItemUp = MenuItemSprite::create(Sprite::createWithSpriteFrameName("up-normal.png") , 75 | Sprite::createWithSpriteFrameName("up-press.png") , CC_CALLBACK_1(GameScene::menuCloseCallback,this)); 76 | menuItemUp->setTag(UP); 77 | menuItemUp->setPosition( 50 , 80 ); 78 | 79 | auto menuItemDown = MenuItemSprite::create(Sprite::createWithSpriteFrameName("down-normal.png") , 80 | Sprite::createWithSpriteFrameName("down-press.png") , CC_CALLBACK_1(GameScene::menuCloseCallback,this)); 81 | menuItemDown->setTag(DOWN); 82 | menuItemDown->setPosition( visibleSize.width - 50 , 80 ); 83 | 84 | auto menuItemRight = MenuItemSprite::create(Sprite::createWithSpriteFrameName("right-normal.png") , 85 | Sprite::createWithSpriteFrameName("right-press.png") , CC_CALLBACK_1(GameScene::menuCloseCallback,this)); 86 | menuItemRight->setTag(RIGHT); 87 | menuItemRight->setPosition( visibleSize.width - 50 , 300 ); 88 | 89 | auto menuItemLeft = MenuItemSprite::create(Sprite::createWithSpriteFrameName("left-normal.png") , 90 | Sprite::createWithSpriteFrameName("left-press.png") , CC_CALLBACK_1(GameScene::menuCloseCallback,this)); 91 | menuItemLeft->setTag(LEFT); 92 | menuItemLeft->setPosition( 50 , 300 ); 93 | 94 | menuItemRestart = MenuItemFont::create("Restart" , CC_CALLBACK_1(GameScene::menuCloseCallback,this)); 95 | menuItemRestart->setTag(RESTART); 96 | menuItemRestart->setVisible(false); 97 | menuItemRestart->setPosition( visibleSize.width / 2 , visibleSize.height / 2 ); 98 | 99 | menuItemMenu = MenuItemFont::create("ReturnMenu" , CC_CALLBACK_1(GameScene::menuCloseCallback,this)); 100 | menuItemMenu->setTag(MENU); 101 | menuItemMenu->setVisible(false); 102 | menuItemMenu->setPosition( visibleSize.width / 2 , visibleSize.height / 2 - 50 ); 103 | 104 | auto menuMain = Menu::create(menuItemUp , menuItemDown , menuItemRight , menuItemLeft ,menuItemRestart ,menuItemMenu , NULL); 105 | menuMain->setPosition(Point::ZERO); 106 | this->addChild(menuMain); 107 | 108 | start(); 109 | 110 | return true; 111 | } 112 | 113 | void GameScene::menuCloseCallback(cocos2d::Ref* pSender) { 114 | MenuItem* item = (MenuItem*)pSender; 115 | DIRECTION direction = (DIRECTION)item->getTag(); 116 | switch (direction) { 117 | case UP: 118 | case DOWN: 119 | case RIGHT: 120 | case LEFT: 121 | changeDirection(direction); 122 | break; 123 | case RESTART: 124 | start(); 125 | break; 126 | case MENU: 127 | { 128 | auto scene = MenuScene::createScene(); 129 | Director::getInstance()->replaceScene(scene); 130 | } 131 | break; 132 | default: 133 | break; 134 | } 135 | } 136 | 137 | void GameScene::start() { 138 | menuItemRestart->setVisible(false); 139 | menuItemMenu->setVisible(false); 140 | for (int i = 2 ; i < bones ; i++) { 141 | layerGameMap->removeChildByTag(i); 142 | } 143 | gameScore = 0; // 分数 144 | bones = 2; // 头 尾 145 | gameTime = 0; // 时间 146 | labelTime->setString("Time: 00:00:00"); 147 | labelScore->setString("Score: 0"); 148 | isMoveEnd = false; 149 | currDirection = RIGHT; 150 | this->schedule(schedule_selector(GameScene::gameTask), UP_TIME); 151 | this->schedule(schedule_selector(GameScene::timeTask), 1.0f); 152 | createFood(); 153 | spriteHeader->setSpriteFrame("snake-head-right.png"); 154 | spriteHeader->setPosition( 40 , 0 ); 155 | spriteFooter->setPosition( 0 , 0 ); 156 | } 157 | 158 | void GameScene::gameTask(float t) { 159 | if(isMoveEnd){ return; }; 160 | check(); 161 | move(); 162 | } 163 | 164 | void GameScene::end() { 165 | this->unschedule(schedule_selector(GameScene::gameTask)); 166 | this->unschedule(schedule_selector(GameScene::timeTask)); 167 | menuItemRestart->setVisible(true); 168 | menuItemMenu->setVisible(true); 169 | auto userData = UserDefault::getInstance(); 170 | int lastScore = userData->getIntegerForKey("score" , 0); 171 | if(lastScore < gameScore){ 172 | userData->setIntegerForKey("score" , gameScore); 173 | userData->flush(); 174 | } 175 | } 176 | 177 | void GameScene::move() { 178 | isMoveEnd = true; 179 | for (int i = 0 ; i < bones ; i++) { 180 | auto snake = (SnakeSprite*)layerGameMap->getChildByTag(i); 181 | auto snakePoint = snake->getPosition(); 182 | SnakeSprite* preSnake = NULL; 183 | Point preSnakePoint; 184 | if(i == 0) { 185 | Point snakeCurrPoint; 186 | switch (currDirection) { 187 | case RIGHT: 188 | snakeCurrPoint = snakePoint + CCPoint(40 , 0); 189 | break; 190 | case LEFT: 191 | snakeCurrPoint = snakePoint + CCPoint(-40 , 0); 192 | break; 193 | case DOWN: 194 | snakeCurrPoint = snakePoint + CCPoint(0 , -40); 195 | break; 196 | case UP: 197 | snakeCurrPoint = snakePoint + CCPoint(0 , 40); 198 | break; 199 | default: 200 | break; 201 | } 202 | 203 | if(snakeCurrPoint.x >= 760 || snakeCurrPoint.x < 0 || snakeCurrPoint.y >= 560 || snakeCurrPoint.y < 0) { 204 | end(); 205 | return; 206 | } 207 | snake->setPosition(snakeCurrPoint); 208 | snake->setNextPoint(snakePoint); 209 | } else { 210 | preSnake = (SnakeSprite*)layerGameMap->getChildByTag(i - 1); 211 | preSnakePoint = preSnake->getNextPoint(); 212 | 213 | snake->setPosition(preSnakePoint); 214 | snake->setNextPoint(snakePoint); 215 | } 216 | } 217 | isMoveEnd = false; 218 | } 219 | 220 | void GameScene::check() { 221 | // 如果头部跟实物坐标一致则代表吃了一个食物 222 | if(spriteFood->getPosition() == spriteHeader->getPosition()) { 223 | SimpleAudioEngine::getInstance()->playEffect("eat.wav"); 224 | addBody(); 225 | createFood(); 226 | gameScore += 5; 227 | labelScore->setString(CCString::createWithFormat("Score: %d" , gameScore)->getCString()); 228 | } 229 | 230 | // 检查当前头部是否与身体节点重叠 重叠为自己咬自己 游戏结束 231 | for (int i = 1 ; i < bones ; i++) { 232 | auto spriteSnake = (SnakeSprite*)layerGameMap->getChildByTag(i); 233 | if(spriteHeader->getPosition() == spriteSnake->getPosition()) { 234 | end(); 235 | } 236 | } 237 | } 238 | 239 | void GameScene::addBody() { 240 | auto spriteBody = SnakeSprite::create(); 241 | spriteBody->setTag(bones); 242 | layerGameMap->addChild(spriteBody); 243 | bones++; 244 | } 245 | 246 | void GameScene::createFood() { 247 | // 760 / 40 560 / 40 248 | int xNumder = (19 * CCRANDOM_0_1()); 249 | int yNumder = (14 * CCRANDOM_0_1()); 250 | Point pointFood = Point(xNumder * 40 , yNumder * 40); 251 | // 防止食物生成在蛇身体节点上 252 | for (int i = 0 ; i < bones ; i++) { 253 | auto spriteSnake = (SnakeSprite*)layerGameMap->getChildByTag(i); 254 | if(spriteSnake->getPosition() == pointFood) { 255 | createFood(); 256 | return; 257 | } 258 | } 259 | spriteFood->setPosition( xNumder * 40 , yNumder * 40); 260 | } 261 | 262 | void GameScene::timeTask(float) { 263 | gameTime++; 264 | int shi,fen,miao; 265 | shi = gameTime / 3600; 266 | fen = gameTime / 60 % 60; 267 | miao = gameTime % 60; 268 | labelTime->setString(CCString::createWithFormat("Time: %02d:%02d:%02d" , shi , fen , miao)->getCString()); 269 | } 270 | 271 | void GameScene::onEnter() { 272 | Layer::onEnter(); 273 | SimpleAudioEngine::getInstance()->preloadEffect("eat.wav"); 274 | 275 | auto listener = EventListenerTouchOneByOne::create(); 276 | listener->onTouchBegan=CC_CALLBACK_2(GameScene::onTouchBegan , this); 277 | listener->onTouchEnded=CC_CALLBACK_2(GameScene::onTouchEnded,this); 278 | _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this); 279 | } 280 | 281 | void GameScene::onExit() { 282 | Layer::onExit(); 283 | SimpleAudioEngine::getInstance()->unloadEffect("eat.wav"); 284 | } 285 | 286 | bool GameScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event) { 287 | if(layerGameMap->getBoundingBox().containsPoint(touch->getLocation())){ 288 | return true; 289 | } 290 | return false; 291 | } 292 | 293 | void GameScene::onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *unused_event) { 294 | Point pointStart = pTouch->getStartLocation(); 295 | Point pointEnd = pTouch->getLocation(); 296 | int x = pointStart.x - pointEnd.x; 297 | int y = pointStart.y - pointEnd.y; 298 | if(abs(x) > abs(y)) { 299 | if(x + 100 > 0) { 300 | changeDirection(LEFT); 301 | } else { 302 | changeDirection(RIGHT); 303 | } 304 | } else { 305 | if(y + 100 > 0) { 306 | changeDirection(DOWN); 307 | } else { 308 | changeDirection(UP); 309 | } 310 | } 311 | } 312 | 313 | void GameScene::changeDirection(DIRECTION direction) { 314 | switch (direction) { 315 | case UP: 316 | if( currDirection == UP || currDirection == DOWN ){ return; } 317 | spriteHeader->setSpriteFrame("snake-head-up.png"); 318 | break; 319 | case DOWN: 320 | if( currDirection == UP || currDirection == DOWN ){ return; } 321 | spriteHeader->setSpriteFrame("snake-head-down.png"); 322 | break; 323 | case RIGHT: 324 | if( currDirection == RIGHT || currDirection == LEFT ){ return; } 325 | spriteHeader->setSpriteFrame("snake-head-right.png"); 326 | break; 327 | case LEFT: 328 | if( currDirection == RIGHT || currDirection == LEFT ){ return; } 329 | spriteHeader->setSpriteFrame("snake-head-left.png"); 330 | break; 331 | } 332 | currDirection = direction; 333 | } -------------------------------------------------------------------------------- /koyoter/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #ifndef _GAMESCENE_SCENE_H__ 2 | #define _GAMESCENE_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | enum DIRECTION 7 | { 8 | RIGHT = 0 , 9 | LEFT , 10 | DOWN , 11 | UP , 12 | RESTART , 13 | MENU 14 | }; 15 | 16 | #define UP_TIME .25f 17 | 18 | class GameScene : public cocos2d::Layer 19 | { 20 | 21 | public: 22 | static cocos2d::Scene* createScene(); 23 | virtual bool init(); 24 | CREATE_FUNC(GameScene); 25 | 26 | protected: 27 | virtual void onEnter(); 28 | virtual void onExit(); 29 | virtual bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event); 30 | virtual void onTouchEnded(cocos2d::Touch *pTouch, cocos2d::Event *pEvent); 31 | private: 32 | cocos2d::Layer* layerGameMap; 33 | cocos2d::Sprite* spriteFood; 34 | cocos2d::Sprite* spriteHeader; 35 | cocos2d::Sprite* spriteFooter; 36 | cocos2d::MenuItem* menuItemRestart; 37 | cocos2d::MenuItem* menuItemMenu; 38 | cocos2d::Label* labelScore; 39 | cocos2d::Label* labelTime; 40 | int gameScore; // 当前分数 41 | long gameTime; // 此局开始了多久 42 | 43 | // 因为更新时间是固定的 怕当前更新蛇坐标还没完成又开始更新可能导致问题 所以加个标识符 44 | // (看代码更容易理解) 45 | bool isMoveEnd; 46 | 47 | // 各种按钮CB函数 48 | void menuCloseCallback(cocos2d::Ref* pSender); 49 | 50 | // 默认头部和脚步 所以初始化会为2 51 | int bones; 52 | 53 | // 蛇前进的方向 54 | DIRECTION currDirection; 55 | void changeDirection(DIRECTION); 56 | 57 | // 开始结束 58 | void start(); 59 | void end(); 60 | 61 | // 一定时间的回调,用于移动蛇节点和检查游戏状态 62 | void gameTask(float); 63 | void timeTask(float); 64 | 65 | // 移动 66 | void move(); 67 | 68 | //检查是否自己咬自己 或者捡到食物 69 | void check(); 70 | 71 | // 就是改变食物坐标 没必要重复创建就复用了 72 | void createFood(); 73 | 74 | // 添加蛇身体节点 75 | void addBody(); 76 | }; 77 | 78 | #endif -------------------------------------------------------------------------------- /koyoter/Classes/MenuScene.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuScene.h" 2 | 3 | #include "GameScene.h" 4 | 5 | USING_NS_CC; 6 | 7 | Scene* MenuScene::createScene() 8 | { 9 | auto scene = Scene::create(); 10 | auto layer = MenuScene::create(); 11 | scene->addChild(layer); 12 | return scene; 13 | } 14 | 15 | bool MenuScene::init() 16 | { 17 | 18 | if(!Layer::init()) 19 | { 20 | return false; 21 | } 22 | 23 | Size visibleSize = Director::getInstance()->getVisibleSize(); 24 | Point origin = Director::getInstance()->getVisibleOrigin(); 25 | 26 | auto labelGameName = Label::create("Block Snake" , "" , 40); 27 | labelGameName->setAnchorPoint(Point(.5f , .5f)); 28 | labelGameName->setPosition( visibleSize.width / 2 , visibleSize.height - 200); 29 | this->addChild(labelGameName); 30 | 31 | auto menuStartGame = MenuItemFont::create("StartGame" , 32 | CC_CALLBACK_1(MenuScene::menuCloseCallback,this)); 33 | menuStartGame->setTag(1); 34 | menuStartGame->setPosition( visibleSize.width / 2 , visibleSize.height / 2 ); 35 | 36 | auto menuExitGame = MenuItemFont::create("ExitGame" , 37 | CC_CALLBACK_1(MenuScene::menuCloseCallback,this)); 38 | menuExitGame->setTag(2); 39 | menuExitGame->setPosition( visibleSize.width / 2 , visibleSize.height / 2 - 50 ); 40 | 41 | int lastScore = UserDefault::getInstance()->getIntegerForKey("score" , -1); 42 | if(lastScore > 0){ 43 | auto labelScore = Label::create(String::createWithFormat("High Score: %d" , lastScore)->getCString() , "" , 25); 44 | labelScore->setAnchorPoint(Point(.5f , .5f)); 45 | labelScore->setPosition( visibleSize.width / 2 , 200); 46 | this->addChild(labelScore); 47 | } 48 | 49 | auto menu = Menu::create(menuStartGame , menuExitGame , NULL); 50 | menu->setPosition(Point::ZERO); 51 | this->addChild(menu); 52 | 53 | return true; 54 | } 55 | 56 | void MenuScene::menuCloseCallback(cocos2d::Ref* pSender) 57 | { 58 | auto menuItem = (MenuItem*)pSender; 59 | switch (menuItem->getTag()) 60 | { 61 | case 1: 62 | { 63 | menuItem->setEnabled(false); 64 | auto gameScene = GameScene::createScene(); 65 | Director::getInstance()->replaceScene(gameScene); 66 | break; 67 | } 68 | case 2: 69 | { 70 | Director::getInstance()->end(); 71 | 72 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 73 | exit(0); 74 | #endif 75 | } 76 | default: 77 | break; 78 | } 79 | } -------------------------------------------------------------------------------- /koyoter/Classes/MenuScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __MENUSCENE_SCENE_H__ 2 | #define __MENUSCENE_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class MenuScene : public cocos2d::Layer 7 | { 8 | public: 9 | static cocos2d::Scene* createScene(); 10 | virtual bool init(); 11 | CREATE_FUNC(MenuScene); 12 | 13 | private: 14 | void menuCloseCallback(cocos2d::Ref* pSender); 15 | }; 16 | 17 | #endif -------------------------------------------------------------------------------- /koyoter/Classes/SnakeSprite.cpp: -------------------------------------------------------------------------------- 1 | #include "SnakeSprite.h" 2 | 3 | USING_NS_CC; 4 | 5 | bool SnakeSprite::init() 6 | { 7 | if(!Sprite::initWithSpriteFrameName("snake-body.png")){ 8 | return false; 9 | } 10 | this->setAnchorPoint(Point::ZERO); 11 | return true; 12 | } 13 | 14 | SnakeSprite* SnakeSprite::create(const char* file) 15 | { 16 | SnakeSprite* spriteSnake = new SnakeSprite; 17 | if(spriteSnake->initWithSpriteFrameName(file)){ 18 | spriteSnake->setAnchorPoint(Point::ZERO); 19 | spriteSnake->autorelease(); 20 | return spriteSnake; 21 | } 22 | CC_SAFE_RELEASE_NULL(spriteSnake); 23 | return NULL; 24 | } 25 | 26 | void SnakeSprite::setNextPoint(cocos2d::Point point) 27 | { 28 | this->nextPoint = point; 29 | } 30 | cocos2d::Point SnakeSprite::getNextPoint() 31 | { 32 | return this->nextPoint; 33 | } 34 | -------------------------------------------------------------------------------- /koyoter/Classes/SnakeSprite.h: -------------------------------------------------------------------------------- 1 | #ifndef _SNAKESPRITE_SPRITE_H__ 2 | #define _SNAKESPRITE_SPRITE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | class SnakeSprite : public cocos2d::Sprite 7 | { 8 | public: 9 | virtual bool init(); 10 | CREATE_FUNC(SnakeSprite); 11 | 12 | // 头部的图片不一样 所以有vip特权加个特别的函数 13 | static SnakeSprite* create(const char*); 14 | 15 | // 设置后面一个节点的坐标 16 | void setNextPoint(cocos2d::Point); // 17 | cocos2d::Point getNextPoint(); // 供后面的节点获取位置 18 | protected: 19 | cocos2d::Point nextPoint; 20 | }; 21 | 22 | #endif -------------------------------------------------------------------------------- /koyoter/Resources/eat.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/koyoter/Resources/eat.wav -------------------------------------------------------------------------------- /koyoter/Resources/fonts/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/koyoter/Resources/fonts/Marker Felt.ttf -------------------------------------------------------------------------------- /koyoter/Resources/image.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | frames 6 | 7 | background.png 8 | 9 | frame 10 | {{2,2},{960,640}} 11 | offset 12 | {0,0} 13 | rotated 14 | 15 | sourceColorRect 16 | {{0,0},{960,640}} 17 | sourceSize 18 | {960,640} 19 | 20 | down-normal.png 21 | 22 | frame 23 | {{90,908},{86,86}} 24 | offset 25 | {0,0} 26 | rotated 27 | 28 | sourceColorRect 29 | {{7,7},{86,86}} 30 | sourceSize 31 | {100,100} 32 | 33 | down-press.png 34 | 35 | frame 36 | {{90,820},{86,86}} 37 | offset 38 | {0,0} 39 | rotated 40 | 41 | sourceColorRect 42 | {{7,7},{86,86}} 43 | sourceSize 44 | {100,100} 45 | 46 | food.png 47 | 48 | frame 49 | {{964,212},{40,40}} 50 | offset 51 | {0,0} 52 | rotated 53 | 54 | sourceColorRect 55 | {{0,0},{40,40}} 56 | sourceSize 57 | {40,40} 58 | 59 | left-normal.png 60 | 61 | frame 62 | {{90,732},{86,86}} 63 | offset 64 | {0,0} 65 | rotated 66 | 67 | sourceColorRect 68 | {{7,7},{86,86}} 69 | sourceSize 70 | {100,100} 71 | 72 | left-press.png 73 | 74 | frame 75 | {{90,644},{86,86}} 76 | offset 77 | {0,0} 78 | rotated 79 | 80 | sourceColorRect 81 | {{7,7},{86,86}} 82 | sourceSize 83 | {100,100} 84 | 85 | right-normal.png 86 | 87 | frame 88 | {{2,908},{86,86}} 89 | offset 90 | {0,0} 91 | rotated 92 | 93 | sourceColorRect 94 | {{7,7},{86,86}} 95 | sourceSize 96 | {100,100} 97 | 98 | right-press.png 99 | 100 | frame 101 | {{2,820},{86,86}} 102 | offset 103 | {0,0} 104 | rotated 105 | 106 | sourceColorRect 107 | {{7,7},{86,86}} 108 | sourceSize 109 | {100,100} 110 | 111 | snake-body.png 112 | 113 | frame 114 | {{964,170},{40,40}} 115 | offset 116 | {0,0} 117 | rotated 118 | 119 | sourceColorRect 120 | {{0,0},{40,40}} 121 | sourceSize 122 | {40,40} 123 | 124 | snake-head-down.png 125 | 126 | frame 127 | {{964,128},{40,40}} 128 | offset 129 | {0,0} 130 | rotated 131 | 132 | sourceColorRect 133 | {{0,0},{40,40}} 134 | sourceSize 135 | {40,40} 136 | 137 | snake-head-left.png 138 | 139 | frame 140 | {{964,86},{40,40}} 141 | offset 142 | {0,0} 143 | rotated 144 | 145 | sourceColorRect 146 | {{0,0},{40,40}} 147 | sourceSize 148 | {40,40} 149 | 150 | snake-head-right.png 151 | 152 | frame 153 | {{964,44},{40,40}} 154 | offset 155 | {0,0} 156 | rotated 157 | 158 | sourceColorRect 159 | {{0,0},{40,40}} 160 | sourceSize 161 | {40,40} 162 | 163 | snake-head-up.png 164 | 165 | frame 166 | {{964,2},{40,40}} 167 | offset 168 | {0,0} 169 | rotated 170 | 171 | sourceColorRect 172 | {{0,0},{40,40}} 173 | sourceSize 174 | {40,40} 175 | 176 | up-normal.png 177 | 178 | frame 179 | {{2,732},{86,86}} 180 | offset 181 | {0,0} 182 | rotated 183 | 184 | sourceColorRect 185 | {{7,7},{86,86}} 186 | sourceSize 187 | {100,100} 188 | 189 | up-press.png 190 | 191 | frame 192 | {{2,644},{86,86}} 193 | offset 194 | {0,0} 195 | rotated 196 | 197 | sourceColorRect 198 | {{7,7},{86,86}} 199 | sourceSize 200 | {100,100} 201 | 202 | 203 | metadata 204 | 205 | format 206 | 2 207 | realTextureFileName 208 | image.png 209 | size 210 | {1024,1024} 211 | smartupdate 212 | $TexturePacker:SmartUpdate:f354744d0df3157d3e3a8a1dafc457dd:b825a88c236f37013883fbcf59f63c52:3981721564b145ebd989175ec4ec092e$ 213 | textureFileName 214 | image.png 215 | 216 | 217 | 218 | -------------------------------------------------------------------------------- /koyoter/Resources/image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/koyoter/Resources/image.png -------------------------------------------------------------------------------- /ldk1119/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "LoadingScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | 19 | if(!glview) { 20 | glview = GLView::create("snake"); 21 | director->setOpenGLView(glview); 22 | } 23 | director->setOpenGLView(glview); 24 | //glview->setFrameSize(480,800); 25 | glview->setDesignResolutionSize(480,800, ResolutionPolicy::SHOW_ALL); 26 | this->setResourceSearchResolution(); 27 | // turn on display FPS 28 | director->setDisplayStats(false); 29 | 30 | // set FPS. the default value is 1.0/60 if you don't call this 31 | director->setAnimationInterval(1.0 / 60); 32 | 33 | // create a scene. it's an autorelease object 34 | auto scene = LoadingScene::create(); 35 | 36 | // run 37 | director->runWithScene(scene); 38 | 39 | return true; 40 | } 41 | 42 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 43 | void AppDelegate::applicationDidEnterBackground() { 44 | Director::getInstance()->stopAnimation(); 45 | 46 | // if you use SimpleAudioEngine, it must be pause 47 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 48 | } 49 | 50 | // this function will be called when the app is active again 51 | void AppDelegate::applicationWillEnterForeground() { 52 | Director::getInstance()->startAnimation(); 53 | 54 | // if you use SimpleAudioEngine, it must resume here 55 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 56 | } 57 | void AppDelegate::setResourceSearchResolution(){ 58 | std::vector paths; 59 | paths.push_back("fonts"); 60 | paths.push_back("image"); 61 | paths.push_back("sounds"); 62 | FileUtils::getInstance()->setSearchResolutionsOrder(paths); 63 | } -------------------------------------------------------------------------------- /ldk1119/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | void setResourceSearchResolution(); 36 | }; 37 | 38 | #endif // _APP_DELEGATE_H_ 39 | 40 | -------------------------------------------------------------------------------- /ldk1119/Classes/BackgroundLayer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Classes/BackgroundLayer.cpp -------------------------------------------------------------------------------- /ldk1119/Classes/BackgroundLayer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | class BackgroundLayer : 5 | public Layer 6 | { 7 | public: 8 | BackgroundLayer(void); 9 | virtual ~BackgroundLayer(void); 10 | virtual bool init(); 11 | CREATE_FUNC(BackgroundLayer); 12 | }; 13 | 14 | -------------------------------------------------------------------------------- /ldk1119/Classes/GameLayer.cpp: -------------------------------------------------------------------------------- 1 | #include "GameLayer.h" 2 | 3 | 4 | GameLayer::GameLayer(void) 5 | { 6 | } 7 | 8 | 9 | GameLayer::~GameLayer(void) 10 | { 11 | this->removeAllChildren(); 12 | } 13 | bool GameLayer::init(){ 14 | if(!Layer::init()) 15 | return false; 16 | ball=NULL; 17 | auto listener = EventListenerTouchOneByOne::create(); 18 | listener->onTouchBegan = [&](Touch *touch, Event *unused_event)->bool {return true;}; 19 | listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this); 20 | this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); 21 | Size size=Director::getInstance()->getVisibleSize(); 22 | Point origin=Director::getInstance()->getVisibleOrigin(); 23 | setGameStatus(GAME_STATUS_READY); 24 | 25 | shiftLand = schedule_selector(GameLayer::snakePlay); 26 | this->schedule(shiftLand, 0.3f); 27 | this->scheduleUpdate(); 28 | 29 | snake=Snake::create(); 30 | 31 | this->addChild(snake,2); 32 | 33 | return true; 34 | } 35 | void GameLayer::snakePlay(float at){ 36 | 37 | if(getGameStatus()==GAME_STATUS_START){ 38 | int width=SCREEN_WIDTH/SNAKE_WIDTH; 39 | int height=SCREEN_HEIGHT/SNAKE_WIDTH; 40 | Rect rect(1,1,width-2,height-2); 41 | if(snake->IsDie(rect)){ 42 | setGameStatus(GAME_STATUS_OVER); 43 | delegator->onGameEnd(); 44 | return; 45 | } 46 | if(IsEatBall()){ 47 | snake->addSnakeNode(); 48 | this->setBall(); 49 | } 50 | else snake->play(); 51 | bSetDirection=false; 52 | return; 53 | } 54 | 55 | } 56 | void GameLayer::onTouchEnded(Touch *touch, Event *unused_event){ 57 | if(getGameStatus()==GAME_STATUS_READY){ 58 | setGameStatus(GAME_STATUS_START); 59 | delegator->onGamePlay(); 60 | snake->resetSnake(); 61 | setBall(); 62 | ball->setVisible(true); 63 | bSetDirection=false; 64 | return ; 65 | } 66 | if(getGameStatus()==GAME_STATUS_OVER){ 67 | setGameStatus(GAME_STATUS_READY); 68 | delegator->onGameStart(); 69 | snake->removeAllChildren(); 70 | ball->setVisible(false); 71 | } 72 | 73 | if(getGameStatus()==GAME_STATUS_START) 74 | this->setSnakeDirection(touch->getLocation()); 75 | } 76 | void GameLayer::setGameStatus(GameStatus status){ 77 | gameStatus=status; 78 | } 79 | GameStatus GameLayer::getGameStatus(){ 80 | return gameStatus; 81 | } 82 | void GameLayer::setSnakeDirection(Point p){ 83 | if(bSetDirection) return; 84 | if(snake->getDirection()%2==0){ 85 | if(p.x>SCREEN_WIDTH/2) 86 | snake->setDirection(SNAKE_HEAD_RIGHT); 87 | else 88 | snake->setDirection(SNAKE_HEAD_LEFT); 89 | } 90 | else{ 91 | if(p.y>SCREEN_HEIGHT/2) 92 | snake->setDirection(SNAKE_HEAD_UP); 93 | else 94 | snake->setDirection(SNAKE_HEAD_DOWN); 95 | } 96 | bSetDirection=true; 97 | } 98 | bool GameLayer::IsSnakeContainsBall(Point p){ 99 | for(SnakeNode *snakeNode:snake->getSnakeNodes()) 100 | if(snakeNode->getBoundingBox().containsPoint(p)) 101 | return true; 102 | 103 | return false; 104 | 105 | } 106 | void GameLayer::setBall(){ 107 | if(ball==NULL){ 108 | ball=Sprite::create(); 109 | this->addChild(ball,1); 110 | } 111 | vector tmpList; 112 | for(int i=1;iIsSnakeContainsBall(point)) 116 | tmpList.push_back(point); 117 | } 118 | int tmpListSize=tmpList.size(); 119 | if(tmpListSize>0){ 120 | 121 | int tmp=CCRANDOM_0_1()*(tmpListSize-1); 122 | ball->setPosition(tmpList[tmp].x,tmpList[tmp].y); 123 | 124 | String str; 125 | tmp=CCRANDOM_0_1()*3; 126 | switch (tmp){ 127 | case 0: 128 | str="football.png"; 129 | break; 130 | case 1: 131 | str="Volleyball.png"; 132 | break; 133 | case 2: 134 | str="basketball.png"; 135 | break; 136 | } 137 | ball->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str._string)); 138 | } 139 | 140 | } 141 | bool GameLayer::IsEatBall(){ 142 | if(snake->getSnakeNodes().front()->boundingBox().containsPoint(ball->getPosition())){ 143 | score++; 144 | return true;} 145 | return false; 146 | } 147 | -------------------------------------------------------------------------------- /ldk1119/Classes/GameLayer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "time.h" 4 | #include "Snake.h" 5 | 6 | USING_NS_CC; 7 | using namespace std; 8 | 9 | typedef enum _game_status { 10 | GAME_STATUS_READY , 11 | GAME_STATUS_START, 12 | GAME_STATUS_OVER, 13 | GAME_STATUS_PLAY 14 | } GameStatus; 15 | const int CAUTI_LEFT=100; 16 | const int GAME_WIDTH=8; 17 | const int GAME_HEIGHT=16; 18 | const int SCREEN_WIDTH=480; 19 | const int SCREEN_HEIGHT=800; 20 | //const int CAU_ 21 | class StatusDelegate { 22 | public: 23 | 24 | virtual void onGamePlay(void) = 0; 25 | 26 | virtual void onGameStart(void) = 0; 27 | 28 | virtual void onGameEnd(void) = 0; 29 | }; 30 | class GameLayer : 31 | public Layer 32 | { 33 | public: 34 | GameLayer(void); 35 | virtual ~GameLayer(void); 36 | virtual bool init(); 37 | CREATE_FUNC(GameLayer); 38 | GameStatus getGameStatus(); 39 | void setGameStatus(GameStatus status); 40 | 41 | private: 42 | //void update(float delta); 43 | 44 | StatusDelegate *delegate; 45 | CC_SYNTHESIZE(StatusDelegate*,delegator,Delegator); 46 | void onTouchEnded(Touch *touch, Event *unused_event) ; 47 | void setSnakeDirection(Point p); 48 | void setBall(); 49 | bool IsEatBall(); 50 | bool IsSnakeContainsBall(Point p); 51 | SEL_SCHEDULE shiftLand; 52 | void snakePlay(float dt); 53 | GameStatus gameStatus; 54 | Snake *snake; 55 | bool bSetDirection; 56 | Sprite *ball; 57 | int score; 58 | }; 59 | 60 | -------------------------------------------------------------------------------- /ldk1119/Classes/GameScene.cpp: -------------------------------------------------------------------------------- 1 | #include "GameScene.h" 2 | #include "BackgroundLayer.h" 3 | #include "GameLayer.h" 4 | #include "StatusLayer.h" 5 | GameScene::GameScene(void) 6 | { 7 | } 8 | 9 | 10 | GameScene::~GameScene(void) 11 | { 12 | } 13 | bool GameScene::init(){ 14 | if(!Scene::init()) 15 | return false; 16 | auto *backgroundLayer=BackgroundLayer::create(); 17 | this->addChild(backgroundLayer); 18 | auto *gameLayer=GameLayer::create(); 19 | auto *statusLayer=StatusLayer::create(); 20 | gameLayer->setDelegator(statusLayer); 21 | 22 | this->addChild(gameLayer); 23 | this->addChild(statusLayer); 24 | return true; 25 | } 26 | -------------------------------------------------------------------------------- /ldk1119/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | 4 | USING_NS_CC; 5 | 6 | class GameScene : 7 | public Scene 8 | { 9 | public: 10 | GameScene(void); 11 | virtual ~GameScene(void); 12 | virtual bool init(); 13 | CREATE_FUNC(GameScene); 14 | }; 15 | 16 | -------------------------------------------------------------------------------- /ldk1119/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 | Point 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(Point(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(Point::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 = LabelTTF::create("Hello World", "Arial", 24); 58 | 59 | // position the label on the center of the screen 60 | label->setPosition(Point(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(Point(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 | -------------------------------------------------------------------------------- /ldk1119/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 | // there's no 'id' in cpp, so we recommend returning the class instance pointer 10 | static cocos2d::Scene* createScene(); 11 | 12 | // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 13 | virtual bool init(); 14 | 15 | // a selector callback 16 | void menuCloseCallback(cocos2d::Ref* pSender); 17 | 18 | // implement the "static create()" method manually 19 | CREATE_FUNC(HelloWorld); 20 | }; 21 | 22 | #endif // __HELLOWORLD_SCENE_H__ 23 | -------------------------------------------------------------------------------- /ldk1119/Classes/LoadingScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Classes/LoadingScene.cpp -------------------------------------------------------------------------------- /ldk1119/Classes/LoadingScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | 4 | //#include "AtlasLoader.h" 5 | #include "SimpleAudioEngine.h" 6 | #include "HelloWorldScene.h" 7 | 8 | //#include "BackgroundLayer.h" 9 | 10 | using namespace cocos2d; 11 | using namespace CocosDenshion; 12 | 13 | class LoadingScene : public Scene { 14 | public: 15 | /** 16 | * The default constructor. 17 | */ 18 | LoadingScene(); 19 | 20 | ~LoadingScene(); 21 | 22 | /** 23 | * Initializes the instance of AtlasLoader. 24 | * 25 | * @note When you are porting Cocos2d-x to a new platform, you may need to take care of this method. 26 | * 27 | * @return true if successed, otherwise it returns false. 28 | * 29 | */ 30 | virtual bool init(); 31 | 32 | CREATE_FUNC(LoadingScene); 33 | 34 | /** 35 | * When this scene appear ,will call this method, 36 | * not this scene is create, so if you have some thing 37 | * want do when scen appear not init, please add to here 38 | */ 39 | void onEnter() override; 40 | 41 | private: 42 | /** 43 | * Called when the texture (exp. atlas.png) is loading finish, will call this function 44 | * this function finish build all the SpritFrame by using AtlasLoader 45 | */ 46 | void loadingCallBack(Texture2D *texture); 47 | }; -------------------------------------------------------------------------------- /ldk1119/Classes/Number.cpp: -------------------------------------------------------------------------------- 1 | #include "Number.h" 2 | 3 | NumberSeries::NumberSeries(){} 4 | 5 | NumberSeries::~NumberSeries(){} 6 | 7 | bool NumberSeries::init() { 8 | this->numberSeries = Vector(10); 9 | return true; 10 | } 11 | 12 | void NumberSeries::loadNumber(const char *fmt, int base) { 13 | //for (int i = base; i < 10 + base; i++){ 14 | const char *filename = String::createWithFormat(fmt, i)->getCString(); 15 | //auto frame = AtlasLoader::getInstance()->getSpriteFrameByName(filename); 16 | //numberSeries.pushBack(frame); 17 | } 18 | } 19 | 20 | SpriteFrame* NumberSeries::at(int index) { 21 | return this->numberSeries.at(index); 22 | } 23 | 24 | Number* Number::sharedNumber = nullptr; 25 | 26 | Number* Number::getInstance(){ 27 | if(sharedNumber == NULL) { 28 | sharedNumber = new Number(); 29 | if(!sharedNumber->init()){ 30 | delete sharedNumber; 31 | sharedNumber = NULL; 32 | CCLOG("ERROR: Could not init sharedNumber"); 33 | } 34 | } 35 | return sharedNumber; 36 | } 37 | 38 | 39 | void Number::destroyInstance() 40 | { 41 | CC_SAFE_DELETE(sharedNumber); 42 | } 43 | 44 | bool Number::init(){ 45 | return true; 46 | } 47 | 48 | Number::Number(){ 49 | } 50 | 51 | 52 | Number::~Number(){ 53 | } 54 | 55 | 56 | bool Number::loadNumber(const char* name, const char *fmt, int base){ 57 | auto numberSeries = NumberSeries::create(); 58 | numberSeries->loadNumber(fmt, base); 59 | numberContainer.insert(name, numberSeries); 60 | return true; 61 | } 62 | 63 | 64 | Node* Number::convert(const char* name, int number, Gravity gravity) { 65 | auto numbers = numberContainer.at(name); 66 | if (number == 0) { 67 | auto numberZero = Sprite::createWithSpriteFrame(numbers->at(0)); 68 | numberZero->setAnchorPoint(Point(0.5, 0)); 69 | return numberZero; 70 | } 71 | 72 | auto numberNode = Node::create(); 73 | float totalWidth = 0; 74 | while(number){ 75 | int temp = number % 10; 76 | number /= 10; 77 | auto sprite = Sprite::createWithSpriteFrame(numbers->at(temp)); 78 | totalWidth += sprite->getContentSize().width; 79 | numberNode->addChild(sprite); 80 | } 81 | 82 | auto numberZero = Sprite::createWithSpriteFrame(numbers->at(0)); 83 | numberNode->setContentSize(Size(totalWidth, numberZero->getContentSize().height)); 84 | 85 | if(gravity == Gravity::GRAVITY_CENTER) { 86 | float singleWidth = totalWidth/numberNode->getChildrenCount(); 87 | int index = numberNode->getChildrenCount()/2; 88 | for(auto child : numberNode->getChildren()) { 89 | child->setAnchorPoint(Point(0.5, 0)); 90 | float offLength = singleWidth*index--; 91 | child->setPositionX(offLength); 92 | } 93 | }else if(gravity == Gravity::GRAVITY_RIGHT) { 94 | float singleWidth = totalWidth/numberNode->getChildrenCount(); 95 | int index = numberNode->getChildrenCount(); 96 | for(auto child : numberNode->getChildren()) { 97 | child->setAnchorPoint(Point(1, 0)); 98 | float offLength = singleWidth*index--; 99 | child->setPositionX(offLength); 100 | } 101 | }else if(gravity == Gravity::GRAVITY_LEFT) { 102 | float singleWidth = totalWidth/numberNode->getChildrenCount(); 103 | int index = 0; 104 | for(auto child : numberNode->getChildren()) { 105 | child->setAnchorPoint(Point::ZERO); 106 | float offLength = singleWidth*index++; 107 | child->setPositionX(offLength); 108 | } 109 | } 110 | return numberNode; 111 | } -------------------------------------------------------------------------------- /ldk1119/Classes/Number.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocos2d.h" 4 | 5 | 6 | using namespace cocos2d; 7 | using namespace std; 8 | 9 | typedef enum _gravity { 10 | GRAVITY_CENTER = 1, 11 | GRAVITY_LEFT, 12 | GRAVITY_RIGHT 13 | } Gravity; 14 | 15 | /** 16 | * The distance between two number 17 | */ 18 | const int NumberInterval = 4; 19 | 20 | class NumberSeries : public Ref{ 21 | public: 22 | NumberSeries(); 23 | 24 | ~NumberSeries(); 25 | 26 | virtual bool init(); 27 | 28 | CREATE_FUNC(NumberSeries); 29 | 30 | void loadNumber(const char *fmt, int base = 0); 31 | 32 | SpriteFrame* at(int index); 33 | private: 34 | Vector numberSeries; 35 | }; 36 | 37 | /** 38 | * @class Number 39 | * Used to create the number sprite from texture and image, and 40 | * give some useful support 41 | */ 42 | class Number{ 43 | public: 44 | /** 45 | * Default constructor 46 | */ 47 | Number(); 48 | 49 | /** 50 | * Default destructor 51 | */ 52 | ~Number(); 53 | 54 | static Number* getInstance(); 55 | 56 | static void destroyInstance(); 57 | 58 | /** 59 | * cocos2d constructor 60 | * 61 | * @var fmt The file name like 'number_score_%2d', constructor will get number_score_00 62 | * to number_score_09 present the number 0 to 9 63 | * 64 | * @var base If the serious file name is not start with 0, you can set base to fix it 65 | * 66 | * #return instnce of number 67 | */ 68 | bool virtual loadNumber(const char* name, const char *fmt, int base = 0); 69 | 70 | /** 71 | * convert the integer number to Sprite present the number 72 | * 73 | * @var number The given number such as 4562 74 | * 75 | * @return the Node presenting the given number 76 | */ 77 | Node* convert(const char* name, int number, Gravity gravity = Gravity::GRAVITY_CENTER); 78 | 79 | virtual bool init(); 80 | 81 | private: 82 | Map numberContainer; 83 | 84 | static Number* sharedNumber; 85 | }; -------------------------------------------------------------------------------- /ldk1119/Classes/Snake.cpp: -------------------------------------------------------------------------------- 1 | #include "Snake.h" 2 | 3 | 4 | SnakeNode::SnakeNode(void){} 5 | SnakeNode::~SnakeNode(void){} 6 | void SnakeNode::setSnakeNode(int px,int py,int tag ){ 7 | //SnakeNode-> 8 | if(tag<=SNAKE_HEAD_LEFT) this->setAnchorPoint(Point(0.5,0.431)); 9 | this->setSnakeNode(tag); 10 | this->setPosition(px,py); 11 | 12 | } 13 | void SnakeNode::setSnakeNode(int tag){ 14 | this->setTag(tag); 15 | this->setSpriteFrame(this->getFrameNameByTag( tag)); 16 | this->setSnakeNodeRotate(); 17 | } 18 | void SnakeNode::setSnakeNodeRotate(){ 19 | int tag=this->getTag(); 20 | int rotate=90*(tag%4); 21 | if(tag==SNAKE_CORNER_RIGHT_UP||tag==SNAKE_CORNER_DOWN_LEFT) 22 | rotate=180; 23 | if(tag==SNAKE_CORNER_RIGHT_DOWN || tag==SNAKE_CORNER_UP_LEFT) 24 | rotate=90; 25 | if(tag==SNAKE_CORNER_LEFT_UP||tag==SNAKE_CORNER_DOWN_RIGHT) 26 | rotate=270; 27 | if(tag==SNAKE_CORNER_LEFT_DOWN||tag==SNAKE_CORNER_UP_RIGHT) 28 | rotate=0; 29 | 30 | this->setRotation(rotate); 31 | } 32 | void SnakeNode::setPosition(int x,int y){ 33 | Sprite::setPosition(x*SNAKE_WIDTH,y*SNAKE_WIDTH); 34 | } 35 | void SnakeNode::setPosition(Point p){ 36 | Sprite::setPosition(p.x*SNAKE_WIDTH,p.y*SNAKE_WIDTH); 37 | } 38 | Point SnakeNode::getPosition(){ 39 | Point p=Sprite::getPosition(); 40 | p.x=(int)p.x/SNAKE_WIDTH; 41 | p.y=(int)p.y/SNAKE_WIDTH; 42 | return p; 43 | } 44 | SpriteFrame* SnakeNode::getFrameNameByTag(int tag){ 45 | switch(tag){ 46 | case SNAKE_HEAD_UP: 47 | case SNAKE_HEAD_DOWN: 48 | case SNAKE_HEAD_LEFT: 49 | case SNAKE_HEAD_RIGHT: 50 | return SpriteFrameCache::getInstance()->getSpriteFrameByName("snake_head.png"); 51 | case SNAKE_BODY_UP: 52 | case SNAKE_BODY_DOWN: 53 | case SNAKE_BODY_LEFT: 54 | case SNAKE_BODY_RIGHT: 55 | return SpriteFrameCache::getInstance()->getSpriteFrameByName("snake_body.png"); 56 | case SNAKE_CORNER_DOWN_LEFT: 57 | case SNAKE_CORNER_LEFT_DOWN: 58 | case SNAKE_CORNER_RIGHT_DOWN: 59 | case SNAKE_CORNER_UP_LEFT: 60 | case SNAKE_CORNER_DOWN_RIGHT: 61 | case SNAKE_CORNER_LEFT_UP: 62 | case SNAKE_CORNER_RIGHT_UP: 63 | case SNAKE_CORNER_UP_RIGHT: 64 | return SpriteFrameCache::getInstance()->getSpriteFrameByName("snake_corner.png"); 65 | case SNAKE_TAIL_UP: 66 | case SNAKE_TAIL_DOWN: 67 | case SNAKE_TAIL_LEFT: 68 | case SNAKE_TAIL_RIGHT: 69 | return SpriteFrameCache::getInstance()->getSpriteFrameByName("snake_tail.png"); 70 | } 71 | return NULL; 72 | } 73 | /////*********************************************************// 74 | // 75 | // 76 | // 77 | // 78 | //**************************************************************// 79 | Snake::Snake(void) 80 | { 81 | } 82 | 83 | 84 | Snake::~Snake(void) 85 | { 86 | 87 | } 88 | void Snake::createSnake(){ 89 | Size size=Director::getInstance()->getVisibleSize(); 90 | Point origin=Director::getInstance()->getVisibleOrigin(); 91 | this->setAnchorPoint(Point::ZERO); 92 | SnakeNode *snakeNode=SnakeNode::create(); 93 | snakeNode->setSnakeNode(7,3,SNAKE_HEAD_UP); 94 | node.pushBack(snakeNode); 95 | this->addChild(snakeNode); 96 | SnakeNode *snakeNode1=SnakeNode::create(); 97 | snakeNode1->setSnakeNode(7,2,SNAKE_BODY_UP); 98 | node.pushBack(snakeNode1); 99 | this->addChild(snakeNode1); 100 | SnakeNode *snakeNode2=SnakeNode::create(); 101 | snakeNode2->setSnakeNode(7,1,SNAKE_TAIL_UP); 102 | node.pushBack(snakeNode2); 103 | this->addChild(snakeNode2); 104 | setDirection(SNAKE_HEAD_UP); 105 | 106 | } 107 | void Snake::setDirection(int direction){ 108 | this->direction=direction; 109 | } 110 | int Snake::getDirection(){ 111 | return this->direction; 112 | } 113 | void Snake::addSnakeNode(){ 114 | SnakeNode *snakeNode=SnakeNode::create(); 115 | snakeNode->setPosition(node.front()->getPosition()); 116 | snakeNode->setSnakeNode(this->getInsertTag(node.front()->getTag(),this->getDirection())); 117 | node.insert(1,snakeNode); 118 | this->addChild(snakeNode); 119 | if(node.front()->getTag()!=this->getDirection()){ 120 | node.front()->setTag(this->getDirection()); 121 | node.front()->setSnakeNodeRotate(); 122 | } 123 | node.front()->setPosition(getNext()); 124 | } 125 | 126 | void Snake::play(){ 127 | SnakeNode *snakeNode=node.back(); 128 | //int tag=snakeNode->getTag(); 129 | snakeNode->setPosition(node.front()->getPosition()); 130 | snakeNode->setSnakeNode(this->getInsertTag(node.front()->getTag(),this->getDirection())); 131 | node.insert(1,snakeNode); 132 | if(node.front()->getTag()!=this->getDirection()){ 133 | node.front()->setTag(this->getDirection()); 134 | node.front()->setSnakeNodeRotate(); 135 | } 136 | node.front()->setPosition(getNext()); 137 | 138 | node.popBack(); 139 | snakeNode=node.back(); 140 | snakeNode->setSnakeNode(snakeNode->getTag()%4+8); 141 | } 142 | Point Snake::getNext(){ 143 | SnakeNode *snakeNode=node.front(); 144 | Point point=snakeNode->getPosition(); 145 | switch(snakeNode->getTag()){ 146 | case SNAKE_HEAD_UP: 147 | ++point.y; 148 | break; 149 | case SNAKE_HEAD_DOWN: 150 | --point.y; 151 | break; 152 | case SNAKE_HEAD_LEFT: 153 | --point.x; 154 | break; 155 | case SNAKE_HEAD_RIGHT: 156 | ++point.x; 157 | break; 158 | } 159 | return point; 160 | } 161 | int Snake::getInsertTag(int oldTag,int newTag){ 162 | if(oldTag==newTag)return oldTag+4; 163 | if(oldTag==SNAKE_HEAD_RIGHT&&newTag==SNAKE_HEAD_UP) 164 | return SNAKE_CORNER_RIGHT_UP; 165 | if(oldTag==SNAKE_HEAD_DOWN&&newTag==SNAKE_HEAD_LEFT) 166 | return SNAKE_CORNER_DOWN_LEFT; 167 | if(oldTag==SNAKE_HEAD_RIGHT&&newTag==SNAKE_HEAD_DOWN) 168 | return SNAKE_CORNER_RIGHT_DOWN; 169 | if(oldTag==SNAKE_HEAD_UP&&newTag==SNAKE_HEAD_LEFT) 170 | return SNAKE_CORNER_UP_LEFT; 171 | if(oldTag==SNAKE_HEAD_LEFT&&newTag==SNAKE_HEAD_UP) 172 | return SNAKE_CORNER_LEFT_UP; 173 | if(oldTag==SNAKE_HEAD_DOWN &&newTag==SNAKE_HEAD_RIGHT) 174 | return SNAKE_CORNER_DOWN_RIGHT; 175 | if(oldTag==SNAKE_HEAD_LEFT && newTag==SNAKE_HEAD_DOWN) 176 | return SNAKE_CORNER_LEFT_DOWN; 177 | if(oldTag==SNAKE_HEAD_UP&&newTag==SNAKE_HEAD_RIGHT) 178 | return SNAKE_CORNER_UP_RIGHT; 179 | return -1; 180 | } 181 | bool Snake::IsDie(Rect rect){ 182 | 183 | if(!rect.containsPoint(node.front()->getPosition())) 184 | return true; 185 | Rect box=node.front()->getBoundingBox(); 186 | auto point=node.front()->getPosition(); 187 | point.x=point.x*SNAKE_WIDTH; 188 | point.y=point.y*SNAKE_WIDTH; 189 | for(SnakeNode *snakenode:node){ 190 | if(snakenode->getTag()>SNAKE_HEAD_LEFT) 191 | if(snakenode->getBoundingBox().containsPoint(point)) 192 | return true; 193 | } 194 | 195 | return false; 196 | } 197 | Vector Snake::getSnakeNodes(){ 198 | return node; 199 | } 200 | void Snake::resetSnake(){ 201 | this->removeAllChildren(); 202 | node.clear(); 203 | this->createSnake(); 204 | } -------------------------------------------------------------------------------- /ldk1119/Classes/Snake.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | USING_NS_CC; 4 | using namespace std; 5 | const int SNAKE_HEAD_UP=0; 6 | const int SNAKE_HEAD_DOWN=2; 7 | const int SNAKE_HEAD_LEFT=3; 8 | const int SNAKE_HEAD_RIGHT=1; 9 | const int SNAKE_BODY_UP=4; 10 | const int SNAKE_BODY_DOWN=6; 11 | const int SNAKE_BODY_LEFT=7; 12 | const int SNAKE_BODY_RIGHT=5; 13 | const int SNAKE_CORNER_LEFT_UP=16; 14 | const int SNAKE_CORNER_LEFT_DOWN=18; 15 | const int SNAKE_CORNER_UP_LEFT=19; 16 | const int SNAKE_CORNER_UP_RIGHT=17; 17 | const int SNAKE_TAIL_UP=8; 18 | const int SNAKE_TAIL_DOWN=10; 19 | const int SNAKE_TAIL_LEFT=11; 20 | const int SNAKE_TAIL_RIGHT=9; 21 | const int SNAKE_CORNER_RIGHT_UP=12; 22 | const int SNAKE_CORNER_RIGHT_DOWN=14; 23 | const int SNAKE_CORNER_DOWN_LEFT=15; 24 | const int SNAKE_CORNER_DOWN_RIGHT=13; 25 | const float SNAKE_WIDTH=50; 26 | class SnakeNode : 27 | public Sprite 28 | { 29 | public: 30 | SnakeNode(void); 31 | ~SnakeNode(void); 32 | CREATE_FUNC(SnakeNode); 33 | //void setSnakeNodeRotiteByTag(); 34 | void setSnakeNode(int x,int y,int tag); 35 | void setSnakeNode(int tag); 36 | SpriteFrame* getFrameNameByTag(int tag); 37 | void setPosition(int x,int y); 38 | void setPosition(Point p); 39 | Point getPosition(); 40 | void setSnakeNodeRotate(); 41 | 42 | }; 43 | class Snake : 44 | public Node 45 | { 46 | public: 47 | Snake(void); 48 | ~Snake(void); 49 | CREATE_FUNC(Snake); 50 | void createSnake(); 51 | void play(); 52 | void setDirection(int direction); 53 | int getDirection(); 54 | bool IsDie(Rect rect); 55 | Vector getSnakeNodes(); 56 | void addSnakeNode(); 57 | void resetSnake(); 58 | private: 59 | void setSnakeNode(Sprite *SnakeNode); 60 | Point getNext(); 61 | int getInsertTag(int oldTag, int newTag); 62 | int direction; 63 | Vector node; 64 | }; 65 | 66 | -------------------------------------------------------------------------------- /ldk1119/Classes/StatusLayer.cpp: -------------------------------------------------------------------------------- 1 | #include "StatusLayer.h" 2 | 3 | 4 | StatusLayer::StatusLayer(void) 5 | { 6 | } 7 | 8 | 9 | StatusLayer::~StatusLayer(void) 10 | { 11 | } 12 | bool StatusLayer::init(){ 13 | if(!Layer::init()) 14 | return false; 15 | Size size=Director::getInstance()->getVisibleSize(); 16 | Point origin=Director::getInstance()->getVisibleOrigin(); 17 | statusSprite=Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("getready.png")); 18 | statusSprite->setPosition(Point(origin.x+size.width/2,origin.y+size.height*0.6f)); 19 | statusSprite->setScale(2.0f); 20 | this->addChild(statusSprite); 21 | 22 | return true; 23 | } 24 | void StatusLayer::onGameStart(){ 25 | Size size=Director::getInstance()->getVisibleSize(); 26 | Point origin=Director::getInstance()->getVisibleOrigin(); 27 | statusSprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("getready.png")); 28 | statusSprite->setVisible(true); 29 | 30 | } 31 | void StatusLayer::onGamePlay(){ 32 | statusSprite->setVisible(false); 33 | 34 | 35 | } 36 | void StatusLayer::onGameEnd(){ 37 | auto size=Director::getInstance()->getVisibleSize(); 38 | auto origin=Director::getInstance()->getVisibleOrigin(); 39 | statusSprite->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("gameover.png")); 40 | statusSprite->setVisible(true); 41 | 42 | 43 | } 44 | -------------------------------------------------------------------------------- /ldk1119/Classes/StatusLayer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "GameLayer.h" 4 | USING_NS_CC; 5 | 6 | class StatusLayer : 7 | public Layer, public StatusDelegate 8 | { 9 | public: 10 | StatusLayer(void); 11 | virtual ~StatusLayer(void); 12 | virtual bool init(); 13 | CREATE_FUNC(StatusLayer); 14 | virtual void onGameStart(void) ; 15 | virtual void onGamePlay(void) ; 16 | virtual void onGameEnd(void); 17 | private: 18 | 19 | Sprite* statusSprite; 20 | Label* label; 21 | 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /ldk1119/Classes/WelcomeLayer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Classes/WelcomeLayer.cpp -------------------------------------------------------------------------------- /ldk1119/Classes/WelcomeLayer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | 4 | #include "SimpleAudioEngine.h" 5 | #include "CCMenuItem.h" 6 | using namespace std; 7 | using namespace cocos2d; 8 | class WelcomeLayer : 9 | public Layer 10 | { 11 | public: 12 | WelcomeLayer(void); 13 | virtual ~WelcomeLayer(void); 14 | virtual bool init(); 15 | // implement the "static create()" method manually 16 | CREATE_FUNC(WelcomeLayer); 17 | private: 18 | void menuStartCallback(Ref *sender); 19 | }; 20 | 21 | -------------------------------------------------------------------------------- /ldk1119/Classes/WelcomeScene.cpp: -------------------------------------------------------------------------------- 1 | #include "WelcomeScene.h" 2 | #include "GameScene.h" 3 | 4 | WelcomeScene::WelcomeScene(void) 5 | { 6 | } 7 | 8 | 9 | WelcomeScene::~WelcomeScene(void) 10 | { 11 | } 12 | bool WelcomeScene::init(){ 13 | if(!Scene::init()) 14 | return false; 15 | WelcomeLayer *welcomeLayer=WelcomeLayer::create(); 16 | this->addChild(welcomeLayer); 17 | return true; 18 | } -------------------------------------------------------------------------------- /ldk1119/Classes/WelcomeScene.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "cocos2d.h" 3 | #include "WelcomeLayer.h" 4 | using namespace cocos2d; 5 | using namespace std; 6 | 7 | class WelcomeScene : 8 | public Scene 9 | { 10 | public: 11 | WelcomeScene(void); 12 | virtual ~WelcomeScene(void); 13 | bool virtual init(); 14 | CREATE_FUNC(WelcomeScene); 15 | }; 16 | 17 | -------------------------------------------------------------------------------- /ldk1119/Resources/CloseNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Resources/CloseNormal.png -------------------------------------------------------------------------------- /ldk1119/Resources/CloseSelected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Resources/CloseSelected.png -------------------------------------------------------------------------------- /ldk1119/Resources/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Resources/HelloWorld.png -------------------------------------------------------------------------------- /ldk1119/Resources/fonts/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Resources/fonts/Marker Felt.ttf -------------------------------------------------------------------------------- /ldk1119/Resources/image/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Resources/image/logo.png -------------------------------------------------------------------------------- /ldk1119/Resources/image/ui.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | frames 6 | 7 | 6245564_100218625138_2.psd 8 | 9 | frame 10 | {{376,983},{28,28}} 11 | offset 12 | {0,0} 13 | rotated 14 | 15 | sourceColorRect 16 | {{1,1},{28,28}} 17 | sourceSize 18 | {30,30} 19 | 20 | Volleyball.png 21 | 22 | frame 23 | {{376,953},{28,28}} 24 | offset 25 | {0,0} 26 | rotated 27 | 28 | sourceColorRect 29 | {{1,1},{28,28}} 30 | sourceSize 31 | {30,30} 32 | 33 | background.jpg 34 | 35 | frame 36 | {{1,1},{480,800}} 37 | offset 38 | {0,0} 39 | rotated 40 | 41 | sourceColorRect 42 | {{0,0},{480,800}} 43 | sourceSize 44 | {480,800} 45 | 46 | basketball.png 47 | 48 | frame 49 | {{413,857},{28,28}} 50 | offset 51 | {0,0} 52 | rotated 53 | 54 | sourceColorRect 55 | {{1,1},{28,28}} 56 | sourceSize 57 | {30,30} 58 | 59 | football.png 60 | 61 | frame 62 | {{383,857},{28,28}} 63 | offset 64 | {0,0} 65 | rotated 66 | 67 | sourceColorRect 68 | {{1,1},{28,28}} 69 | sourceSize 70 | {30,30} 71 | 72 | gameover.png 73 | 74 | frame 75 | {{351,803},{202,52}} 76 | offset 77 | {0,-1} 78 | rotated 79 | 80 | sourceColorRect 81 | {{0,2},{202,52}} 82 | sourceSize 83 | {202,54} 84 | 85 | getready.png 86 | 87 | frame 88 | {{177,941},{197,56}} 89 | offset 90 | {-2,-1} 91 | rotated 92 | 93 | sourceColorRect 94 | {{0,2},{197,56}} 95 | sourceSize 96 | {201,58} 97 | 98 | snake_body.png 99 | 100 | frame 101 | {{229,999},{20,50}} 102 | offset 103 | {0,0} 104 | rotated 105 | 106 | sourceColorRect 107 | {{15,0},{20,50}} 108 | sourceSize 109 | {50,50} 110 | 111 | snake_corner.png 112 | 113 | frame 114 | {{376,915},{36,36}} 115 | offset 116 | {7,-7} 117 | rotated 118 | 119 | sourceColorRect 120 | {{14,14},{36,36}} 121 | sourceSize 122 | {50,50} 123 | 124 | snake_head.png 125 | 126 | frame 127 | {{351,857},{30,56}} 128 | offset 129 | {0,-1} 130 | rotated 131 | 132 | sourceColorRect 133 | {{10,2},{30,56}} 134 | sourceSize 135 | {50,58} 136 | 137 | snake_logo.png 138 | 139 | frame 140 | {{1,803},{348,136}} 141 | offset 142 | {0,3} 143 | rotated 144 | 145 | sourceColorRect 146 | {{6,11},{348,136}} 147 | sourceSize 148 | {360,164} 149 | 150 | snake_play.png 151 | 152 | frame 153 | {{1,941},{174,71}} 154 | offset 155 | {0,0} 156 | rotated 157 | 158 | sourceColorRect 159 | {{0,1},{174,71}} 160 | sourceSize 161 | {174,73} 162 | 163 | snake_tail.png 164 | 165 | frame 166 | {{177,999},{20,50}} 167 | offset 168 | {0,0} 169 | rotated 170 | 171 | sourceColorRect 172 | {{15,0},{20,50}} 173 | sourceSize 174 | {50,50} 175 | 176 | 177 | metadata 178 | 179 | format 180 | 2 181 | realTextureFileName 182 | ui.png 183 | size 184 | {1024,1024} 185 | smartupdate 186 | $TexturePacker:SmartUpdate:8051dc39cde7c99b29618d67453fce99$ 187 | textureFileName 188 | ui.png 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /ldk1119/Resources/image/ui.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/ldk1119/Resources/image/ui.png -------------------------------------------------------------------------------- /pashan/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("My Game"); 20 | director->setOpenGLView(glview); 21 | } 22 | 23 | // turn on display FPS 24 | director->setDisplayStats(true); 25 | 26 | // set FPS. the default value is 1.0/60 if you don't call this 27 | director->setAnimationInterval(1.0 / 60); 28 | 29 | // create a scene. it's an autorelease object 30 | auto scene = HelloWorld::createScene(); 31 | 32 | // run 33 | director->runWithScene(scene); 34 | 35 | return true; 36 | } 37 | 38 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 39 | void AppDelegate::applicationDidEnterBackground() { 40 | Director::getInstance()->stopAnimation(); 41 | 42 | // if you use SimpleAudioEngine, it must be pause 43 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 44 | } 45 | 46 | // this function will be called when the app is active again 47 | void AppDelegate::applicationWillEnterForeground() { 48 | Director::getInstance()->startAnimation(); 49 | 50 | // if you use SimpleAudioEngine, it must resume here 51 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 52 | } 53 | -------------------------------------------------------------------------------- /pashan/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /pashan/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | #include "SimpleAudioEngine.h" 3 | 4 | USING_NS_CC; 5 | 6 | #define BACKGROUND_SOUND "background.wav" 7 | #define EAT_SOUND "effect.wav" 8 | #define GAMEOVER_SOUND "gameover.wav" 9 | 10 | Scene* HelloWorld::createScene() 11 | { 12 | // 'scene' is an autorelease object 13 | auto scene = Scene::create(); 14 | 15 | // 'layer' is an autorelease object 16 | auto layer = HelloWorld::create(); 17 | 18 | // add layer as a child to scene 19 | scene->addChild(layer); 20 | 21 | // return the scene 22 | return scene; 23 | } 24 | 25 | // on "init" you need to initialize your instance 26 | bool HelloWorld::init() 27 | { 28 | if ( !Layer::init() ) 29 | { 30 | return false; 31 | } 32 | 33 | Size visibleSize = Director::getInstance()->getVisibleSize(); 34 | Point origin = Director::getInstance()->getVisibleOrigin(); 35 | 36 | auto labelStart = Label::createWithSystemFont("Start", "Arial", 32); 37 | 38 | auto startItem = MenuItemLabel::create(labelStart, CC_CALLBACK_1(HelloWorld::menuCallback, this)); 39 | startItem->setTag(START); 40 | startItem->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height-420)); 41 | 42 | auto menu = Menu::create(startItem, NULL); 43 | menu->setPosition(Point::ZERO); 44 | this->addChild(menu, 1); 45 | 46 | auto label = Label::createWithSystemFont("Snake", "Arial", 64); 47 | label->setPosition(Point(visibleSize.width/2, 48 | visibleSize.height-160)); 49 | this->addChild(label, 1); 50 | 51 | if(highScore>0){ 52 | char buff[20] = {0}; 53 | sprintf(buff,"High Score: %d",highScore); 54 | auto labelHighScore = Label::createWithSystemFont(buff, "Arial", 32); 55 | labelHighScore->setPosition(Point(visibleSize.width/2, 56 | visibleSize.height-320)); 57 | this->addChild(labelHighScore); 58 | } 59 | 60 | Director::getInstance()->setDisplayStats(false); 61 | return true; 62 | } 63 | 64 | 65 | void HelloWorld::menuCallback(Ref* pSender) 66 | { 67 | switch (((Node*)pSender)->getTag()) { 68 | case START: 69 | CCLOG("go to game"); 70 | Director::getInstance()->replaceScene(GameScene::createScene()); 71 | break; 72 | case EXIT: 73 | Director::getInstance()->end(); 74 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 75 | exit(0); 76 | #endif 77 | } 78 | } 79 | 80 | bool GameScene::init() 81 | { 82 | if (!Layer::init()) { 83 | return false; 84 | } 85 | Size visibleSize = Director::getInstance()->getVisibleSize(); 86 | auto labelBack = Label::createWithSystemFont("Main Menu", "Arial", 16); 87 | auto itemBack = MenuItemLabel::create(labelBack, CC_CALLBACK_1(GameScene::menuBackToMain, this)); 88 | itemBack->setPosition(Point(visibleSize.width, 0)); 89 | itemBack->setAnchorPoint(Point::ANCHOR_BOTTOM_RIGHT); 90 | auto menu = Menu::create(itemBack, NULL); 91 | menu->setPosition(Point(0, 0)); 92 | this->addChild(menu); 93 | 94 | this->labelScore = Label::createWithSystemFont("Score: 0", "Arial", 22); 95 | this->labelScore->setPosition(Point(visibleSize.width- labelScore->getContentSize().width, 96 | visibleSize.height - labelScore->getContentSize().height)); 97 | this->addChild(labelScore); 98 | 99 | winWidth=visibleSize.width; 100 | winHeight=visibleSize.height; 101 | score=0; 102 | speed=0.1f; 103 | gameOverFlag=false; 104 | gameOverDelay=1.0f/speed; 105 | 106 | //初始化蛇 107 | snakeLength=3; 108 | snakeNodeWidth=18; 109 | snakeStep=1; 110 | snakeAllStep=0; 111 | controlDir=DIR_DEF::LEFT; 112 | int x=winWidth-snakeLength*snakeNodeWidth-10; 113 | int y=rand()%winHeight-snakeNodeWidth; 114 | x=(x/snakeNodeWidth)*snakeNodeWidth; 115 | y=(y/snakeNodeWidth)*snakeNodeWidth; 116 | 117 | 118 | for(int i=0;ix=x+snakeNodeWidth*i; 122 | node->y=y; 123 | int dir=controlDir; 124 | node->sprite=sprite; 125 | node->sprite->setPosition(Point(node->x, node->y)); 126 | node->sprite->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); 127 | 128 | 129 | this->addChild(node->sprite); 130 | snake.pushBack(node); 131 | if(i>0){ 132 | for(int j=0;jdir=dir; 135 | dirChain.pushBack(dirNode); 136 | } 137 | } 138 | } 139 | 140 | //初始化蛋 141 | egg =genEgg(); 142 | this->addChild(egg->sprite); 143 | 144 | //建立触摸监听 145 | auto listener = EventListenerTouchOneByOne::create(); 146 | listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); 147 | listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this); 148 | this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this); 149 | 150 | //建立键盘监听 151 | auto listenerKeyboard = EventListenerKeyboard::create(); 152 | listenerKeyboard->onKeyPressed = CC_CALLBACK_2(GameScene::onKeyPressed, this); 153 | listenerKeyboard->onKeyReleased = CC_CALLBACK_2(GameScene::onKeyReleased, this); 154 | 155 | _eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKeyboard, this); 156 | 157 | CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(0.1f); 158 | CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(BACKGROUND_SOUND); 159 | 160 | 161 | this->schedule(SEL_SCHEDULE(&GameScene::play), speed); 162 | 163 | return true; 164 | } 165 | 166 | SnakeNode* GameScene::genEgg(){ 167 | auto node=new SnakeNode(); 168 | auto sprite = Sprite::create("egg.png"); 169 | node->x=rand()%winWidth-sprite->getContentSize().width; 170 | node->y=rand()%winHeight-sprite->getContentSize().height; 171 | node->sprite=sprite; 172 | node->sprite->setPosition(Point(node->x, node->y)); 173 | //node->sprite->setAnchorPoint(Point::ANCHOR_MIDDLE); 174 | node->sprite->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); 175 | return node; 176 | } 177 | bool GameScene::onTouchBegan( Touch* touch, Event* event ) 178 | { 179 | Point touchPoint = touch->getLocation(); 180 | touch_x= (int)touchPoint.x; 181 | touch_y= (int)touchPoint.y; 182 | return true; 183 | } 184 | void GameScene::onTouchEnded( Touch* touch, Event* event ) 185 | { 186 | Point touchPoint = touch->getLocation(); 187 | int x = (int)touchPoint.x; 188 | int y = (int)touchPoint.y; 189 | int x1=touch_x; 190 | int y1=touch_y; 191 | int dir; 192 | 193 | if (abs(y - y1) > abs(x - x1)) { 194 | if (y > y1) dir = DIR_DEF::UP; 195 | else dir = DIR_DEF::DOWN; 196 | } 197 | else { 198 | if (x > x1) 199 | dir = DIR_DEF::RIGHT; 200 | else dir = DIR_DEF::LEFT; 201 | } 202 | snakeMove(dir); 203 | } 204 | 205 | 206 | // Implementation of the keyboard event callback function prototype 207 | void GameScene::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) 208 | { 209 | log("Key with keycode %d pressed", keyCode); 210 | 211 | if(keyCode==EventKeyboard::KeyCode::KEY_LEFT_ARROW) snakeMove(DIR_DEF::LEFT); 212 | else if(keyCode==EventKeyboard::KeyCode::KEY_UP_ARROW) snakeMove(DIR_DEF::UP); 213 | else if(keyCode==EventKeyboard::KeyCode::KEY_RIGHT_ARROW) snakeMove(DIR_DEF::RIGHT); 214 | else if(keyCode==EventKeyboard::KeyCode::KEY_DOWN_ARROW) snakeMove(DIR_DEF::DOWN); 215 | 216 | } 217 | 218 | void GameScene::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) 219 | { 220 | log("Key with keycode %d released", keyCode); 221 | } 222 | 223 | void GameScene::snakeMove(int dir){ 224 | //避免舌头直接撞到蛇身 225 | if(controlDir==DIR_DEF::UP && dir == DIR_DEF::DOWN); 226 | else if(controlDir==DIR_DEF::DOWN && dir == DIR_DEF::UP); 227 | else if(controlDir==DIR_DEF::LEFT && dir == DIR_DEF::RIGHT); 228 | else if(controlDir==DIR_DEF::RIGHT && dir == DIR_DEF::LEFT); 229 | else controlDir=dir; 230 | } 231 | void GameScene::play(float t) 232 | { 233 | if(!gameOverFlag){ 234 | //移动蛇 235 | for(int i=snake.size()-1;i>=0;i--){ 236 | SnakeNode *node=snake.at(i); 237 | int x=node->x; 238 | int y=node->y; 239 | int d=controlDir; 240 | int step=snakeNodeWidth/snakeStep; 241 | if(i==0) d=controlDir; 242 | else { 243 | DirNode *dirNode =dirChain.at((i-1)*snakeStep+snakeStep-1); 244 | d=dirNode->dir; 245 | //CCLOG("end %d %d",dirChain.,y); 246 | } 247 | switch (d) 248 | { 249 | case DIR_DEF::UP: 250 | y +=step; 251 | break; 252 | case DIR_DEF::DOWN: 253 | y -=step; 254 | break; 255 | case DIR_DEF::LEFT: 256 | x -=step; 257 | break; 258 | case DIR_DEF::RIGHT: 259 | x +=step; 260 | break; 261 | } 262 | 263 | node->x=x; 264 | node->y=y; 265 | //node->sprite->setPosition(Point(node->x, node->y)); 266 | 267 | float duration =speed*0.8f;//+(CCRANDOM_0_1()-0.5f)*speed*0.2 ; 268 | auto actionMove = MoveTo::create(duration, 269 | Point(node->x, node->y)); 270 | 271 | node->sprite->runAction(Sequence::create(actionMove, NULL, NULL)); 272 | 273 | 274 | } 275 | //调整移动方向链 276 | for(int i=dirChain.size()-1;i>=0;i--){ 277 | DirNode *dirNode=dirChain.at(i); 278 | int d=dirNode->dir; 279 | if(i==0) d=controlDir; 280 | else { 281 | DirNode *lastDirNode =dirChain.at(i-1); 282 | d=lastDirNode->dir; 283 | } 284 | dirNode->dir=d; 285 | } 286 | 287 | //碰撞判断 288 | SnakeNode *head=snake.at(0); 289 | //检测是否吃到蛋 290 | if(collisionDetection(head,egg)){ 291 | //处理并显示分数 292 | score+=1;//增加分数 293 | if(score>highScore) highScore=score; 294 | this->removeChild(labelScore); 295 | char buff[20] = {0}; 296 | sprintf(buff,"Score: %d",score); 297 | labelScore = Label::createWithSystemFont(buff, "Arial", 22); 298 | 299 | labelScore->setPosition(Point(winWidth-labelScore->getContentSize().width, 300 | winHeight-labelScore->getContentSize().height)); 301 | this->addChild(labelScore); 302 | 303 | CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(EAT_SOUND); 304 | 305 | //增长蛇身的处理 306 | auto tail=snake.at(snake.size()-1); 307 | auto sprite = Sprite::create("snake.png"); 308 | auto node = new SnakeNode(); 309 | Point pt=tail->sprite->getPosition(); 310 | node->x=tail->x; 311 | node->y=tail->y; 312 | node->sprite=sprite; 313 | node->sprite->setPosition(Point(node->x, node->y)); 314 | node->sprite->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); 315 | this->addChild(node->sprite); 316 | snake.pushBack(node); 317 | snakeLength++; 318 | 319 | for(int j=0;jdir=0; 322 | dirChain.pushBack(dirNode); 323 | } 324 | 325 | 326 | //生成新的鸡蛋 327 | this->removeChild(egg->sprite); 328 | egg =genEgg(); 329 | this->addChild(egg->sprite); 330 | 331 | } 332 | else { 333 | //检测是否撞到边界 334 | if(borderDetection(head->sprite)) { 335 | gameOverFlag=true; 336 | } 337 | //检测是否撞到蛇身 338 | for(int i=2;istopBackgroundMusic(); 349 | CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(GAMEOVER_SOUND); 350 | double PI=3.14159f; 351 | 352 | for(int i=0;idir; 359 | } 360 | double angle=(rand()%150+15)*PI/180; 361 | if(dir==DIR_DEF::UP) angle=-angle; 362 | else if(dir==DIR_DEF::LEFT) angle=angle-PI/2; 363 | else if(dir==DIR_DEF::RIGHT) angle=angle+PI/2; 364 | 365 | int x=node->x+cos(angle)*snakeNodeWidth*100.0f; 366 | int y=node->y+sin(angle)*snakeNodeWidth*100.0f; 367 | //log("%d %d angle: %d %d,%d",node->x,node->y,(int)(angle/PI*180),x,y); 368 | float duration =speed*20.0f;//+(CCRANDOM_0_1()-0.5f)*speed*0.2 ; 369 | auto actionMove = MoveTo::create(duration, 370 | Point(x, y)); 371 | 372 | node->sprite->runAction(Sequence::create(actionMove, NULL, NULL)); 373 | } 374 | 375 | } 376 | } 377 | else { 378 | gameOverDelay--; 379 | if(gameOverDelay<0) gameOver(); 380 | } 381 | } 382 | 383 | void GameScene::gameOver(){ 384 | CCLOG("Game Over"); 385 | CocosDenshion::SimpleAudioEngine::getInstance()->stopAllEffects();//停止全部音效 386 | Director::getInstance()->replaceScene(HelloWorld::createScene()); 387 | } 388 | 389 | Scene* GameScene::createScene() 390 | { 391 | auto scene = Scene::create(); 392 | auto layer = GameScene::create(); 393 | scene->addChild(layer); 394 | return scene; 395 | } 396 | 397 | void GameScene::menuBackToMain( cocos2d::Ref* pSender ) { 398 | Director::getInstance()->replaceScene(HelloWorld::createScene()); 399 | } 400 | 401 | bool GameScene::borderDetection(cocos2d::Sprite* sprite){ 402 | Point pt=sprite->getPosition(); 403 | Size sz=sprite->getContentSize(); 404 | if(pt.x<0 || pt.y<0 || pt.x+sz.width>=winWidth || pt.y+sz.height>=winHeight) return true; 405 | else return false; 406 | } 407 | 408 | bool GameScene::collisionDetection(SnakeNode* node1,SnakeNode* node2){ 409 | Size sz1=node1->sprite->getContentSize(); 410 | int p1x1=node1->x; 411 | int p1y1=node1->y; 412 | int p1x2=p1x1+sz1.width-1; 413 | int p1y2=p1y1+sz1.height-1; 414 | Size sz2=node2->sprite->getContentSize(); 415 | int p2x1=node2->x; 416 | int p2y1=node2->y; 417 | int p2x2=p2x1+sz2.width-1; 418 | int p2y2=p2y1+sz2.height-1; 419 | //CCLOG("%d %d %d %d - %d %d %d %d ",p1x1,p1x2,p1y1,p1y2,p2x1,p2x2,p2y1,p2y2); 420 | if(p1x1>p2x2 || p1x2p2y2 || p1y2 snake; 56 | int controlDir; 57 | int snakeLength; 58 | int snakeNodeWidth; 59 | int snakeStep; 60 | int snakeAllStep; 61 | cocos2d::Vector dirChain;//方向链 62 | cocos2d::Label* labelScore; 63 | 64 | SnakeNode* genEgg(); 65 | void snakeMove(int dir); 66 | 67 | void play(float t); 68 | void gameOver(); 69 | bool onTouchBegan( cocos2d::Touch* touch, cocos2d::Event* event ); 70 | void onTouchEnded( cocos2d::Touch* touch, cocos2d::Event* event ); 71 | int touch_x; 72 | int touch_y; 73 | void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event); 74 | void onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event); 75 | 76 | bool borderDetection(cocos2d::Sprite* sprite); 77 | bool collisionDetection(SnakeNode* node1,SnakeNode* node2); 78 | }; 79 | #endif // __HELLOWORLD_SCENE_H__ 80 | -------------------------------------------------------------------------------- /pashan/Resources/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/pashan/Resources/HelloWorld.png -------------------------------------------------------------------------------- /pashan/Resources/background.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/pashan/Resources/background.wav -------------------------------------------------------------------------------- /pashan/Resources/effect.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/pashan/Resources/effect.wav -------------------------------------------------------------------------------- /pashan/Resources/egg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/pashan/Resources/egg.png -------------------------------------------------------------------------------- /pashan/Resources/gameover.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/pashan/Resources/gameover.wav -------------------------------------------------------------------------------- /pashan/Resources/snake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/pashan/Resources/snake.png -------------------------------------------------------------------------------- /small/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/small/Classes/HelloWorldScene.cpp -------------------------------------------------------------------------------- /small/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/small/Classes/HelloWorldScene.h -------------------------------------------------------------------------------- /start530/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("My Game"); 20 | director->setOpenGLView(glview); 21 | } 22 | 23 | // glview->setFrameSize(1000,1000); 24 | 25 | // turn on display FPS 26 | director->setDisplayStats(true); 27 | 28 | // set FPS. the default value is 1.0/60 if you don't call this 29 | director->setAnimationInterval(1.0 / 60); 30 | 31 | // create a scene. it's an autorelease object 32 | auto scene = HelloWorld::createScene(); 33 | 34 | // run 35 | director->runWithScene(scene); 36 | 37 | return true; 38 | } 39 | 40 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 41 | void AppDelegate::applicationDidEnterBackground() { 42 | Director::getInstance()->stopAnimation(); 43 | 44 | // if you use SimpleAudioEngine, it must be pause 45 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 46 | } 47 | 48 | // this function will be called when the app is active again 49 | void AppDelegate::applicationWillEnterForeground() { 50 | Director::getInstance()->startAnimation(); 51 | 52 | // if you use SimpleAudioEngine, it must resume here 53 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 54 | } 55 | -------------------------------------------------------------------------------- /start530/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /start530/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/start530/Classes/HelloWorldScene.cpp -------------------------------------------------------------------------------- /start530/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/start530/Classes/HelloWorldScene.h -------------------------------------------------------------------------------- /waitingfy.com/github.md: -------------------------------------------------------------------------------- 1 | ###H5贪吃蛇源码 2 | --- 3 | [https://github.com/waitingfy/snake](https://github.com/waitingfy/snake) -------------------------------------------------------------------------------- /wind_zero/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("My Game"); 20 | director->setOpenGLView(glview); 21 | } 22 | glview->setDesignResolutionSize(480, 320, ResolutionPolicy::EXACT_FIT); 23 | 24 | // turn on display FPS 25 | director->setDisplayStats(false); 26 | 27 | // set FPS. the default value is 1.0/60 if you don't call this 28 | director->setAnimationInterval(1.0 / 60); 29 | 30 | // create a scene. it's an autorelease object 31 | auto scene = HelloWorld::createScene(); 32 | 33 | // run 34 | director->runWithScene(scene); 35 | 36 | return true; 37 | } 38 | 39 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 40 | void AppDelegate::applicationDidEnterBackground() { 41 | Director::getInstance()->stopAnimation(); 42 | 43 | // if you use SimpleAudioEngine, it must be pause 44 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 45 | } 46 | 47 | // this function will be called when the app is active again 48 | void AppDelegate::applicationWillEnterForeground() { 49 | Director::getInstance()->startAnimation(); 50 | 51 | // if you use SimpleAudioEngine, it must resume here 52 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 53 | } 54 | -------------------------------------------------------------------------------- /wind_zero/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /wind_zero/Classes/HelloWorldScene.cpp: -------------------------------------------------------------------------------- 1 | #include "HelloWorldScene.h" 2 | #include "CCEventDispatcher.h" 3 | 4 | USING_NS_CC; 5 | 6 | Scene* HelloWorld::createScene() 7 | { 8 | auto scene = Scene::create(); 9 | auto layer = HelloWorld::create(); 10 | scene->addChild(layer); 11 | return scene; 12 | } 13 | 14 | bool HelloWorld::init() 15 | { 16 | if ( !Layer::init() ) 17 | { 18 | return false; 19 | } 20 | 21 | Size visibleSize = Director::getInstance()->getVisibleSize(); 22 | Point origin = Director::getInstance()->getVisibleOrigin(); 23 | 24 | auto labelStart = Label::create("StartGame", "Arial", 24); 25 | auto labelHelp = Label::create("GameHelp", "Arial", 24); 26 | auto labelExit = Label::create("ExitGame", "Arial", 24); 27 | 28 | auto startItem = MenuItemLabel::create(labelStart, CC_CALLBACK_1(HelloWorld::menuCallback, this)); 29 | startItem->setTag(START); 30 | startItem->setPosition(Point(100, 200)); 31 | 32 | auto helpItem = MenuItemLabel::create(labelHelp, CC_CALLBACK_1(HelloWorld::menuCallback, this)); 33 | helpItem->setTag(HELP); 34 | helpItem->setPosition(Point(100, 150)); 35 | 36 | auto exitItem = MenuItemLabel::create(labelExit, CC_CALLBACK_1(HelloWorld::menuCallback, this)); 37 | exitItem->setTag(EXIT); 38 | exitItem->setPosition(Point(100, 50)); 39 | 40 | auto closeItem = MenuItemImage::create( 41 | "CloseNormal.png", 42 | "CloseSelected.png", 43 | CC_CALLBACK_1(HelloWorld::menuCallback, this)); 44 | 45 | closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , 46 | origin.y + closeItem->getContentSize().height/2)); 47 | closeItem->setTag(EXIT); 48 | auto menu = Menu::create(startItem, helpItem, exitItem, closeItem, NULL); 49 | menu->setPosition(Point::ZERO); 50 | this->addChild(menu, 1); 51 | 52 | auto label = Label::create("Snake", "Arial", 24); 53 | label->setPosition(Point(origin.x + visibleSize.width/2, 54 | origin.y + visibleSize.height - label->getContentSize().height)); 55 | this->addChild(label, 1); 56 | 57 | return true; 58 | } 59 | 60 | 61 | void HelloWorld::menuCallback(Ref* pSender) 62 | { 63 | switch (((Node*)pSender)->getTag()) 64 | { 65 | case START: 66 | CCLOG("go to game"); 67 | Director::getInstance()->replaceScene(GameScene::createScene()); 68 | break; 69 | case HELP: 70 | CCLOG("go to help"); 71 | Director::getInstance()->replaceScene(GameHelp::createScene()); 72 | break; 73 | case EXIT: 74 | Director::getInstance()->end(); 75 | #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 76 | exit(0); 77 | #endif 78 | } 79 | } 80 | 81 | bool GameHelp::init() 82 | { 83 | if (!Layer::init()) 84 | { 85 | return false; 86 | } 87 | 88 | Size visibleSize = Director::getInstance()->getVisibleSize(); 89 | 90 | auto labelHelp = Label::create("please touch screen to play the game", "Arial", 15); 91 | labelHelp->setPosition(Point(visibleSize.width / 2, 280)); 92 | labelHelp->setAnchorPoint(Point::ANCHOR_MIDDLE); 93 | this->addChild(labelHelp); 94 | 95 | auto labelBack = Label::create("Main Menu", "Arial", 15); 96 | auto itemBack = MenuItemLabel::create(labelBack, CC_CALLBACK_1(GameHelp::menuBackToMain, this)); 97 | itemBack->setPosition(Point(visibleSize.width / 2, 160)); 98 | 99 | auto menu = Menu::create(itemBack, NULL); 100 | menu->setPosition(Point(0, 0)); 101 | this->addChild(menu); 102 | 103 | return true; 104 | } 105 | 106 | Scene* GameHelp::createScene() 107 | { 108 | auto scene = Scene::create(); 109 | auto layer = GameHelp::create(); 110 | scene->addChild(layer); 111 | return scene; 112 | } 113 | 114 | void GameHelp::menuBackToMain( cocos2d::Ref* pSender ) 115 | { 116 | Director::getInstance()->replaceScene(HelloWorld::createScene()); 117 | } 118 | 119 | bool GameScene::init() 120 | { 121 | if (!Layer::init()) 122 | { 123 | return false; 124 | } 125 | 126 | Size visibleSize = Director::getInstance()->getVisibleSize(); 127 | 128 | auto labelBack = Label::create("Main Menu", "Arial", 15); 129 | auto itemBack = MenuItemLabel::create(labelBack, CC_CALLBACK_1(GameScene::menuBackToMain, this)); 130 | itemBack->setPosition(Point(visibleSize.width, 0)); 131 | itemBack->setAnchorPoint(Point::ANCHOR_BOTTOM_RIGHT); 132 | 133 | auto menu = Menu::create(itemBack, NULL); 134 | menu->setPosition(Point(0, 0)); 135 | this->addChild(menu); 136 | 137 | sHead = new SnakeNode(); 138 | sHead->row = rand()%10; 139 | sHead->col = rand()%15; 140 | sHead->preNode = NULL; 141 | 142 | sFood = new SnakeNode(); 143 | sFood->row = rand()%10; 144 | sFood->col = rand()%15; 145 | 146 | auto snakeNode = Sprite::create("node_snake.png"); 147 | snakeNode->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); 148 | snakeNode->setPosition(Point(sHead->col * 32, sHead->row * 32)); 149 | sHead->nodeSprite = snakeNode; 150 | this->addChild(snakeNode); 151 | 152 | auto foodNode = Sprite::create("node_food.png"); 153 | foodNode->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); 154 | foodNode->setPosition(Point(sFood->col * 32, sFood->row * 32)); 155 | sFood->nodeSprite = foodNode; 156 | this->addChild(foodNode); 157 | 158 | auto touchListener = EventListenerTouchOneByOne::create(); 159 | touchListener->setSwallowTouches(true); 160 | 161 | touchListener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); 162 | 163 | Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this); 164 | 165 | this->schedule(SEL_SCHEDULE(&GameScene::logic), 0.5f); 166 | return true; 167 | } 168 | 169 | Scene* GameScene::createScene() 170 | { 171 | auto scene = Scene::create(); 172 | auto layer = GameScene::create(); 173 | scene->addChild(layer); 174 | return scene; 175 | } 176 | 177 | void GameScene::menuBackToMain( cocos2d::Ref* pSender ) 178 | { 179 | Director::getInstance()->replaceScene(HelloWorld::createScene()); 180 | } 181 | 182 | bool GameScene::onTouchBegan( Touch* touch, Event* event ) 183 | { 184 | Point touchPoint = touch->getLocation(); 185 | int touchRow = ((int)touchPoint.y) / 32; 186 | int touchCol = ((int)touchPoint.x) / 32; 187 | 188 | if (abs(touchRow - sHead->row) > abs(touchCol - sHead->col)) 189 | { 190 | if (touchRow > sHead->row) 191 | { 192 | sHead->dir = DIR_DEF::UP; 193 | } 194 | else 195 | { 196 | sHead->dir = DIR_DEF::DOWN; 197 | } 198 | } 199 | else 200 | { 201 | if (touchCol > sHead->col) 202 | { 203 | sHead->dir = DIR_DEF::RIGHT; 204 | } 205 | else 206 | { 207 | sHead->dir = DIR_DEF::LEFT; 208 | } 209 | } 210 | 211 | return true; 212 | } 213 | 214 | void GameScene::logic( float t ) 215 | { 216 | for (int i = allbody.size() - 1; i >= 0; i--) 217 | { 218 | auto sn = allbody.at(i); 219 | sn->dir = sn->preNode->dir; 220 | sn->row = sn->preNode->row; 221 | sn->col = sn->preNode->col; 222 | sn->nodeSprite->setPosition(Point(sn->col * 32, sn->row *32)); 223 | } 224 | 225 | switch (sHead->dir) 226 | { 227 | case DIR_DEF::UP: 228 | sHead->row ++; 229 | if (sHead->row >= 10) 230 | { 231 | sHead->row = 0; 232 | } 233 | break; 234 | case DIR_DEF::DOWN: 235 | sHead->row --; 236 | if (sHead->row <= 0) 237 | { 238 | sHead->row = 9; 239 | } 240 | break; 241 | case DIR_DEF::LEFT: 242 | sHead->col --; 243 | if (sHead->col <= 0) 244 | { 245 | sHead->col = 14; 246 | } 247 | break; 248 | case DIR_DEF::RIGHT: 249 | sHead->col ++; 250 | if (sHead->col >= 15) 251 | { 252 | sHead->col = 0; 253 | } 254 | break; 255 | } 256 | sHead->nodeSprite->setPosition(Point(sHead->col * 32, sHead->row * 32)); 257 | 258 | if (sHead->col == sFood->col && sHead->row == sFood->row) 259 | { 260 | sFood->row = rand()%10; 261 | sFood->col = rand()%15; 262 | sFood->nodeSprite->setPosition(Point(sFood->col * 32, sFood->row * 32)); 263 | auto snakeNode = new SnakeNode(); 264 | if (allbody.empty()) 265 | { 266 | snakeNode->preNode = sHead; 267 | } 268 | else 269 | { 270 | snakeNode->preNode = allbody.back(); 271 | } 272 | switch (snakeNode->preNode->dir) 273 | { 274 | case DIR_DEF::UP: 275 | snakeNode->row = snakeNode->preNode->row - 1; 276 | snakeNode->col = snakeNode->preNode->col; 277 | break; 278 | case DIR_DEF::DOWN: 279 | snakeNode->row = snakeNode->preNode->row + 1; 280 | snakeNode->col = snakeNode->preNode->col; 281 | break; 282 | case DIR_DEF::LEFT: 283 | snakeNode->row = snakeNode->preNode->row; 284 | snakeNode->col = snakeNode->preNode->col + 1; 285 | break; 286 | case DIR_DEF::RIGHT: 287 | snakeNode->row = snakeNode->preNode->row; 288 | snakeNode->col = snakeNode->preNode->col - 1; 289 | break; 290 | } 291 | 292 | auto nodeSprite = Sprite::create("node_snake.png"); 293 | nodeSprite->setPosition(Point(snakeNode->col * 32, snakeNode->row * 32)); 294 | nodeSprite->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT); 295 | snakeNode->nodeSprite = nodeSprite; 296 | this->addChild(nodeSprite); 297 | 298 | allbody.pushBack(snakeNode); 299 | } 300 | 301 | } 302 | -------------------------------------------------------------------------------- /wind_zero/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | 6 | typedef enum { 7 | UP=1, 8 | DOWN, 9 | LEFT, 10 | RIGHT 11 | }DIR_DEF; 12 | 13 | typedef enum { 14 | START=10, 15 | HELP, 16 | EXIT 17 | }TAG_MENU; 18 | 19 | class SnakeNode :public cocos2d::Ref 20 | { 21 | public: 22 | int row; 23 | int col; 24 | int dir; 25 | SnakeNode* preNode; 26 | cocos2d::Sprite* nodeSprite; 27 | }; 28 | 29 | class HelloWorld : public cocos2d::Layer 30 | { 31 | public: 32 | static cocos2d::Scene* createScene(); 33 | virtual bool init(); 34 | void menuCallback(cocos2d::Ref* pSender); 35 | CREATE_FUNC(HelloWorld); 36 | }; 37 | 38 | class GameHelp : public cocos2d::Layer 39 | { 40 | public: 41 | static cocos2d::Scene* createScene(); 42 | virtual bool init(); 43 | void menuBackToMain(cocos2d::Ref* pSender); 44 | CREATE_FUNC(GameHelp); 45 | }; 46 | 47 | class GameScene : public cocos2d::Layer 48 | { 49 | public: 50 | virtual bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); 51 | void logic(float t); 52 | static cocos2d::Scene* createScene(); 53 | virtual bool init(); 54 | void menuBackToMain(cocos2d::Ref* pSender); 55 | CREATE_FUNC(GameScene); 56 | 57 | protected: 58 | SnakeNode* sHead; 59 | SnakeNode* sFood; 60 | cocos2d::Vector allbody; 61 | }; 62 | 63 | #endif // __HELLOWORLD_SCENE_H__ 64 | -------------------------------------------------------------------------------- /wind_zero/Resources/CloseNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/wind_zero/Resources/CloseNormal.png -------------------------------------------------------------------------------- /wind_zero/Resources/CloseSelected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/wind_zero/Resources/CloseSelected.png -------------------------------------------------------------------------------- /wind_zero/Resources/fonts/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/wind_zero/Resources/fonts/Marker Felt.ttf -------------------------------------------------------------------------------- /wind_zero/Resources/node_food.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/wind_zero/Resources/node_food.png -------------------------------------------------------------------------------- /wind_zero/Resources/node_snake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/wind_zero/Resources/node_snake.png -------------------------------------------------------------------------------- /zhubingyi/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "HelloWorldScene.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | if(!glview) { 19 | glview = GLView::create("My Game"); 20 | director->setOpenGLView(glview); 21 | 22 | } 23 | 24 | glview->setDesignResolutionSize(480, 320, ResolutionPolicy::EXACT_FIT); 25 | 26 | // turn on display FPS 27 | director->setDisplayStats(true); 28 | 29 | // set FPS. the default value is 1.0/60 if you don't call this 30 | director->setAnimationInterval(1.0 / 60); 31 | 32 | // create a scene. it's an autorelease object 33 | auto scene = HelloWorld::createScene(); 34 | 35 | // run 36 | director->runWithScene(scene); 37 | 38 | return true; 39 | } 40 | 41 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 42 | void AppDelegate::applicationDidEnterBackground() { 43 | Director::getInstance()->stopAnimation(); 44 | 45 | // if you use SimpleAudioEngine, it must be pause 46 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 47 | } 48 | 49 | // this function will be called when the app is active again 50 | void AppDelegate::applicationWillEnterForeground() { 51 | Director::getInstance()->startAnimation(); 52 | 53 | // if you use SimpleAudioEngine, it must resume here 54 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 55 | } 56 | -------------------------------------------------------------------------------- /zhubingyi/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /zhubingyi/Classes/File.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // File.cpp 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #include "File.h" 10 | -------------------------------------------------------------------------------- /zhubingyi/Classes/File.h: -------------------------------------------------------------------------------- 1 | // 2 | // File.h 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #ifndef __Snake__File__ 10 | #define __Snake__File__ 11 | 12 | #include 13 | 14 | #endif /* defined(__Snake__File__) */ 15 | -------------------------------------------------------------------------------- /zhubingyi/Classes/GameLayer.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GameLayer.cpp 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #include "GameLayer.h" 10 | 11 | 12 | 13 | GameLayer::GameLayer() 14 | { 15 | 16 | } 17 | 18 | GameLayer::~GameLayer() 19 | { 20 | 21 | } 22 | 23 | Scene* GameLayer::GameScene(){ 24 | Scene* gamescene = Scene::create(); 25 | GameLayer* gamelayer = GameLayer::create(); 26 | gamescene->addChild(gamelayer); 27 | return gamescene; 28 | } 29 | 30 | bool GameLayer::init(){ 31 | bool bRect = false; 32 | 33 | do { 34 | ifgameover = false; 35 | SetApperance(); 36 | SetSnake(); 37 | SetFood(); 38 | this->schedule(schedule_selector(GameLayer::update), 0.6); 39 | bRect = true; 40 | } while (0); 41 | 42 | return bRect; 43 | } 44 | 45 | void GameLayer::update(float dt){ 46 | if (!HeadCollideBody(head->getDirec()) && !OutofRange()) { 47 | MoveStep(); 48 | if(ifGetFood()){ 49 | AddBody(); 50 | SetFood(); 51 | } 52 | lastbodyposi = body.at(body.size()-1)->getPosition(); 53 | } 54 | else{ 55 | GameOver(); 56 | } 57 | } 58 | 59 | void GameLayer::SetApperance(){ 60 | visiblesize = Director::getInstance()->getVisibleSize(); 61 | 62 | background = Sprite::create("BackGround.png"); 63 | background->setPosition(Point(visiblesize.width/2,visiblesize.height/2)); 64 | this->addChild(background,0); //this is the background 65 | 66 | butup = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,up)); 67 | Size bs = butup->getContentSize(); 68 | butup->setPosition(Point(visiblesize.width-3*bs.width+bs.width/2,3*bs.height+bs.height/2)); 69 | 70 | butdown = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,down)); 71 | butdown ->setPosition(Point(visiblesize.width-3*bs.width+bs.width/2,bs.height+bs.height/2)); 72 | 73 | butleft = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,left)); 74 | butleft ->setPosition(Point(visiblesize.width-4*bs.width+bs.width/2,2*bs.height+bs.height/2)); 75 | 76 | butright = MenuItemImage::create("Button.png","Button2.png","Button.png",CC_CALLBACK_1(GameLayer::SetDirection,this,right)); 77 | butright ->setPosition(Point(visiblesize.width-2*bs.width+bs.width/2,2*bs.height+bs.height/2)); 78 | 79 | pausebutton = MenuItemImage::create("PauseButton.png","PauseButton.png",CC_CALLBACK_0(GameLayer::PauseGame,this)); 80 | pausebutton->setPosition(Point(visiblesize.width - pausebutton->getContentSize().width,visiblesize.height - pausebutton->getContentSize().height/2)); 81 | 82 | playbutton = MenuItemImage::create("rePlayButton1.png","rePlayButton2.png",CC_CALLBACK_0(GameLayer::StartGame, this)); 83 | playbutton->setPosition(Point(visiblesize.width/2,visiblesize.height/2)); 84 | playbutton->setVisible(false); 85 | 86 | menu = Menu::create(butup,butdown,butleft,butright,pausebutton,playbutton,NULL); 87 | menu->setPosition(Point::ZERO); 88 | this->addChild(menu,2); 89 | 90 | TTFConfig ttfconfig("Marker Felt.ttf",40); 91 | gameover = Label::createWithTTF(ttfconfig, " Game Over !",TextHAlignment::CENTER); 92 | gameover->setPosition(Point(visiblesize.width/2,visiblesize.height/2+gameover->getContentSize().height)); 93 | gameover->setVisible(false); 94 | gameover->setColor(Color3B::BLACK); 95 | this->addChild(gameover,2); 96 | 97 | 98 | 99 | } 100 | 101 | void GameLayer::SetSnake(){ 102 | body.clear(); 103 | Size visiblesize = Director::getInstance()->getVisibleSize(); 104 | head = SnakeHead::create(); 105 | head->setNode(Sprite::create("SnakeHead.png")); 106 | head->setPosition(Point(visiblesize.width/2-head->getNode()->getContentSize().width/2,visiblesize.height/2-head->getNode()->getContentSize().height/2)); 107 | this->addChild(head,1); 108 | 109 | for (int i=1 ; i < 4; i++) { 110 | Snake* bodynode = Snake::create(); 111 | bodynode->setNode(Sprite::create("Snake.png")); 112 | bodynode->setPosition(Point(head->getPosition().x+i*bodynode->getNode()->getContentSize().width,head->getPosition().y)); 113 | body.pushBack(bodynode); 114 | this->addChild(bodynode); 115 | } 116 | 117 | } 118 | 119 | void GameLayer::SetDirection(Ref* psender,Direction direc){ 120 | Direction headdir = head->getDirec(); 121 | if (movedflag && direc!= headdir) { 122 | switch (direc) { 123 | case up: 124 | if(headdir==left||headdir==right){ 125 | head->setDirec(up); 126 | } 127 | break; 128 | case down: 129 | if(headdir==left||headdir==right){ 130 | head->setDirec(down); 131 | } 132 | break; 133 | case left: 134 | if(headdir==up||headdir==down){ 135 | head->setDirec(left); 136 | } 137 | break; 138 | case right: 139 | if(headdir==up||headdir==down){ 140 | head->setDirec(right); 141 | } 142 | break; 143 | default: 144 | break; 145 | } 146 | movedflag = false; 147 | }else{ 148 | 149 | } 150 | 151 | 152 | } 153 | 154 | void GameLayer::MoveStep(){ 155 | movedflag = true; 156 | Direction temp = head->getDirec(); 157 | Point po = head->Node::getPosition(); 158 | switch (temp) { 159 | case up: 160 | //log("upupup"); 161 | po.y += 20; 162 | break; 163 | case down: 164 | po.y -= 20; 165 | break; 166 | case left: 167 | po.x -= 20; 168 | break; 169 | case right: 170 | po.x += 20; 171 | break; 172 | 173 | default: 174 | break; 175 | } 176 | MoveBody(); 177 | head->setPosition(po); 178 | 179 | } 180 | 181 | bool GameLayer::OutofRange(){ 182 | Point po = head->getPosition(); 183 | if (po.x < 0||po.x > visiblesize.width||po.y < 0||po.y > visiblesize.height) { 184 | return true; 185 | } 186 | return false; 187 | } 188 | 189 | void GameLayer::MoveBody(){ 190 | int n=body.size(); 191 | Point HeadPosition = head->getPosition(); 192 | Point a,temp; 193 | for (int i = 0; i < n; i++) { 194 | if(i==0){ 195 | a = body.at(i)->getPosition(); 196 | body.at(i)->setPosition(HeadPosition); 197 | } 198 | else { 199 | temp = a; 200 | a = body.at(i)->getPosition(); 201 | body.at(i)->setPosition(temp); 202 | } 203 | } 204 | 205 | } 206 | 207 | bool GameLayer::HeadCollideBody(Direction headdirec){ 208 | float x = head->getPosition().x; 209 | float y = head->getPosition().y; 210 | bool iscollide = false; 211 | switch (headdirec) { 212 | case up: 213 | y += 20; 214 | break; 215 | case down: 216 | y -= 20; 217 | break; 218 | case left: 219 | x -= 20; 220 | break; 221 | case right: 222 | x += 20; 223 | break; 224 | default: 225 | break; 226 | } 227 | Point headnextpos = Point (x,y); 228 | iscollide = ifCollideBody(headnextpos); 229 | return iscollide; 230 | } 231 | 232 | void GameLayer::SetFood(){ 233 | this->removeChild(food); 234 | Point foodposi = RandomPosition(); 235 | Point headposi = head->getPosition(); 236 | while (foodposi==headposi||ifCollideBody(foodposi)) { 237 | foodposi = RandomPosition(); 238 | } 239 | food = Sprite::create("Food.png"); 240 | food->setPosition(foodposi); 241 | this->addChild(food); 242 | } 243 | 244 | Point GameLayer::RandomPosition(){ 245 | int x = (arc4random()%24); 246 | int y = (arc4random()%16); 247 | Point position = Point(x*20+10,y*20+10); 248 | return position; 249 | } 250 | 251 | bool GameLayer::ifCollideBody(Point pos){ 252 | bool value = false; 253 | Snake* node; 254 | for (int i =0; igetPosition(); 257 | if(nodepos==pos){ 258 | value = true; 259 | } 260 | } 261 | return value; 262 | } 263 | 264 | bool GameLayer::ifGetFood(){ 265 | bool value = false; 266 | if(food->getPosition() == head->getPosition()){ 267 | value = true; 268 | } 269 | return value; 270 | } 271 | 272 | void GameLayer::AddBody(){ 273 | head->setPosition(food->getPosition()); 274 | Snake* node = Snake::create(); 275 | node->setNode(Sprite::create("Snake.png")); 276 | node->setPosition(lastbodyposi); 277 | body.pushBack(node); 278 | this->addChild(node); 279 | } 280 | 281 | void GameLayer::PauseGame(){ 282 | 283 | this->pause(); 284 | SetSnakeVisible(false); 285 | playbutton->setVisible(true); 286 | 287 | } 288 | 289 | void GameLayer::GameOver(){ 290 | PauseGame(); 291 | SetSnakeVisible(false); 292 | playbutton->setPosition(Point(visiblesize.width/2,visiblesize.height/2-gameover->getContentSize().height)); 293 | gameover->setVisible(true); 294 | ifgameover = true; 295 | } 296 | 297 | void GameLayer::StartGame(){ 298 | if(!ifgameover){ 299 | SetSnakeVisible(true); 300 | this->resume(); 301 | playbutton->setVisible(false); 302 | }else{ 303 | this->unscheduleAllSelectors(); 304 | this->removeAllChildren(); 305 | //body.clear(); 306 | this->init(); 307 | 308 | } 309 | } 310 | 311 | void GameLayer::SetSnakeVisible(bool val){ 312 | food->setVisible(val); 313 | head->getNode()->setVisible(val); 314 | for (int i =0; isetVisible(val); 316 | } 317 | } 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | -------------------------------------------------------------------------------- /zhubingyi/Classes/GameLayer.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameLayer.h 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #ifndef __Snake__GameLayer__ 10 | #define __Snake__GameLayer__ 11 | 12 | #include 13 | #include "cocos2d.h" 14 | #include "SnakeHead.h" 15 | 16 | USING_NS_CC; 17 | 18 | class GameLayer : public Layer{ 19 | 20 | public: 21 | GameLayer(void); 22 | ~GameLayer(void); 23 | virtual bool init(); 24 | virtual void update(float dt); 25 | CREATE_FUNC(GameLayer); 26 | 27 | void SetApperance(); 28 | void SetSnake(); 29 | void SetDirection(Ref* psender,Direction direc); 30 | void MoveStep(); 31 | bool OutofRange(); 32 | bool HeadCollideBody(Direction headdirec); 33 | void MoveBody(); 34 | void SetFood(); 35 | Point RandomPosition(); 36 | bool ifCollideBody(Point pos); 37 | bool ifGetFood(); 38 | void AddBody(); 39 | void SetGameScore(int gs); 40 | static Scene* GameScene(); 41 | void PauseGame(); 42 | void GameOver(); 43 | void StartGame(); 44 | void SetSnakeVisible(bool value); 45 | 46 | private: 47 | SnakeHead* head; 48 | Vector body; 49 | Sprite* background; 50 | Sprite* food; 51 | MenuItemImage* butup; 52 | MenuItemImage* butdown; 53 | MenuItemImage* butleft; 54 | MenuItemImage* butright; 55 | MenuItemImage* pausebutton; 56 | MenuItemImage* playbutton; 57 | Menu* menu; 58 | Label* gameover; 59 | bool ifgameover; 60 | Point lastbodyposi; 61 | Size visiblesize; 62 | bool movedflag; 63 | 64 | 65 | 66 | 67 | 68 | }; 69 | 70 | 71 | #endif /* defined(__Snake__GameLayer__) */ 72 | -------------------------------------------------------------------------------- /zhubingyi/Classes/GameScene.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // GameScene.cpp 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-18. 6 | // 7 | // 8 | 9 | #include "GameScene.h" 10 | #include "cocos2d.h" 11 | 12 | USING_NS_CC; 13 | 14 | -------------------------------------------------------------------------------- /zhubingyi/Classes/GameScene.h: -------------------------------------------------------------------------------- 1 | // 2 | // GameScene.h 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-18. 6 | // 7 | // 8 | 9 | #ifndef __Snake__GameScene__ 10 | #define __Snake__GameScene__ 11 | 12 | #include 13 | 14 | #endif /* defined(__Snake__GameScene__) */ 15 | -------------------------------------------------------------------------------- /zhubingyi/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 | if ( !Layer::init() ) 25 | { 26 | return false; 27 | } 28 | 29 | Size visibleSize = Director::getInstance()->getVisibleSize(); 30 | //Point origin = Director::getInstance()->getVisibleOrigin(); 31 | background = Sprite::create("StartScene.png"); 32 | background->setPosition(Point(visibleSize.width/2,visibleSize.height/2)); 33 | this->addChild(background); 34 | 35 | playbutton = MenuItemImage::create("PlayButton1.png","PlayButton2.png",CC_CALLBACK_0(HelloWorld::ChangeScene,this)); 36 | playbutton ->setPosition(Point(visibleSize.width/2,visibleSize.height/2-1.5*playbutton->getContentSize().height)); 37 | 38 | menu = Menu::create(playbutton,NULL); 39 | menu->setPosition(Point::ZERO); 40 | this->addChild(menu); 41 | 42 | return true; 43 | } 44 | 45 | void HelloWorld::ChangeScene(){ 46 | //log("Change"); 47 | Director::getInstance()->replaceScene(TransitionProgressInOut::create(0.5f,GameLayer::GameScene())); 48 | } 49 | 50 | 51 | -------------------------------------------------------------------------------- /zhubingyi/Classes/HelloWorldScene.h: -------------------------------------------------------------------------------- 1 | #ifndef __HELLOWORLD_SCENE_H__ 2 | #define __HELLOWORLD_SCENE_H__ 3 | 4 | #include "cocos2d.h" 5 | #include "GameLayer.h" 6 | 7 | class HelloWorld : public cocos2d::Layer 8 | { 9 | public: 10 | // there's no 'id' in cpp, so we recommend returning the class instance pointer 11 | static cocos2d::Scene* createScene(); 12 | 13 | // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 14 | virtual bool init(); 15 | 16 | // a selector callback 17 | 18 | 19 | 20 | // implement the "static create()" method manually 21 | CREATE_FUNC(HelloWorld); 22 | 23 | void ChangeScene(); 24 | 25 | //GameLayer* gamelayer; 26 | private: 27 | 28 | Sprite* background; 29 | MenuItemImage* playbutton; 30 | Menu* menu; 31 | 32 | 33 | }; 34 | 35 | 36 | 37 | #endif // __HELLOWORLD_SCENE_H__ 38 | -------------------------------------------------------------------------------- /zhubingyi/Classes/Snake.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Snake.cpp 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #include "Snake.h" 10 | 11 | 12 | Sprite* Snake::getNode(){ 13 | return this->snakenode; 14 | } 15 | 16 | void Snake::setNode(Sprite* node){ 17 | this->snakenode = node; 18 | this->addChild(snakenode); 19 | } 20 | 21 | bool Snake::init(){ 22 | return true; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /zhubingyi/Classes/Snake.h: -------------------------------------------------------------------------------- 1 | // 2 | // Snake.h 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #ifndef __Snake__Snake__ 10 | #define __Snake__Snake__ 11 | 12 | #include 13 | 14 | #include "cocos2d.h" 15 | 16 | using namespace cocos2d; 17 | 18 | class Snake : public Node{ 19 | 20 | private: 21 | Sprite* snakenode; 22 | public: 23 | void setNode(Sprite* node); 24 | Sprite* getNode(); 25 | CREATE_FUNC(Snake); 26 | virtual bool init(); 27 | 28 | }; 29 | 30 | 31 | #endif /* defined(__Snake__Snake__) */ 32 | -------------------------------------------------------------------------------- /zhubingyi/Classes/SnakeHead.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // SnakeHead.cpp 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #include "SnakeHead.h" 10 | 11 | bool SnakeHead::init(){ 12 | return true; 13 | } 14 | 15 | SnakeHead::SnakeHead(){ 16 | this->direction = up; 17 | } 18 | 19 | SnakeHead::~SnakeHead(){ 20 | 21 | } 22 | 23 | Direction SnakeHead::getDirec(){ 24 | return this->direction; 25 | } 26 | 27 | void SnakeHead::setDirec(Direction di){ 28 | this->direction = di; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /zhubingyi/Classes/SnakeHead.h: -------------------------------------------------------------------------------- 1 | // 2 | // SnakeHead.h 3 | // Snake 4 | // 5 | // Created by cookie on 14-4-13. 6 | // 7 | // 8 | 9 | #ifndef __Snake__SnakeHead__ 10 | #define __Snake__SnakeHead__ 11 | 12 | #include 13 | 14 | #include "cocos2d.h" 15 | #include "Snake.h" 16 | 17 | USING_NS_CC; 18 | 19 | enum Direction { 20 | up = 0, 21 | down = 1, 22 | left = 2, 23 | right = 3, 24 | }; 25 | 26 | class SnakeHead : public Snake{ 27 | private: 28 | Direction direction; 29 | public: 30 | CREATE_FUNC(SnakeHead); 31 | virtual bool init(); 32 | SnakeHead(); 33 | ~SnakeHead(); 34 | void setDirec(Direction di); 35 | Direction getDirec(); 36 | 37 | }; 38 | 39 | #endif /* defined(__Snake__SnakeHead__) */ 40 | -------------------------------------------------------------------------------- /zhubingyi/Resources/BackGround.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/BackGround.png -------------------------------------------------------------------------------- /zhubingyi/Resources/BackGround.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/BackGround.psd -------------------------------------------------------------------------------- /zhubingyi/Resources/Button.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Button.png -------------------------------------------------------------------------------- /zhubingyi/Resources/Button.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Button.psd -------------------------------------------------------------------------------- /zhubingyi/Resources/Button2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Button2.png -------------------------------------------------------------------------------- /zhubingyi/Resources/CloseNormal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/CloseNormal.png -------------------------------------------------------------------------------- /zhubingyi/Resources/CloseSelected.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/CloseSelected.png -------------------------------------------------------------------------------- /zhubingyi/Resources/Food.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Food.png -------------------------------------------------------------------------------- /zhubingyi/Resources/HelloWorld.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/HelloWorld.png -------------------------------------------------------------------------------- /zhubingyi/Resources/Marker Felt.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Marker Felt.ttf -------------------------------------------------------------------------------- /zhubingyi/Resources/PauseButton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/PauseButton.png -------------------------------------------------------------------------------- /zhubingyi/Resources/Play.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Play.png -------------------------------------------------------------------------------- /zhubingyi/Resources/Play.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Play.psd -------------------------------------------------------------------------------- /zhubingyi/Resources/PlayButton.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/PlayButton.png -------------------------------------------------------------------------------- /zhubingyi/Resources/PlayButton.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/PlayButton.psd -------------------------------------------------------------------------------- /zhubingyi/Resources/PlayButton1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/PlayButton1.png -------------------------------------------------------------------------------- /zhubingyi/Resources/PlayButton2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/PlayButton2.png -------------------------------------------------------------------------------- /zhubingyi/Resources/Snake.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/Snake.png -------------------------------------------------------------------------------- /zhubingyi/Resources/SnakeHead.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/SnakeHead.png -------------------------------------------------------------------------------- /zhubingyi/Resources/StartScene.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/StartScene.png -------------------------------------------------------------------------------- /zhubingyi/Resources/StartScene.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/StartScene.psd -------------------------------------------------------------------------------- /zhubingyi/Resources/icon-114n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/icon-114n.png -------------------------------------------------------------------------------- /zhubingyi/Resources/icon-120.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/icon-120.png -------------------------------------------------------------------------------- /zhubingyi/Resources/icon-120n.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/icon-120n.png -------------------------------------------------------------------------------- /zhubingyi/Resources/icon-120n.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/icon-120n.psd -------------------------------------------------------------------------------- /zhubingyi/Resources/rePlayButton1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/rePlayButton1.png -------------------------------------------------------------------------------- /zhubingyi/Resources/rePlayButton2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zhubingyi/Resources/rePlayButton2.png -------------------------------------------------------------------------------- /zuoeryuner/Classes/AppDelegate.cpp: -------------------------------------------------------------------------------- 1 | #include "AppDelegate.h" 2 | #include "GameHome.h" 3 | 4 | USING_NS_CC; 5 | 6 | AppDelegate::AppDelegate() { 7 | 8 | } 9 | 10 | AppDelegate::~AppDelegate() 11 | { 12 | } 13 | 14 | bool AppDelegate::applicationDidFinishLaunching() { 15 | // initialize director 16 | auto director = Director::getInstance(); 17 | auto glview = director->getOpenGLView(); 18 | 19 | if(!glview) { 20 | glview = GLView::create("My Game"); 21 | director->setOpenGLView(glview); 22 | glview->setDesignResolutionSize(960,640,ResolutionPolicy::EXACT_FIT); 23 | } 24 | 25 | // turn on display FPS 26 | director->setDisplayStats(true); 27 | 28 | // set FPS. the default value is 1.0/60 if you don't call this 29 | director->setAnimationInterval(1.0 / 60); 30 | 31 | // create a scene. it's an autorelease object 32 | auto scene = GameHome::create(); 33 | 34 | // run 35 | director->runWithScene(scene); 36 | 37 | return true; 38 | } 39 | 40 | // This function will be called when the app is inactive. When comes a phone call,it's be invoked too 41 | void AppDelegate::applicationDidEnterBackground() { 42 | Director::getInstance()->stopAnimation(); 43 | 44 | // if you use SimpleAudioEngine, it must be pause 45 | // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 46 | } 47 | 48 | // this function will be called when the app is active again 49 | void AppDelegate::applicationWillEnterForeground() { 50 | Director::getInstance()->startAnimation(); 51 | 52 | // if you use SimpleAudioEngine, it must resume here 53 | // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 54 | } 55 | -------------------------------------------------------------------------------- /zuoeryuner/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 | /** 18 | @brief Implement Director and Scene init code here. 19 | @return true Initialize success, app continue. 20 | @return false Initialize failed, app terminate. 21 | */ 22 | virtual bool applicationDidFinishLaunching(); 23 | 24 | /** 25 | @brief The function be called when the application enter background 26 | @param the pointer of the application 27 | */ 28 | virtual void applicationDidEnterBackground(); 29 | 30 | /** 31 | @brief The function be called when the application enter foreground 32 | @param the pointer of the application 33 | */ 34 | virtual void applicationWillEnterForeground(); 35 | }; 36 | 37 | #endif // _APP_DELEGATE_H_ 38 | 39 | -------------------------------------------------------------------------------- /zuoeryuner/Classes/Game.cpp: -------------------------------------------------------------------------------- 1 | #include "Game.h" 2 | 3 | Size Game::kSnakeNodeSize = Size(30,30); 4 | 5 | void Game::onEnter() 6 | { 7 | Scene::onEnter(); 8 | 9 | this->schedule(schedule_selector(Game::snakeMove),0.2f); 10 | 11 | _istouchSnakeDirection = false; 12 | _snakeDirection = RIGHT; 13 | initSnake(); 14 | 15 | auto listener = EventListenerTouchOneByOne::create(); 16 | listener->onTouchBegan = [=](Touch* touch,Event*){ return true; }; 17 | listener->onTouchEnded = [=](Touch* touch,Event*){ touchSnakeDirection(touch->getLocation()); }; 18 | this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this); 19 | 20 | createApple(); 21 | } 22 | 23 | void Game::createSnakeNode() 24 | { 25 | auto drawNode = createDrawNode(Point::ZERO); 26 | _snake.push_back(drawNode); 27 | } 28 | 29 | DrawNode* Game::createDrawNode( const Point& pos ) 30 | { 31 | auto drawNode = DrawNode::create(); 32 | Size size(kSnakeNodeSize); 33 | Point point = Point(-size.width/2,size.height/2); 34 | Point points[4] = { 35 | point, 36 | Point(point.x,point.y-size.height), 37 | Point(point.x + size.width,point.y-size.height), 38 | Point(point.x+size.width,point.y)}; 39 | drawNode->drawPolygon(points,4,Color4F::WHITE,1,Color4F::RED); 40 | drawNode->setPosition(pos); 41 | this->addChild(drawNode); 42 | return drawNode; 43 | } 44 | 45 | void Game::createApple() 46 | { 47 | Point pos; 48 | bool b = false; 49 | do 50 | { 51 | Size size = Director::getInstance()->getVisibleSize(); 52 | pos.x = ( (int)(CCRANDOM_0_1() * (size.width/kSnakeNodeSize.width-1)) + 0.5f ) * kSnakeNodeSize.width; 53 | pos.y = ( (int)(CCRANDOM_0_1() * (size.height/kSnakeNodeSize.height-1)) + 0.5f ) * kSnakeNodeSize.height; 54 | for (auto m : _snake) 55 | { 56 | if (m->getPosition().equals(pos)) 57 | { 58 | b = true; 59 | break; 60 | } 61 | else 62 | { 63 | b = false; 64 | } 65 | } 66 | } while (b); 67 | _apple = createDrawNode(pos); 68 | } 69 | 70 | void Game::snakeMove( float ) 71 | { 72 | if (_istouchSnakeDirection) 73 | { 74 | _snakeDirection = _touchSnakeDirection; 75 | } 76 | 77 | for (size_t i = _snake.size()-1; i > 0 ;--i) 78 | { 79 | auto pos = _snake[i-1]->getPosition(); 80 | _snake[i]->setPosition(pos); 81 | } 82 | snakeHeadMove(); 83 | 84 | if (snakeIsDie()) 85 | { 86 | snakeDie(); 87 | } 88 | if (snakeIsAapple()) 89 | { 90 | createSnakeNode(); 91 | } 92 | } 93 | 94 | void Game::initSnake() 95 | { 96 | _snake.clear(); 97 | for (int i = 0; i < 3;++i) 98 | { 99 | auto drawNode = createDrawNode(Point(0-kSnakeNodeSize.width*i,0)+Point(kSnakeNodeSize*12.5f)); 100 | _snake.push_back(drawNode); 101 | } 102 | } 103 | 104 | void Game::touchSnakeDirection( const Point& tochPoint ) 105 | { 106 | _istouchSnakeDirection = false; 107 | if (abs(_snakeDirection) == UP_OR_DOWN) 108 | { 109 | if (tochPoint.x < _snake[0]->getPosition().x) 110 | { 111 | _touchSnakeDirection = LEFT; 112 | } 113 | else 114 | { 115 | _touchSnakeDirection = RIGHT; 116 | } 117 | _istouchSnakeDirection = true; 118 | } 119 | else if (abs(_snakeDirection) == LEFT_OR_RIGHT) 120 | { 121 | if (tochPoint.y < _snake[0]->getPosition().y) 122 | { 123 | _touchSnakeDirection = DOWN; 124 | } 125 | else 126 | { 127 | _touchSnakeDirection = UP; 128 | } 129 | _istouchSnakeDirection = true; 130 | } 131 | } 132 | 133 | bool Game::snakeIsDie() 134 | { 135 | auto pos = _snake[0]->getPosition(); 136 | for (size_t i = 1; i < _snake.size(); ++i) 137 | { 138 | if (_snake[i]->getPosition().equals(pos)) 139 | { 140 | return true; 141 | } 142 | } 143 | auto size = Director::getInstance()->getVisibleSize(); 144 | Rect rect(kSnakeNodeSize.width/2,kSnakeNodeSize.height/2, 145 | size.width - kSnakeNodeSize.width,size.height - kSnakeNodeSize.height); 146 | if (!rect.containsPoint(pos)) 147 | { 148 | return true; 149 | } 150 | return false; 151 | } 152 | 153 | void Game::snakeDie() 154 | { 155 | this->unscheduleAllSelectors(); 156 | 157 | auto title = Label::create("Game Over","fonts/Marker Felt.ttf",50); 158 | title->setAnchorPoint(Point::ANCHOR_MIDDLE); 159 | title->setPosition(480,520); 160 | title->enableShadow(Color3B::RED); 161 | this->addChild(title); 162 | 163 | auto closeLabel = Label::create("Restart","fonts/Marker Felt.ttf",40); 164 | closeLabel->enableShadow(Color3B::GREEN); 165 | auto menuItemLabel = MenuItemLabel::create(closeLabel,[](Ref*){ 166 | Director::getInstance()->replaceScene(Game::create()); }); 167 | auto menu = Menu::create(menuItemLabel,nullptr); 168 | this->addChild(menu); 169 | } 170 | 171 | void Game::snakeHeadMove() 172 | { 173 | switch (_snakeDirection) 174 | { 175 | case Game::UP: 176 | _snake[0]->setPositionY(_snake[0]->getPositionY()+kSnakeNodeSize.height); 177 | break; 178 | case Game::DOWN: 179 | _snake[0]->setPositionY(_snake[0]->getPositionY()-kSnakeNodeSize.height); 180 | break; 181 | case Game::LEFT: 182 | _snake[0]->setPositionX(_snake[0]->getPositionX()-kSnakeNodeSize.width); 183 | break; 184 | case Game::RIGHT: 185 | _snake[0]->setPositionX(_snake[0]->getPositionX()+kSnakeNodeSize.width); 186 | break; 187 | default: 188 | break; 189 | } 190 | } 191 | 192 | bool Game::snakeIsAapple() 193 | { 194 | auto posApple = _apple->getPosition(); 195 | auto posSnake = _snake[0]->getPosition(); 196 | if (posApple.equals(posSnake)) 197 | { 198 | _apple->removeFromParent(); 199 | createApple(); 200 | return true; 201 | } 202 | return false; 203 | } -------------------------------------------------------------------------------- /zuoeryuner/Classes/Game.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocos2d.h" 4 | 5 | using namespace std; 6 | USING_NS_CC; 7 | 8 | class Game : public Scene 9 | { 10 | public: 11 | CREATE_FUNC(Game); 12 | protected: 13 | 14 | private: 15 | enum SnakeDirection 16 | { 17 | UP_OR_DOWN = 1, 18 | UP = 1, 19 | DOWN = -1, 20 | 21 | LEFT_OR_RIGHT = 2, 22 | LEFT = 2, 23 | RIGHT = -2 24 | }; 25 | 26 | virtual void onEnter(); 27 | 28 | DrawNode* createDrawNode(const Point& pos); 29 | 30 | void initSnake(); 31 | 32 | void createSnakeNode(); 33 | 34 | void createApple(); 35 | 36 | void snakeMove(float); 37 | 38 | void snakeHeadMove(); 39 | 40 | void touchSnakeDirection(const Point& tochPoint); 41 | 42 | bool snakeIsAapple(); 43 | 44 | bool snakeIsDie(); 45 | 46 | void snakeDie(); 47 | 48 | 49 | 50 | static Size kSnakeNodeSize; 51 | 52 | vector _snake; 53 | 54 | DrawNode* _apple; 55 | 56 | SnakeDirection _snakeDirection; 57 | 58 | SnakeDirection _touchSnakeDirection; 59 | bool _istouchSnakeDirection; 60 | }; -------------------------------------------------------------------------------- /zuoeryuner/Classes/GameHome.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fusijie/Snake/7852d937682e54d9aa5e5c12455c61380c031f05/zuoeryuner/Classes/GameHome.cpp -------------------------------------------------------------------------------- /zuoeryuner/Classes/GameHome.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cocos2d.h" 4 | 5 | using namespace std; 6 | USING_NS_CC; 7 | 8 | class GameHome : public Scene 9 | { 10 | public: 11 | CREATE_FUNC(GameHome); 12 | 13 | protected: 14 | 15 | private: 16 | virtual void onEnter(); 17 | 18 | void close(Ref* sender); 19 | 20 | void play(Ref* sender); 21 | 22 | MenuItemLabel* createMenuTTF(const string& name,const Point& pos,const ccMenuCallback& callback); 23 | 24 | void createMenu(); 25 | 26 | void createTitle(); 27 | }; 28 | --------------------------------------------------------------------------------