竖屏小游戏--喵星战争源代码分析【完整】

 转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/26942311

源代码地址:http://download.csdn.net/detail/oyangyufu/7409593

Ccp文件介绍:

GameMenuScene.cpp游戏主菜单

GameMainScene.cpp游戏主页面

GameObjHero.cpp主角

GameHeroBullet.cpp主角的子弹

GameObjEnemy.cpp敌人

GameEnemyBullet.cpp敌人的子弹

GameAboutScene.cpp关于页面

GameMark.cpp游戏分数

执行游戏进入游戏主菜单页面

代码:

GameMenuScene类init()函数,初始化主菜单页面背景图

	CCSprite* back = CCSprite::create("bg.png");
	back->setScale(0.5);
	back->setPosition(ccp(size.width/2,size.height/2));
	this->addChild(back,0,0);

初始化主菜单页面背景星球图片

	CCSprite* bgstar = CCSprite::create("moon.png");
	bgstar->setAnchorPoint(ccp(0.5,0));
	bgstar->setScale(0.5);
	bgstar->setPosition(ccp(size.width/3 * 2, 0));
	this->addChild(bgstar,1,1);

初始化主菜单页面标题名称meowstar、battle

	CCNode* title = CCNode::create();
	title->setContentSize(CCSizeMake(0,0));
	CCSprite* ptmLabel = CCSprite::create("meowstar.png");
	ptmLabel->setScale(0.5);
	ptmLabel->setPosition(ccp(0,30));
	title->addChild(ptmLabel);
	CCSprite *ptbLabel = CCSprite::create("battle.png");
	ptbLabel->setScale(0.5);
	ptbLabel->setPosition( ccp(0,-30) );
	title->addChild(ptbLabel);
	title->setPosition(ccp(size.width / 2, size.height - 80));
	this->addChild(title,2,2);

初始化主菜单页面中的菜单项及单击事件,并设置为不可见

	CCMenuItemImage *newGameItem = CCMenuItemImage::create("newgameA.png", "newgameB.png",this,menu_selector(GameMenuScene::menuNewGameCallback));//newgame菜单项
	newGameItem->setScale(0.5);
	newGameItem->setPosition(ccp(size.width / 2,size.height / 2 - 20));
	newGameItem->setEnabled(false);
	CCMenuItemImage *continueItem = CCMenuItemImage::create("continueA.png", "continueB.png",this,menu_selector(GameMenuScene::menuContinueCallback));//continue菜单项
	continueItem->setScale(0.5);
	continueItem->setPosition(ccp(size.width / 2,size.height / 2 - 80));
	continueItem->setEnabled(false);
	CCMenuItemImage *aboutItem = CCMenuItemImage::create("aboutA.png", "aboutB.png",this,menu_selector(GameMenuScene::menuAboutCallback));//aboutA菜单项
	aboutItem->setScale(0.5);
	aboutItem->setPosition(ccp(size.width / 2,size.height / 2 - 140));
	aboutItem->setEnabled(false);
	m_soundItem = CCMenuItemImage::create("sound-on-A.png", "sound-on-B.png",this,menu_selector(GameMenuScene::menuSoundCallback));//设置声音菜单项
	m_soundItem->setScale(0.5);
	m_soundItem->setEnabled(false);
	m_soundItem->setPosition(ccp(40,40));

	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		this,
		menu_selector(GameMenuScene::menuCloseCallback));
	pCloseItem->setPosition(ccp(size.width - 20, 20));
	//使用button创建菜单
	CCMenu* mainmenu = CCMenu::create(newGameItem,continueItem,aboutItem,m_soundItem,pCloseItem,NULL);
	mainmenu->setPosition(ccp(0,0));
	this->addChild(mainmenu,1,3);

初始化背景音乐

	m_isSound = true;//是否开启声音标识
	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic( CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str() );
	CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str(), true);

进入主菜单页面有动画效果

void GameMenuScene::onEnter()
{
	CCLayer::onEnter();
	CCSize size = CCDirector::sharedDirector()->getWinSize();

	CCNode* mainmenu = this->getChildByTag(3);//主菜单
	mainmenu->setPositionX(-200);
	mainmenu->runAction(CCSequence::create(CCEaseElasticOut::create(CCMoveTo::create(1,ccp(0,0))),CCCallFuncN::create(this, callfuncN_selector(GameMenuScene::menuEnter)),NULL));//终点有弹力

	CCNode* title = this->getChildByTag(2);//标题
	title->setPositionY(size.height + 20);
	title->runAction(CCEaseElasticOut::create(CCMoveBy::create(1,ccp(0,-100))));

	CCNode* bgstar = this->getChildByTag(1);//星球
	bgstar->setPositionX(size.width/3);
	bgstar->runAction(CCMoveBy::create(0.5,ccp(size.width/3,0)));
}
void GameMenuScene::menuEnter( CCNode* pSender )
{
	//菜单项可见
	CCNode* mainmenu = this->getChildByTag(3);
	CCArray* temp = mainmenu->getChildren();
	for(unsigned int i = 0;i < temp->count();i ++)
	{
		((CCMenuItemImage *)temp->objectAtIndex(i))->setEnabled(true);
	}
}

点击NEW GAME、Contunue菜单按键切换GameMainScene场景进入游戏主页面

void GameMenuScene::menuNewGameCallback( CCObject* pSender )
{
	CCDirector::sharedDirector()->replaceScene(GameMainScene::scene());
}

void GameMenuScene::menuContinueCallback( CCObject* pSender )
{
	CCDirector::sharedDirector()->replaceScene(GameMainScene::scene());
}

点击About 菜单按键切换GameAboutScene场景进入关于页面

void GameMenuScene::menuAboutCallback( CCObject* pSender )
{
	CCDirector::sharedDirector()->replaceScene(GameAboutScene::scene());
}

点击声音菜单按键,变换按键图标,设置声音开关

void GameMenuScene::menuSoundCallback( CCObject* pSender )
{
	if(!m_isSound)//声音开
	{
		m_soundItem->setNormalImage(CCSprite::create("sound-on-A.png"));
		m_soundItem->setDisabledImage(CCSprite::create("sound-on-B.png"));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str(), true);
		m_isSound = true;
	}
	else//声音关
	{
		m_soundItem->setNormalImage(CCSprite::create("sound-off-A.png"));
		m_soundItem->setDisabledImage(CCSprite::create("sound-off-B.png"));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
		m_isSound = false;
	}
}

关于页面

代码:

初始化关于页面

背景

	CCSprite* bg = CCSprite::create("bg.png");
	bg->setScale(0.5);
	bg->setPosition( ccp(size.width/2, size.height/2) );
	this->addChild(bg, 0,0);

星球

	CCSprite*bgstar = CCSprite::create("moon.png");
	bgstar->setAnchorPoint(ccp(0.5,0));
	bgstar->setScale(0.5);
	bgstar->setPosition(ccp(size.width/3 * 1, 0));
	this->addChild(bgstar,1,1);

关于页面的信息

	CCSprite*kuang = CCSprite::create("tb.png");
	kuang->setScale(0.5);
	kuang->setPosition(ccp(size.width/2, size.height/2));
	this->addChild(kuang,2,2);
	char inf[256];
	sprintf(inf,"游戏名称:喵星战争\n\n作者:\n\n公司:\n\n使用工具:cocos2D-x");
	CCLabelTTF * myjineng = CCLabelTTF::create(inf,"Marker Felt",40,CCSizeMake(400,400),kCCTextAlignmentLeft);
	myjineng->setAnchorPoint(ccp(0,1));
	myjineng->setColor(ccc3(255,200,0));
	myjineng->setPosition(ccp(50,600));
	kuang->addChild(myjineng);//将标签增加关于框

返回菜单按键,并注冊点击事件

	CCMenuItemImage *back = CCMenuItemImage::create("backA.png", "backB.png",this,menu_selector(GameAboutScene::menuBackCallback));
	back->setScale(0.5);
	back->setPosition(ccp(size.width - 20,size.height - 20));
	back->setEnabled(false);
	CCMenu* mainmenu = CCMenu::create(back,NULL);
	mainmenu->setPosition(ccp(0,0));
	this->addChild(mainmenu,3,4);

返回键点击返回到主菜单页面

void GameAboutScene::menuBackCallback( CCObject* pSender )
{
	 CCDirector::sharedDirector()->replaceScene(GameMenuScene::scene());
}

进入关于页面时的动画

void GameAboutScene::onEnter()
{
	 CCLayer::onEnter();
	  //菜单进入后,菜单项点击有效
	 CCSize size = CCDirector::sharedDirector()->getWinSize();
	 CCNode* mainmenu = this->getChildByTag(4);//返回键
	 mainmenu->setPositionX(-100);
	 mainmenu->runAction(CCSequence::create(CCEaseElasticOut::create(CCMoveBy::create(0.5,ccp(100,0))),CCCallFuncN::create(this, callfuncN_selector(GameAboutScene::menuEnter)),NULL));//终点弹力效果
	 //
	 CCNode* title = this->getChildByTag(3);//关于页面标题
	 title->setPositionY(size.height+20);
	 title->runAction(CCEaseElasticOut::create(CCMoveBy::create(0.5,ccp(0,-40))));

	 CCNode*bgstar = this->getChildByTag(1);
	 bgstar->setPositionX(size.width/3 * 2);
	 bgstar->runAction(CCMoveBy::create(0.5,ccp(-size.width/3,0)));

	 CCNode*kuang = this->getChildByTag(2);//关于框
	 kuang->setPositionX(-200);
	 kuang->runAction(CCEaseElasticOut::create(CCMoveTo::create(0.5,ccp(size.width/2,size.height/2))));
}

进入主页面

代码:

初始化游戏主页面

背景,创建2个图拼接一起

	m_bg1 = CCSprite::create("bg.png");
	m_bg1->setScale(0.5);
	m_bg2 = CCSprite::create("bg.png");
	m_bg2->setScale(0.5);
	m_bg1->setAnchorPoint(ccp(0,0));//设置锚点
	m_bg2->setAnchorPoint(ccp(0,0));
	m_bg1->setPosition( ccp(0,0) );
	m_bg2->setPosition( ccp(0,size.height) );
	this->addChild(m_bg1, 0);
	this->addChild(m_bg2, 0);

创建主角及出场动画

	m_hero = new GameObjHero();
	m_hero->setPosition(ccp(size.width/2,-50));
	m_hero->setScale(0.5);
	this->addChild(m_hero,2,1);
	m_hero->runAction(CCMoveBy::create(0.5,ccp(0,150)));

创建3个敌人,每一个敌人有移动动作

	m_enemys = CCArray::createWithCapacity(3);
	for(int i = 0;i < 3;i ++)
	{
		int type = i + 1;
		GameObjEnemy* enemy = new GameObjEnemy();
		enemy->setPosition(ccp(size.width/4 * type,size.height + 50));
		enemy->setScale(0.5);
		enemy->setType(type);
		m_enemys->addObject(enemy);
		this->addChild(enemy,1);
		enemy->moveStart();
	}

初始主角生命数及生命图标

	m_blood = 3;
	CCSpriteBatchNode* ui = CCSpriteBatchNode::create("cat.png");
	//CCNode *ui = CCNode::create();
	m_blood1 = CCSprite::createWithTexture(ui->getTexture());
	m_blood1->setPosition(ccp(20,size.height - 20));
	m_blood1->setScale(0.2f);
	ui->addChild(m_blood1);
	m_blood2 = CCSprite::createWithTexture(ui->getTexture());
	m_blood2->setPosition(ccp(50,size.height - 20));
	m_blood2->setScale(0.2f);
	ui->addChild(m_blood2);
	m_blood3 = CCSprite::createWithTexture(ui->getTexture());
	m_blood3->setPosition(ccp(80,size.height - 20));
	m_blood3->setScale(0.2f);
	ui->addChild(m_blood3);
	addChild(ui,4);

初始化主角子弹

 	m_heroBullets = CCArray::createWithCapacity(5);
 	for(size_t i = 0;i < m_heroBullets->capacity();i ++)
 	{
 		GameHeroBullet * mybullet = new GameHeroBullet();
 		mybullet->setIsNotVisable(this);
 		mybullet->setScale(0.5);
 		m_heroBullets->addObject(mybullet);
 		this->addChild(mybullet,3);
 	}
 	m_heroBullets->retain();//计数器+1

初始化敌人子弹

 	m_enemyBullets = CCArray::createWithCapacity(10);
 	for(size_t i = 0;i < m_enemyBullets->capacity();i ++)
 	{
 		GameEnemyBullet * mybullet = new GameEnemyBullet();
 		mybullet->setIsNotVisable(this);//若是没加object參数,会发生执行时调用约定错误,原因未知
 		mybullet->setScale(0.5);
 		m_enemyBullets->addObject(mybullet);
 		this->addChild(mybullet,3);
 	}
 	m_enemyBullets->retain();

游戏分数

	m_gameMark = new GameMark();
	this->addChild(m_gameMark,4);

初始化游戏结束语gameover

	m_gameOver = CCSprite::create("gameover.png");
	m_gameOver->setAnchorPoint(ccp(0.5,0.5));
	m_gameOver->setPosition(ccp(0,0));
	m_gameOver->setPosition(ccp(size.width/2,size.height/2 + 70));
	m_gameOver->setVisible(false);
	m_gameOver->setScale(0.5);
	this->addChild(m_gameOver,5);

游戏结束上一步菜单按键

	CCMenuItemImage *pCloseItem = CCMenuItemImage::create("back.png","back.png",
		this,menu_selector(GameMainScene::menuBackCallback) );
	pCloseItem->setPosition( ccp(size.width/2,size.height/2 - 50) );
	pCloseItem->setScale(0.5);
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition( CCPointZero );
	this->addChild(pMenu,5,25);
	pMenu->setVisible(false);
	pMenu->setEnabled(false);
	m_isReduce = false;
	m_isGameOver = false;

	scheduleUpdate();

主角及主角子弹

代码:

主角接收触摸

	CCDirector* pDirector = CCDirector::sharedDirector();
	pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);

主角身体动画

	CCAnimation* animation = CCAnimation::create();
	animation->addSpriteFrameWithFileName("catBody1.png");
	animation->addSpriteFrameWithFileName("catBody2-4.png");
	animation->addSpriteFrameWithFileName("catBody3.png");
	animation->addSpriteFrameWithFileName("catBody2-4.png");
	animation->setDelayPerUnit(0.1f);
	animation->setRestoreOriginalFrame(true);
	mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
	this->addChild(mainsprite);

主角尾巴摇摆动画

	CCSprite *tail = CCSprite::create("catTail.png");
 	tail->setAnchorPoint(ccp(0.5,1));
 	tail->setPosition(ccp(-5,-29));
	tail->setScale(0.5);
	tail->setRotation(20);
	tail->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create(CCRotateBy::create(0.5,-40),CCRotateBy::create(0.5,40),NULL)));
	this->addChild(tail);

主角手动画

	m_leftHand = CCSprite::create("catHand1.png");
	m_leftHand->setAnchorPoint(ccp(1,0.5));
	m_leftHand->setPosition(ccp(-18,-20));
	this->addChild(m_leftHand);
	m_rightHand = CCSprite::create("catHand2.png");
	m_rightHand->setPosition(ccp(18,-20));
	m_rightHand->setAnchorPoint(ccp(0,0.5));
	this->addChild(m_rightHand);
	m_offset = ccp(0,0);
	m_isControl = false;

主角定时发射子弹

schedule(schedule_selector(GameObjHero::releasebullet), 0.8f);
void GameObjHero::releasebullet( float dt )
{
	if(m_isControl)//是否触摸了主角,触摸了则发射子弹
	{
		CCPoint pos = this->getPosition(); //获取主角坐标
		GameMainScene *p = (GameMainScene*) this->getParent();//获取父场景调用releaseHeroBullet发射子弹
		p->releaseHeroBullet(pos.x,pos.y + 30);
	}
}
void GameMainScene::releaseHeroBullet( int x,int y )//參数主角坐标
{
	for(size_t i = 0;i < m_heroBullets->capacity();i++)//遍历主角每一个子弹
	{
		if(!((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->getIsvisble())
		{
			//设置位置,并设置为显示
			((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->setPosition(ccp(x,y));//子弹起始位置
			((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->setIsVisable();//子弹移动动画
			break;
		}
	}
}
void GameHeroBullet::setIsVisable()
{
	this->setVisible(true);
	m_isVisable = true;
	int a = (-this->getPosition().y + 550)/150;
	this->runAction(CCSequence::create(CCMoveTo::create((-this->getPosition().y + 550)/150,ccp(this->getPosition().x,550)),CCCallFuncN::create(this, callfuncN_selector(GameHeroBullet::setIsNotVisable)),NULL));;
}
void GameHeroBullet::setIsNotVisable(CCNode* pSender)
{
	this->setVisible(false);
	m_isVisable = false;
	this->stopAllActions();
}

ccTouchBegan中推断是否点中主角

	if(! containsTouchLocation(touch))//没选中主角
	{
		return false;
	}
	else//选中了
	{
		m_isControl = true;//发射子弹
		//获取触摸偏移
		CCPoint touchPoint = touch->getLocationInView();
		touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
		m_offset.x = touchPoint.x - this->getPosition().x;
		m_offset.y = touchPoint.y - this->getPosition().y;
	}
	//选中的点是否在精灵矩形范围内
	bool GameObjHero::containsTouchLocation(CCTouch* touch)
	{
		return rect().containsPoint(convertTouchToNodeSpaceAR(touch));//
	}

	CCRect GameObjHero::rect()
	{
		CCSize s = CCSizeMake(85,90);
		CCPoint p = ccp(-s.width / 2, -s.height / 2);
		return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
	}

敌人及敌人子弹

代码:

创建敌人

this->setContentSize(CCSizeMake(99,115));
	m_mainBody = CCSprite::create("DrDog1.png");
	//敌人自身动画
	CCAnimation* animation = CCAnimation::create();
	animation->addSpriteFrameWithFileName("DrDog1.png");
	animation->addSpriteFrameWithFileName("DrDog2.png");
	animation->setDelayPerUnit(0.1f);
	animation->setRestoreOriginalFrame(true);
	m_mainBody->runAction(CCRepeatForever::create(CCAnimate::create(animation)));
	this->addChild(m_mainBody);
	//初始化爆炸,不可见
	m_boom = CCSprite::create("boom1.png");
	this->addChild(m_boom);
	m_boom->setVisible(false);
	m_isLife = true;

敌人移动动画

	int type = CCRANDOM_0_1() * 4;//4种随机移动
	//敌人运动轨迹(贝尔曲线)
	ccBezierConfig bezier2;
	bezier2.controlPoint_1 = CCPointMake(this->getPosition().x - 400,330);
	bezier2.controlPoint_2 = CCPointMake(this->getPosition().x + 400,280);
	bezier2.endPosition = CCPointMake(this->getPosition().x,-70);
	CCBezierTo * bezierBy2 = CCBezierTo::create(6, bezier2);
	ccBezierConfig bezier1;
	bezier1.controlPoint_1 = CCPointMake(this->getPosition().x + 400,330);
	bezier1.controlPoint_2 = CCPointMake(this->getPosition().x - 400,280);
	bezier1.endPosition = CCPointMake(this->getPosition().x,-70);
	CCBezierTo * bezierBy1 = CCBezierTo::create(6, bezier1);
	switch(type)
	{
	case 0:
	case 1:
		this->runAction(CCSequence::create(CCMoveBy::create(6,ccp(0,-600)),CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL));
		break;
	case 2:
		this->runAction(CCSequence::create(bezierBy2,CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL));
		break;
	case 3:
		this->runAction(CCSequence::create(bezierBy1,CCCallFuncN::create(this, callfuncN_selector(GameObjEnemy::reStart)),NULL));
		break;
	}
	schedule(schedule_selector(GameObjEnemy::releaseBullet), 1.2f);

当第一波敌人过屏以后,又一次设置一波敌人初始位置及移动的动画, 这样敌人就无限循环出现

void GameObjEnemy::reStart( CCNode* pSender )
{
	m_mainBody->setVisible(true);
	m_boom->setVisible(false);
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	this->setPosition(ccp(size.width/4 * m_type,size.height + 50));
	m_isLife = true;
	this->moveStart();
}

每一个敌人发射子弹

void GameObjEnemy::releaseBullet( float dt )
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCPoint pos = this->getPosition();//敌人位置
	if(pos.y > 20 && pos.y < size.height && m_isLife)//敌人出在屏幕上才干发
	{
		GameMainScene *p = (GameMainScene *) this->getParent();
		p->releaseEnemyBullet(pos.x,pos.y - 30);//发射子弹
	}
}
void GameMainScene::releaseEnemyBullet( int x,int y )
{
	for(unsigned int i = 0; i<m_enemyBullets->capacity(); i++)//每一个敌人的子弹10发
	{
		if(!((GameEnemyBullet*)m_enemyBullets->objectAtIndex(i))->getIsvisble())
		{
			((GameEnemyBullet*)m_enemyBullets->objectAtIndex(i))->setPosition(ccp(x,y));
			((GameEnemyBullet*)m_enemyBullets->objectAtIndex(i))->setIsVisable();//子弹动画
			break;
		}
	}
}
void GameEnemyBullet::setIsVisable()
{
	this->setVisible(true);
	m_isVisable = true;
	this->runAction(CCRepeatForever::create(CCRotateBy::create(1,360)));
	this->runAction(CCSequence::create(CCMoveTo::create((this->getPosition().y + 50)/150,ccp(this->getPosition().x,-50)),CCCallFuncN::create(this, callfuncN_selector(GameEnemyBullet::setIsNotVisable)),NULL));;
}

void GameEnemyBullet::setIsNotVisable(CCNode* pSender)
{
	this->setVisible(false);
	m_isVisable = false;
	this->stopAllActions();
}

游戏主页面update定时器进行游戏背景动画处理和主角与敌人攻击碰撞检測

游戏背景动画至上往下移动,当第1张图滑过底部时,第2张图连接上,每次移动2

游戏背景动画至上往下移动,当第1张图滑过底部时,第2张图连接上,每次移动2
	m_bg1->setPosition(ccp(m_bg1->getPosition().x,m_bg1->getPosition().y - 2));
	m_bg2->setPosition(ccp(m_bg2->getPosition().x,m_bg2->getPosition().y - 2));
	if(m_bg2->getPosition().y <= 0)
	{
		float temp = m_bg2->getPosition().y + 480;
		m_bg1->setPosition(ccp(m_bg2->getPosition().x,temp));
	}
	if(m_bg1->getPosition().y <= 0)
	{
		float temp = m_bg1->getPosition().y + 480;
		m_bg2->setPosition(ccp(m_bg1->getPosition().x,temp));
	}

三种碰撞检測

敌人和主角、主角子弹碰撞检測

CCPoint hpos = m_hero->getPosition();//主角坐标
 	for(size_t i = 0;i < m_enemys->capacity();i ++)//遍历敌人
 	{
 		GameObjEnemy * enemy = ((GameObjEnemy *) m_enemys->objectAtIndex(i));//单个敌人
 		CCPoint epos = enemy->getPosition();
 		if(enemy->m_isLife)//子弹击中敌人
 		{
 			for(size_t i = 0;i < m_heroBullets->capacity();i ++)//主角子弹
 			{
 				if(((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->getIsvisble())
 				{
 					if(isHit(((GameHeroBullet *)m_heroBullets->objectAtIndex(i))->getPosition(),epos,5,13,21,28))//主角子弹打中敌人碰撞检測
 					{
 						enemy->setDie();//敌人消失,有爆炸动画
 						m_gameMark->addNumber(200);//加分
 						break;
 					}
 				}
 			}
 		}
		//敌人和主角碰撞
		if(!m_isReduce && enemy->m_isLife && isHit(hpos,epos,21,22.5,21,28))
		{
			enemy->setDie();
			setHeroHurt();//主角掉命
		}
	}

主角被敌人子弹打中碰撞检測

	if(!m_isReduce)
	{
		for(size_t i = 0;i < m_enemyBullets->capacity();i ++)//敌人的子弹10发
		{
			if(isHit(hpos,((GameEnemyBullet *)m_enemyBullets->objectAtIndex(i))->getPosition(),21,22.5,5,13))
			{
				setHeroHurt();//主角掉命
			}
		}
	}

推断两个矩形是否碰撞

bool GameMainScene::isHit( CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2 )
{
	if(abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2)
	{
		return true;
	}
	return false;
}

主角掉命

void GameMainScene::setHeroHurt()
{
	m_hero->stopAllActions();
	switch(m_blood)//初始3
	{
	case 3:
		m_blood1->setVisible(false);//命图标不可见
		m_blood --;
		break;
	case 2:
		m_blood2->setVisible(false);
		m_blood --;
		break;
	case 1:
		m_blood3->setVisible(false);
		m_blood --;
		break;
	case 0:
		if(! m_isGameOver)
		{
			m_isGameOver = true;
			setGameOver();//3条命用完,游戏结束
		}
		break;
	}
 	CCActionInterval*  action = CCBlink::create(5, 10);//3秒内闪烁6次
 	m_hero->runAction(action);
	scheduleOnce(schedule_selector(GameMainScene::resetReduce), 5.0f);//5秒后运行一次
	m_isReduce = true;//5秒不受伤害
}
void GameMainScene::resetReduce( float dt )
{
	m_isReduce = false;//又一次能收到伤害
}

敌人消失后爆炸

void GameObjEnemy::setDie()
{
	//敌人消失
	m_isLife = false;
	m_mainBody->setVisible(false);
	m_boom->setVisible(true);
	this->stopAllActions();
	//爆炸动画
	CCAnimation* boomAnimation = CCAnimation::create();
	boomAnimation->addSpriteFrameWithFileName("boom1.png");
	boomAnimation->addSpriteFrameWithFileName("boom2.png");
	boomAnimation->addSpriteFrameWithFileName("boom3.png");
	boomAnimation->addSpriteFrameWithFileName("boom4.png");
	boomAnimation->addSpriteFrameWithFileName("boom5.png");
	boomAnimation->setDelayPerUnit(0.1f);
	boomAnimation->setRestoreOriginalFrame(true);
	m_boom->runAction(CCSequence::create(CCAnimate::create(boomAnimation),CCCallFuncN::create(this,
	  callfuncN_selector(GameObjEnemy::reStart)),NULL));
}

竖屏小游戏--喵星战争源代码分析【完整】,布布扣,bubuko.com

时间: 2024-10-18 18:38:28

竖屏小游戏--喵星战争源代码分析【完整】的相关文章

横屏小游戏--萝莉快跑源代码分析三

主角出场: 初始化主角 hero = new GameObjHero(); hero->setScale(0.5); hero->setPosition(ccp(100,160)); hero->setVisible(false); addChild(hero,1); 进入GameObjHero类ccp文件 创建主角及动作 this->setContentSize(CCSizeMake(85,90)); //接收触摸事件 CCDirector* pDirector = CCDire

喵星战争源码分析

 转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/26942311 Ccp文件介绍: GameMenuScene.cpp游戏主菜单 GameMainScene.cpp游戏主页面 GameObjHero.cpp主角 GameHeroBullet.cpp主角的子弹 GameObjEnemy.cpp敌人 GameEnemyBullet.cpp敌人的子弹 GameAboutScene.cpp关于页面 GameMark.cpp游戏分数 运行游戏

简单的5*5,五子棋小游戏

使用C语言简单的实现棋盘为5*5的五子棋小游戏,以下为源代码: #include <stdio.h> #include <stdlib.h> //初始化一个数组 void Init(char arr[5][5])        //arr数组大小可以改变棋盘大小 {     int i = 0, j = 0;     for (i = 0; i < 5; i++)     {         for (j = 0; j < 5; j++)         {      

青瓷qici - H5小游戏 抽奖机 0 创建工程

安装运行平台需要nodejs,具体方法请参照官方说明文档. 运行后打开了一个空空的窗口. 首先我们进行工程设置,菜单>工程>设置 菜单里面设置我们游戏的名称,到时候会显示在游戏的title上面,设置版本 这里其中关键的地方我们要设计一个竖屏的游戏,分辨率是640*960,游戏帧率设置为30帧,让编辑时和实际手机运行效果同等. 应用,生效设计到工程中. 保存我们当前未命名的场景到工作目录中,以后我们运用的所有变化就在这个场景之中了. 注意尽量少使用中文,毕竟大家都懂得..

【开发记录】微信小游戏开发入门——俄罗斯方块

叨叨 我在前一阵子,打算做一个微信小游戏,当然是单机的,只是为了了解小游戏开发的过程,最终选择了俄罗斯方块这一经典小游戏作为demo,源代码已托管值github,当然,这个游戏demo对用不并不友好,但是已经可以让我入门小程序开发了XD. demo地址:https://github.com/nbclw/Laya_Brick 准备 在任何开发前都需要对要开发的东西有一定的了解.准备: 小游戏原理:微信小游戏是属于H5游戏的一种吧,我是这样理解的:在H5中,有一个叫Canvas(画布)的存在,与电脑

小游戏●两人对战

利用结构体编写的两人对战小游戏,代码及简要分析如下 1 public struct Fighter 2 { 3 public string name; 4 public int blood; 5 public int attack; 6 public int defense; 7 public int hit; 8 public int hide; 9 } 10 11 12 static void Main(string[] arge) 13 { 14 15 //定义一组对手 16 Fighte

【python游戏编程之旅】第九篇---嗷大喵快跑小游戏开发实例

本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 前几期博客我们一起学习了,pygame中的冲突检测技术以及一些常用的数据结构. 这次我们来一起做一个简单的酷跑类游戏综合运用以前学到的知识. 程序下载地址:http://yunpan.cn/cLIcJgTvq4tZS 访问密码 901f 源代码网盘地址:http://yunpan.cn/cLIc67S4nNRFY 访问密码 c139 github地址:https://github.com/XINCGer/

【python游戏编程之旅】第五篇---嗷大喵爱吃鱼小游戏开发实例

本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 我们一同在前几期的博客中已经学到了很多pygame的基本知识了,现在该做个小游戏实战一下了. 前几期博客链接: 第一篇:初识pygame  http://www.cnblogs.com/msxh/p/4966899.html 第二篇:pygame中的IO.数据  http://www.cnblogs.com/msxh/p/4973003.html 第三篇:pygame事件与设备轮询  http://www

圣诞节寻找圣诞老人小游戏源代码,上传即可使用

圣诞节快到了,提前祝大家节日快乐,在这也给打击分享个应景的寻找圣诞老人小游戏的源码 来自图灵机器人官方微社区的分享,是一个比拼记忆力,寻找圣诞老人的html5小游戏,直接下载源码,上传至服务器就能够使用,游戏操作简单,但是有一定的难度,体验不错.体验地址:http://182.92.67.78/game2/index.html 这个部分是游戏的主页面代码,源代码已上传,下载上传至自己服务器即可使用 <html> <!--图灵机器人圣诞节小游戏--> <!--下载源文件上传至服