前言

前段时间心血来潮看了下app游戏方面的东西

,对比了下各种技术和市场招聘情况,赶脚cocos2dx在2D游戏方向还算是大有所为,遂找了几个基础教程看看了解了解。并附上一个简单demo作为成果

准备工作

环境搭建倒是重头戏,相关教程也比较多,我直接转个给大家参考吧(安装教程戳这里)。

开始游戏

找了个经典游戏是男人就坚持20秒,相信大家都接触过,游戏逻辑比较简单不外乎控制飞机躲避子弹,这里就山寨它吧

可以看到组成部分只有计时器,子弹和小鸟(为什么选小鸟呢,因为圆形图标做碰撞检测比较简单,本来用飞机的,但是飞机的空白地方不好处理,简单实例就用简单的方法吧)

1、计时器

int time=0;
CCLabelTTF* timelb;//文本框
schedule(schedule_selector(manfor20s::timecount), 1.0f);//每秒执行的计时器//每秒累加
void  manfor20s::timecount(float dt)
{time= time+1;CCString* ns=CCString::createWithFormat("%d", manfor20s::time);timelb->setString(ns->getCString()  );
}

计时器逻辑

2、子弹的生成和碰撞检测

CCArray* listSpirit;//获取页面上所有元素的容器
CCSprite* plane;//小鸟
schedule(schedule_selector(manfor20s::update));//每一帧执行void  manfor20s::update(float dt)
{  CCSprite *pl =  plane ;  CCRect targetRect = CCRectMake(  pl->getPosition().x - (pl->getContentSize().width/2),  pl->getPosition().y - (pl->getContentSize().height/2),  pl->getContentSize().width,  pl->getContentSize().height); CCRect win=CCRectMake(0,0,visibleSize.width,visibleSize.height);listSpirit=this->getChildren();//获取所有元素for (int i=listSpirit->count()-1;i>=0;i--){CCSprite* it=(CCSprite*)listSpirit->objectAtIndex(i);if(it->getTag()==2)//tag为2则为子弹
        {/*CCSprite *sp = dynamic_cast<CCSprite*>(it);  */CCRect projectileRect = CCRectMake( it->getPosition().x - (it->getContentSize().width/2),  it->getPosition().y - (it->getContentSize().height/2),  it->getContentSize().width,  it->getContentSize().height);if ( ccpDistance(it->getPosition(),plane->getPosition())<15)  //子弹和小鸟圆心点相距小于15则认为碰撞了
            {  CCMessageBox("被击中了","alert");menuCloseCallback();//关闭break;}  if(!win.intersectsRect(projectileRect))//如果子弹超出窗体则删除             {this->removeChild(it);  }}}#pragma region 产生弹道 随机生成各个方向的子弹if(getRand(1,10)>8)//随机因子
    {//get directerint di =getRand(0,3);CCSprite * pu =CCSprite::create("p.png");  pu->setTag(2);CCPoint from;CCPoint to;switch(di){case 0://up to down
                {from=ccp(getRand(0,visibleSize.width),visibleSize.height);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),-10);  } break;case 1://down to up
                {from=ccp(getRand(0,visibleSize.width),0);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),visibleSize.height+10);}break;case 2://left to right
                {from=ccp(0,getRand(0,visibleSize.height)); to=ccp(visibleSize.width+10,getRand(-visibleSize.height,visibleSize.height*2));} break;case 3://right to left
                {from=ccp(visibleSize.width,getRand(0,visibleSize.height));  to=ccp(-10,getRand(-visibleSize.height,visibleSize.height*2));} break;default:break;}pu->setPosition(from);this->addChild(pu);int distance=cocos2d::ccpDistance(from,to);CCActionInterval *forward = CCMoveTo::create(distance/50,to);  //moveto 速度控制pu->runAction(forward);   }#pragma endregion
}//random
int manfor20s::getRand(int start,int end)
{  float i = CCRANDOM_0_1()*(end-start+1)+start;  //get random from start to endreturn (int)i;
}  

子弹的生成和碰撞检测

3、小鸟的移动

bool manfor20s::ccTouchBegan(CCTouch * touch,CCEvent* event)
{CCPoint heropos = plane->getPosition();CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);if (location.x > heropos.x - 24 && location.x < heropos.x + 24 && location.y > heropos.y - 24 && location.y < heropos.y + 24){isControl = true;deltax = location.x - heropos.x;deltay = location.y - heropos.y;}return true;
}void manfor20s::ccTouchMoved(CCTouch * touch,CCEvent* event)
{if (isControl){CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);float x = location.x - deltax;float y = location.y - deltay;plane->setPosition(ccp(x,y));}
}void manfor20s::ccTouchEnded(CCTouch * touch, CCEvent * event)
{isControl = false;
}

小鸟的移动

大体逻辑就是这样,第一次做c++项目,分不清::   .  ->的概念,幸好项目比较小问题不大,希望有机会能接触高大上一点的项目做做,哈哈,不知道怎么传代码,就吧.h文件和.cpp文件都贴上来吧

#ifndef __manfor20s_SCENE_H__
#define __manfor20s_SCENE_H__#include "cocos2d.h"class manfor20s:public cocos2d::CCLayer
{public: virtual bool init();  // there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::CCScene* scene();// a selector callbackvoid menuCloseCallback();// implement the "static node()" method manually
    CREATE_FUNC(manfor20s);void  timecount(float dt);void update(float dt);int getRand(int start,int end) ; int time;bool isControl;int deltax;int deltay;//触屏响应重写这三个方法virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//按下virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//拖动virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//松开
};#endif

游戏页.h

#include "manfor20s.h"
#include "MainPage.h"
USING_NS_CC;
CCLabelTTF* timelb;
CCSize visibleSize;
CCArray* listSpirit;
CCSprite* plane;
CCScene* manfor20s::scene(){CCScene *scene = CCScene::create(); manfor20s *layer = manfor20s::create(); scene->addChild(layer); return scene;
}bool manfor20s::init()
{if ( !CCLayer::init() ){return false;}this->setTouchEnabled(true);CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); timelb=CCLabelTTF::create("0",  "Arial", 20);timelb->setPosition(ccp(origin.x+10,origin.y +visibleSize.height-20));this->addChild(timelb);   manfor20s::time=0;plane=CCSprite::create("bird.png"); plane->setTag(1);plane->setPosition(ccp(origin.x+visibleSize.width/2,origin.y + visibleSize.height/2));this->addChild(plane);schedule(schedule_selector(manfor20s::update));schedule(schedule_selector(manfor20s::timecount), 1.0f);return true;
}void  manfor20s::update(float dt)
{  CCSprite *pl =  plane ;  CCRect targetRect = CCRectMake(  pl->getPosition().x - (pl->getContentSize().width/2),  pl->getPosition().y - (pl->getContentSize().height/2),  pl->getContentSize().width,  pl->getContentSize().height); CCRect win=CCRectMake(0,0,visibleSize.width,visibleSize.height);listSpirit=this->getChildren();for (int i=listSpirit->count()-1;i>=0;i--){CCSprite* it=(CCSprite*)listSpirit->objectAtIndex(i);if(it->getTag()==2){/*CCSprite *sp = dynamic_cast<CCSprite*>(it);  */CCRect projectileRect = CCRectMake( it->getPosition().x - (it->getContentSize().width/2),  it->getPosition().y - (it->getContentSize().height/2),  it->getContentSize().width,  it->getContentSize().height);if ( ccpDistance(it->getPosition(),plane->getPosition())<15)  {  CCMessageBox("被击中了","alert");menuCloseCallback();break;}  if(!win.intersectsRect(projectileRect))//delete if over the windows
             {this->removeChild(it);  }}}#pragma region 产生弹道 if(getRand(1,10)>8)//随机因子
    {//get directerint di =getRand(0,3);CCSprite * pu =CCSprite::create("p.png");  pu->setTag(2);CCPoint from;CCPoint to;switch(di){case 0://up to down
                {from=ccp(getRand(0,visibleSize.width),visibleSize.height);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),-10);  } break;case 1://down to up
                {from=ccp(getRand(0,visibleSize.width),0);to=ccp(getRand(-visibleSize.width,visibleSize.width*2),visibleSize.height+10);}break;case 2://left to right
                {from=ccp(0,getRand(0,visibleSize.height)); to=ccp(visibleSize.width+10,getRand(-visibleSize.height,visibleSize.height*2));} break;case 3://right to left
                {from=ccp(visibleSize.width,getRand(0,visibleSize.height));  to=ccp(-10,getRand(-visibleSize.height,visibleSize.height*2));} break;default:break;}pu->setPosition(from);this->addChild(pu);int distance=cocos2d::ccpDistance(from,to);CCActionInterval *forward = CCMoveTo::create(distance/50,to);  //moveto 速度控制pu->runAction(forward);   }#pragma endregion
}void  manfor20s::timecount(float dt)
{manfor20s::time= manfor20s::time+1;CCString* ns=CCString::createWithFormat("%d", manfor20s::time);timelb->setString(ns->getCString()  );
}int manfor20s::getRand(int start,int end)
{  float i = CCRANDOM_0_1()*(end-start+1)+start;  //get random from start to endreturn (int)i;
}  //close button
void manfor20s::menuCloseCallback()
{this->removeAllChildren();this->unscheduleAllSelectors();  CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // turn on display FPSpDirector->setDisplayStats(true);// set FPS. the default value is 1.0/60 if you don't call thispDirector->setAnimationInterval(1.0 / 60);// create a scene. it's an autorelease objectCCScene *pScene = MainPage::scene(); pDirector->replaceScene(pScene);
}bool manfor20s::ccTouchBegan(CCTouch * touch,CCEvent* event)
{CCPoint heropos = plane->getPosition();CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);if (location.x > heropos.x - 24 && location.x < heropos.x + 24 && location.y > heropos.y - 24 && location.y < heropos.y + 24){isControl = true;deltax = location.x - heropos.x;deltay = location.y - heropos.y;}return true;
}void manfor20s::ccTouchMoved(CCTouch * touch,CCEvent* event)
{if (isControl){CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);float x = location.x - deltax;float y = location.y - deltay;plane->setPosition(ccp(x,y));}
}void manfor20s::ccTouchEnded(CCTouch * touch, CCEvent * event)
{isControl = false;
}

游戏页.cpp

#ifndef __MainPage_SCENE_H__
#define __MainPage_SCENE_H__#include "cocos2d.h"class MainPage : public cocos2d::CCLayer
{
public:// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtual bool init();  // there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::CCScene* scene();// a selector callbackvoid menuCloseCallback(CCObject* pSender);// a selector callbackvoid menustartGame(CCObject* pSender);// implement the "static node()" method manually
    CREATE_FUNC(MainPage);
};#endif // __HELLOWORLD_SCENE_H__

菜单页.h

#include "MainPage.h"
#include "manfor20s.h"
USING_NS_CC;CCScene* MainPage::scene()
{// 'scene' is an autorelease objectCCScene *scene = CCScene::create();// 'layer' is an autorelease objectMainPage *layer = MainPage::create();// add layer as a child to scenescene->addChild(layer);// return the scenereturn scene;
}// on "init" you need to initialize your instance
bool MainPage::init()
{//
    // 1. super init firstif ( !CCLayer::init() ){return false;}//获取原始尺寸CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();//开始和退出按钮CCLabelTTF *label1 = CCLabelTTF::create("Start",  "Arial", 20); // create a exit botton  CCMenuItemLabel *start_game = CCMenuItemLabel::create(label1, this, menu_selector(MainPage::menustartGame) );  CCLabelTTF *label2 = CCLabelTTF::create("Exit",  "Arial", 20); // create a exit botton  CCMenuItemLabel *exit_game = CCMenuItemLabel::create(label2, this, menu_selector(MainPage::menuCloseCallback) );  start_game->setPosition(ccp((origin.x + visibleSize.width - start_game->getContentSize().width)/2 ,origin.y+visibleSize.height/2 + start_game->getContentSize().height/2));exit_game->setPosition(ccp((origin.x + visibleSize.width - exit_game->getContentSize().width)/2 ,origin.y+visibleSize.height/2 + exit_game->getContentSize().height/2-50));// create menu, it's an autorelease objectCCMenu* pMenu = CCMenu::create(start_game,exit_game, NULL);pMenu->setPosition(CCPointZero);this->addChild(pMenu, 1);//标题CCLabelTTF* pLabel = CCLabelTTF::create("can you hold 20 sec?", "Arial", 28);// position the label on the center of the screenpLabel->setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - pLabel->getContentSize().height));// add the label as a child to this layerthis->addChild(pLabel, 1);//背景图片CCSprite* pSprite = CCSprite::create("background.jpg");pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));this->addChild(pSprite, 0);return true;
}void MainPage::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#elseCCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)exit(0);
#endif
#endif
}void MainPage::menustartGame(CCObject* psender)
{CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();pDirector->setOpenGLView(pEGLView);// turn on display FPSpDirector->setDisplayStats(true);// set FPS. the default value is 1.0/60 if you don't call thispDirector->setAnimationInterval(1.0 / 60);// create a scene. it's an autorelease objectCCScene *pScene = manfor20s::scene(); pDirector->replaceScene(pScene);
}

菜单页.cpp

下载代码戳这里

转载于:https://www.cnblogs.com/qyzBlog/p/3627592.html

cocos2dx-是男人就坚持20s 练手项目相关推荐

  1. 70个Python练手项目列表 预祝大家 快乐

    小孩眺望远方,成人怀念故乡. 为此给大家分享一下珍藏的Python实战项目,祝大家节日快乐哦!!! Python 前言:不管学习哪门语言都希望能做出实际的东西来,这个实际的东西当然就是项目啦,不用多说 ...

  2. 一个适合于Python 初学者的入门练手项目

    随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...

  3. 推荐 Python 十大经典练手项目,让你的 Python 技能点全亮!

    前言:如果有人问:"Python还火吗?""当然,很火.""哪能火多久呢?""不知道." 技术发展到现在衍生出许多种编程 ...

  4. 别让双手闲下来,来做一些练手项目吧

    作者:Weston,原文链接,原文日期:2016-01-27 译者:saitjr:校对:Cee:定稿:千叶知风 自从我昨天发了文,收到的最多的评论就是: 我应该选择哪些 App 来练手呢? 这个问题很 ...

  5. python项目-推荐 10 个有趣的 Python 练手项目

    想成为一个优秀的Python程序员,没有捷径可走,势必要花费大量时间在键盘后. 而不断地进行各种小项目开发,可以为之后的大开发项目积攒经验,做好准备. 但不少人都在为开发什么项目而苦恼. 因此,我为大 ...

  6. python新手项目-Python 的练手项目有哪些值得推荐?

    其实初学者大多和题主类似都会经历这样一个阶段,当一门语言基础语法学完,之后刷了不少题,接下来就开始了一段迷茫期,不知道能用已经学到的东西做些什么即便有项目也无从下手,而且不清楚该如何去提高技术水平. ...

  7. python可以做什么项目-适合Python 新手的5大练手项目,你练了么?

    已经学习了一段时间的Python,如果你看过之前W3Cschool的文章,就知道是时候该进去[项目]阶段了. 但是在练手项目的选择上,还存在疑问?不知道要从哪种项目先下手? W3Cschool首先有两 ...

  8. python新手项目-推荐:一个适合于Python新手的入门练手项目

    原标题:推荐:一个适合于Python新手的入门练手项目 随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人 ...

  9. python新手小项目-推荐:一个适合于Python新手的入门练手项目

    随着人工智能的兴起,国内掀起了一股Python学习热潮,入门级编程语言,大多选择Python,有经验的程序员,也开始学习Python,正所谓是人生苦短,我用Python 有个Python入门练手项目, ...

最新文章

  1. GridSearchCV 与 RandomizedSearchCV 调参
  2. 数据安全最佳实践案例库建设项目案例征集
  3. 2019上海车展展后报告(整车篇)
  4. 华为云免费体验 怎么使用_华为云Classroom免费向全国高校开放,云端学习更高效...
  5. Scala模式匹配:对象匹配
  6. 【小马哥】Spring Cloud系列讲座
  7. PYPL 6 月编程语言排行
  8. java webservice wsimport 无法将名称 'soapenc:Array' 解析为 'type definition' 组件 时对应的解决方法...
  9. [Mugeda HTML5技术教程之3] Hello World: 第一个Mugeda动画
  10. Python文件之----CSV
  11. 第二季-专题19-移植tftp客户端
  12. try与raise用法
  13. apache + phpStudy 配置vue history模式
  14. Java反射学习总结终(使用反射和注解模拟JUnit单元测试框架)
  15. 汽车电子中的3225贴片晶振
  16. 简单工厂模式学习总结
  17. Hilbert 变换
  18. 转page类事件执行顺序
  19. TinyOS数据帧与CC2420 Radio Stack解读
  20. 2018-2019-2 20189206 《密码与安全新技术专题》 第六次作业

热门文章

  1. 正则表达式介绍+一些简单应用
  2. 扫雷游戏(模拟算法)
  3. java中有测试方法主方法不运行_java – 我的Eclipse无法再运行(或调试)我的JUnit测试...
  4. 中秋福利!三维重建/SLAM/点云/相机标定/深度估计/缺陷检测课程
  5. android wifi增强,Android增强WiFi性能
  6. 付费会员亿时代即将来临,如何才能打造“终身俱乐部”?
  7. Android 最简单的自定义证件照Mask之一
  8. 【Vue】Emitted value instead of an instance of Error
  9. jQuery仿天猫完美加入购物车
  10. 还在担心零基础绘画?这篇文章让你少走弯路!