这是楼楼以前学习游戏开发过程中自己做的一个Demo,无意间找到,与喜欢游戏的你一起分享下,代码不是很完整,但是作为博客看待还是绰绰有余的。
环境:cocos2dx 3.10 C++ xcode
游戏逻辑:
驾驶英雄飞机发射子弹消灭两种敌机并避免与它们发生碰撞;
逻辑实现:
1:背景滚动(调度器无限循环播放两张背景图片);
2:敌机移动(调度器无限添加敌机并并执行相应动作用容器管理);
3:敌机AI(不断获取英雄飞机位置并移动到英雄飞机位置附近);
4:英雄飞机子弹(调度器无限添加并执行相应动作容器管理);
5;英雄飞机子弹变幻(飞机碰撞到空降物就变幻);
6;碰撞检测(检测矩形是否相交);
7:分数显示(碰撞检测消灭一架就加多少分并不断刷新);
8:触摸实现(检测飞机的位置和实际触摸的位置是否相同);
9;游戏结束(碰撞检测并执行相应动画);
H
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
//是否开启触摸事件
bool onTouchBegan(cocos2d::Touch*touch,cocos2d::Event*event);
//移动事件
void onTouchMoved(cocos2d::Touch*touch,cocos2d::Event*event);
private:
//屏幕大小
cocos2d::Size visibleSize;
//背景图一
cocos2d::Sprite*background01;
//背景图二
cocos2d::Sprite*background02;
//英雄飞机
cocos2d::Sprite*hero;
//英雄飞机的僚机
cocos2d::Sprite*hero_liaoji;
//英雄飞机第一种子弹
cocos2d::Sprite*hero_bullet01;
//英雄飞机第二种子弹
cocos2d::Sprite*hero_bullet02;
//第一种敌人
cocos2d::Sprite*enemy01;
//第二种敌人
cocos2d::Sprite*enemy02;
//空降物
cocos2d::Sprite*kongjiangwu;
//分数标签
cocos2d::Label*_scoreLabel;
//分数值
int _scoreValue;
public:
//背景滚动
void background_remove(float dt);
//添加第一种敌机
void enemy01_add(float dt);
//添加第二种敌机
void enemy02_add(float dt);
//第一种英雄飞机子弹移动并创建
void hero_bullet01_remove(float dt);
//第二种英雄飞机子弹移动并创建
void hero_bullet02_remove(float dt);
//碰撞检测
void collisionDection(float dt);
//英雄僚机移动更新
void hero_lioaji_remove(float dt);
//空降物移动
void kongjingwu_remove(float dt);
};
CPP
#include "HelloWorldScene.h"
//#include "cocostudio/CocoStudio.h"
//#include "ui/CocosGUI.h"
USING_NS_CC;
//using namespace cocostudio::timeline;
Vector<Sprite*>_enemy01;
Vector<Sprite*>_enemy02;
Vector<Sprite*>_hero_bullet;
Vector<Sprite*>_kongjingwu;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//auto rootNode = CSLoader::createNode("MainScene.csb");
//addChild(rootNode);
background01=Sprite::create();
background02=Sprite::create();
background01->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
background02->setPosition(Vec2(visibleSize.width/2,visibleSize.height+background02->getContentSize().height/2));
addChild(background01);
addChild(background02);
hero=Sprite::create("hero01.png");
hero->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
addChild(hero);
Animation*an=Animation::create();
an->addSpriteFrameWithFile("hero02.png");
an->addSpriteFrameWithFile("hero03.png");
an->addSpriteFrameWithFile("hero01.png");
an->addSpriteFrameWithFile("hero04.png");
an->addSpriteFrameWithFile("hero05.png");
an->setDelayPerUnit(0.5f/3.0f);
an->setLoops(-1);
Animate*ani=Animate::create(an);
hero->runAction(ani);
hero_liaoji=Sprite::create("hero01.png");
hero_liaoji->setScale(0.5);
addChild(hero_liaoji);
_scoreLabel=Label::create("00","",20);
_scoreLabel->setPosition(Vec2(visibleSize.width/2,visibleSize.height*0.9));
addChild(_scoreLabel);
_scoreValue=0;
EventListenerTouchOneByOne*a=EventListenerTouchOneByOne::create();
a->onTouchBegan=CC_CALLBACK_2(HelloWorld::onTouchBegan,this);
a->onTouchMoved=CC_CALLBACK_2(HelloWorld::onTouchMoved,this);
a->setSwallowTouches(true);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(a,this);
this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::enemy01_add),1);
this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::enemy02_add),5);
this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::hero_bullet01_remove),0.2);
this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::collisionDection),0.1);
this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::kongjingwu_remove),5);
this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::hero_lioaji_remove));
return true;
}
void HelloWorld::background_remove(float dt){
float y1=background01->getPositionY()-2;
float y2=background02->getPositionY()-2;
background01->setPositionY(y1);
background02->setPositionY(y2);
if(-background01->getContentSize().height>y1){
background01->setPositionY(background02->getPositionY()+background02->getContentSize().height-2);
}else if(-background02->getContentSize().height>y2)
{
background02->setPositionY(background01->getPositionY()+background01->getContentSize().height-2);
}
}
void HelloWorld::enemy01_add(float dt){
int a=rand()%960;
enemy01=Sprite::create("enemy01.png");
enemy01->setPosition(Vec2(a,visibleSize.height+enemy01->getContentSize().height/2));
addChild(enemy01);
enemy01->setScale(0.4);
_enemy01.pushBack(enemy01);
auto moto=MoveTo::create(10,Vec2(enemy01->getPosition().x,-visibleSize.height-enemy01->getContentSize().height/2));
enemy01->runAction(moto);
}
void HelloWorld::enemy02_add(float dt){
int a=rand()%960;
int d=rand()%100;
enemy02=Sprite::create("ai01.png");
enemy02->setPosition(Vec2(a,visibleSize.height+enemy02->getContentSize().height/2));
addChild(enemy02);
_enemy02.pushBack(enemy02);
Animation*an=Animation::create();
an->addSpriteFrameWithFile("ai01.png");
an->addSpriteFrameWithFile("ai02.png");
an->addSpriteFrameWithFile("ai03.png");
an->addSpriteFrameWithFile("ai04.png");
an->addSpriteFrameWithFile("ai05.png");
an->setDelayPerUnit(0.5f/3.0f);
an->setLoops(-1);
Animate*ani=Animate::create(an);
enemy02->runAction(ani);
auto move=MoveTo::create(5,Vec2(hero->getPosition().x-d,hero->getPosition().y+d));
auto move1=MoveTo::create(2,Vec2(enemy02->getPosition().x,-visibleSize.height-enemy02->getContentSize().height/2));
auto sequence=Sequence::create(move,move1, NULL);
enemy02->runAction(sequence);
}
void HelloWorld::hero_bullet01_remove(float dt){
hero_bullet01=Sprite::create("bullet01.png");
hero_bullet01->setPosition(Vec2(hero->getPosition().x,hero->getPosition().y+50));
addChild(hero_bullet01);
_hero_bullet.pushBack(hero_bullet01);
auto movet=MoveTo::create(2,Vec2(hero->getPosition().x,visibleSize.height+hero_bullet01->getContentSize().height/2));
hero_bullet01->runAction(movet);
}
void HelloWorld::hero_bullet02_remove(float dt){
hero_bullet02=Sprite::create("enemy02.png");
hero_bullet02->setPosition(Vec2(hero->getPosition().x+50,hero->getPosition().y));
addChild(hero_bullet02);
hero_bullet02->setScale(2);
hero_bullet02->setVisible(false);
auto movet=MoveTo::create(2,Vec2(hero->getPosition().x,visibleSize.height+hero_bullet02->getContentSize().height/2));
hero_bullet02->runAction(movet);
}
void HelloWorld::collisionDection(float dt){
for(int i=0;i<_enemy01.size();++i){
Sprite*en=_enemy01.at(i);
if(en->getBoundingBox().intersectsRect(hero_bullet01->getBoundingBox())){
_scoreLabel->setString(StringUtils::format("%d",_scoreValue+=100));
_enemy01.eraseObject(en);
removeChild(en);
}else if (en->getBoundingBox().intersectsRect(hero->getBoundingBox())){
//removeChild(hero);
hero->setVisible(false);
MessageBox("GameOver","Sorry");
hero_bullet01->setVisible(false);
_hero_bullet.clear();
_enemy01.clear();
_enemy02.clear();
}
}
for(int u=0;u<_enemy02.size();++u){
Sprite*en2=_enemy02.at(u);
if(en2->getBoundingBox().intersectsRect(hero_bullet01->getBoundingBox())){
_scoreLabel->setString(StringUtils::format("%d",_scoreValue+=500)); _enemy02.eraseObject(en2);
removeChild(en2);
}else if (en2->getBoundingBox().intersectsRect(hero->getBoundingBox())){
//removeChild(hero);
hero->setVisible(false);
MessageBox("GameOver","Sorry");
_hero_bullet.clear();
hero_bullet01->setVisible(false);
_enemy01.clear();
_enemy02.clear();
}
}
}
void HelloWorld::kongjingwu_remove(float dt){
Size s=Director::getInstance()->getVisibleSize();
int a=rand()%960;
kongjiangwu=Sprite::create("bz.png");
kongjiangwu->setScale(3);
kongjiangwu->setPosition(Vec2(a,s.height+kongjiangwu->getContentSize().height/2));
this->addChild(kongjiangwu);
auto move=MoveTo::create(5,Vec2(kongjiangwu->getPosition().x,-s.height-kongjiangwu->getContentSize().height/2));
kongjiangwu->runAction(move);
_kongjingwu.pushBack(kongjiangwu);
}
void HelloWorld::hero_lioaji_remove(float dt){
if(hero->getPosition().x>200){
hero_liaoji->setPosition(Vec2(hero->getPosition().x-100,hero->getPosition().y));
}else if (hero->getPosition().x<=200){
hero_liaoji->setPosition(Vec2(hero->getPosition().x+100,hero->getPosition().y));
}
if(_scoreValue>1000){
_scoreLabel->setScale(2);
}
}
bool HelloWorld::onTouchBegan(cocos2d::Touch*touch,cocos2d::Event*event){
auto point=touch->getLocation();
auto rect=hero->getBoundingBox();
if(rect.containsPoint(point))
{
return true;
}
return false;
}
void HelloWorld::onTouchMoved(cocos2d::Touch*touch,cocos2d::Event*event){
auto add_point=touch->getLocation()-touch->getPreviousLocation();
hero->setPosition(hero->getPosition()+add_point);
auto Size=Director::getInstance()->getWinSize();
auto hero_size=hero->getContentSize();
if(hero->getPositionX()<hero_size.width/2){
hero->setPositionX(hero_size.width/2);
}else if(hero->getPositionX()>Size.width-hero_size.width/2){
hero->setPositionX(Size.width-hero_size.width/2);
}
if(hero->getPositionY()<hero_size.height/2){
hero->setPositionX(hero_size.width/2);
}else if(hero->getPositionY()>Size.height-hero_size.height/2){
hero->setPositionY(Size.height-hero_size.height/2);
}
}
点击查看更多内容
31人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦