cocos2dx-是男人就坚持20s 练手项目
前言
前段時(shí)間心血來(lái)潮看了下app游戲方面的東西
?
,對(duì)比了下各種技術(shù)和市場(chǎng)招聘情況,趕腳cocos2dx在2D游戲方向還算是大有所為,遂找了幾個(gè)基礎(chǔ)教程看看了解了解。并附上一個(gè)簡(jiǎn)單demo作為成果
準(zhǔn)備工作
環(huán)境搭建倒是重頭戲,相關(guān)教程也比較多,我直接轉(zhuǎn)個(gè)給大家參考吧(安裝教程戳這里)。
開(kāi)始游戲
找了個(gè)經(jīng)典游戲是男人就堅(jiān)持20秒,相信大家都接觸過(guò),游戲邏輯比較簡(jiǎn)單不外乎控制飛機(jī)躲避子彈,這里就山寨它吧
可以看到組成部分只有計(jì)時(shí)器,子彈和小鳥(niǎo)(為什么選小鳥(niǎo)呢,因?yàn)閳A形圖標(biāo)做碰撞檢測(cè)比較簡(jiǎn)單,本來(lái)用飛機(jī)的,但是飛機(jī)的空白地方不好處理,簡(jiǎn)單實(shí)例就用簡(jiǎn)單的方法吧)
1、計(jì)時(shí)器
int time=0; CCLabelTTF* timelb;//文本框 schedule(schedule_selector(manfor20s::timecount), 1.0f);//每秒執(zhí)行的計(jì)時(shí)器//每秒累加 void manfor20s::timecount(float dt) {time= time+1;CCString* ns=CCString::createWithFormat("%d", manfor20s::time);timelb->setString(ns->getCString() ); } 計(jì)時(shí)器邏輯2、子彈的生成和碰撞檢測(cè)
CCArray* listSpirit;//獲取頁(yè)面上所有元素的容器 CCSprite* plane;//小鳥(niǎo) schedule(schedule_selector(manfor20s::update));//每一幀執(zhí)行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) //子彈和小鳥(niǎo)圓心點(diǎn)相距小于15則認(rèn)為碰撞了 { CCMessageBox("被擊中了","alert");menuCloseCallback();//關(guān)閉break;} if(!win.intersectsRect(projectileRect))//如果子彈超出窗體則刪除 {this->removeChild(it); }}}#pragma region 產(chǎn)生彈道 隨機(jī)生成各個(gè)方向的子彈if(getRand(1,10)>8)//隨機(jī)因子 {//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; } 子彈的生成和碰撞檢測(cè)3、小鳥(niǎo)的移動(dòng)
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; } 小鳥(niǎo)的移動(dòng)大體邏輯就是這樣,第一次做c++項(xiàng)目,分不清::?? .? ->的概念,幸好項(xiàng)目比較小問(wèn)題不大,希望有機(jī)會(huì)能接觸高大上一點(diǎn)的項(xiàng)目做做,哈哈,不知道怎么傳代碼,就吧.h文件和.cpp文件都貼上來(lái)吧
#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;//觸屏響應(yīng)重寫(xiě)這三個(gè)方法virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//按下virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//拖動(dòng)virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event);//松開(kāi) };#endif 游戲頁(yè).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 產(chǎn)生彈道 if(getRand(1,10)>8)//隨機(jī)因子 {//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; } 游戲頁(yè).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__ 菜單頁(yè).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();//開(kāi)始和退出按鈕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);//標(biāo)題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); } 菜單頁(yè).cpp?下載代碼戳這里
?
轉(zhuǎn)載于:https://www.cnblogs.com/qyzBlog/p/3627592.html
總結(jié)
以上是生活随笔為你收集整理的cocos2dx-是男人就坚持20s 练手项目的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Echarts一些社区网站,亲测可用,新
- 下一篇: 对计算机课的期待200字,谈《计算机应用