cocos2dx 打灰机

#include "GamePlane.h"
#include "PlaneSprite.h"
#include "BulletNode.h"
#include "EnemyPlane.h"
#include "EnemyBullet.h"

USING_NS_CC;

Scene* GamePlane::createScene()
{
	auto scene = Scene::create();
	auto layer = GamePlane::create();
	scene->addChild(layer);
	return scene;
}

bool GamePlane::init()
{
	if (!Layer::init())
		return false;

	auto layer = Layer::create();
	bg[0] = Sprite::create("background.png");
	bg[0]->setAnchorPoint(Vec2(0, 0));
	bg[0]->setPosition(0, 0);
	layer->addChild(bg[0]);
	bg[1] = Sprite::create("background.png");
	bg[1]->setAnchorPoint(Vec2(0, 0));
	bg[1]->setPosition(0, HEIGH);
	layer->addChild(bg[1]);
	bg[2] = Sprite::create("plane_background.png");
	bg[2]->setAnchorPoint(Vec2(0, 0));
	bg[2]->setPosition(0, 0);
	layer->addChild(bg[2]);
	bg[3] = Sprite::create("plane_background.png");
	bg[3]->setAnchorPoint(Vec2(0, 0));
	bg[3]->setPosition(0, -HEIGH);
	layer->addChild(bg[3]);
	addChild(layer);

	auto plane = PlaneSprite::createPlane(Point(180, 100));
	plane->setTag(PLANE);
	addChild(plane);

	for (int i = 0; i < BULLE; ++i)
	{
		bullets[i] = Bullet::createBullet(Point(0, 660));
		addChild(bullets[i]);
	}
	for (int i = 0; i < BULLE; ++i)
	{
		enemyBullet[i] = EnemyBullet::createBullet(Point(0, 720));
		addChild(enemyBullet[i]);
	}


	scheduleUpdate();

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(GamePlane::touchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(GamePlane::touchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(GamePlane::touchEnded, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

	for (int i = 0; i < ENEMY; ++i)
	{
		enemy[i] = EnemyPlane::create();
		addChild(enemy[i]);
	}

	return true;
}

void GamePlane::update(float dt)
{
	bg[0]->setPositionY(bg[0]->getPositionY() - 1);
	bg[1]->setPositionY(bg[1]->getPositionY() - 1);
	if (bg[0]->getPositionY() <= -HEIGH)
	{
		bg[0]->setPositionY(HEIGH);
	}
	if (bg[1]->getPositionY() <= -HEIGH)
	{
		bg[1]->setPositionY(HEIGH);
	}

	bg[2]->setPositionY(bg[2]->getPositionY() + 2);
	bg[3]->setPositionY(bg[3]->getPositionY() + 2);
	if (bg[2]->getPositionY() >= HEIGH)
	{
		bg[2]->setPositionY(-HEIGH);
	}
	if (bg[3]->getPositionY() >= HEIGH)
	{
		bg[3]->setPositionY(-HEIGH);
	}

	auto plane = (PlaneSprite*)getChildByTag(PLANE);
	for (int i = 0; i < BULLE; ++i)
	{
		if (!bullets[i]->Using())
		{
			bullets[i]->shoot(plane->getPosition());
			break;
		}
	}
	for (int i = 0; i < BULLE; ++i)
	{
		if (bullets[i]->Using() && bullets[i]->getPosY() == 660)
			bullets[i]->setUnUse();
	}

	static int enemyBulletLock = 0;
	++enemyBulletLock;
	if (enemyBulletLock % 100 == 0)
	{
		for (int i = 0; i < ENEMY; ++i)
		{
			if (enemy[i]->getType() == 1)
			{
				for (int j = 0; j < BULLE; ++j)
				{
					if (!enemyBullet[j]->Using())
					{
						enemyBullet[j]->shoot(enemy[i]->getPosition());
						break;
					}
				}
			}
		}
	}
	for (int i = 0; i < BULLE; ++i)
	{
		if (enemyBullet[i]->Using() && enemyBullet[i]->getPositionY() == -80)
			enemyBullet[i]->setUnUse();
	}

	static float t = 0;
	t += dt;
	if (t < 0.1)
	{
		return;
	}
	t = 0;
	
	static int number = time(NULL);
	srand(number);
	number = rand();
	srand(number);
	int num = number % 150;
	int type = 0;
	bool fly = false;
	switch (num)
	{
		
	case 30:
	case 120:
		type = 1;
		fly = true;
		break;
	case 2:
	case 22:
		type = 2;
		fly = true;
		break;
	case 3:
	case 33:
		type = 3;
		fly = true;
		break;
	default:
		type = 0;
		fly = false;
		break;
	}
	if (fly)
	{
		for (int i = 0; i < ENEMY; ++i)
		{
			if (enemy[i]->getType() == 0)
			{
				enemy[i]->start(type, num * number % 360);
				break;
			}
		}
		for (int i = 0; i < ENEMY; ++i)
		{
			if (enemy[i]->getPositionY() == -80)
			{
				enemy[i]->setType(0);
			}
		}
	}

	checkEnemy();
}

bool GamePlane::touchBegan(cocos2d::Touch* touch, cocos2d::Event*)
{
	auto plane = (PlaneSprite*)getChildByTag(PLANE);
	auto tPos = touch->getLocation();
	if (plane->boundingBox().containsPoint(tPos))
	{
		return true;
	}
	return false;
}
void GamePlane::touchMoved(cocos2d::Touch* touch, cocos2d::Event*)
{
	auto plane = (PlaneSprite*)getChildByTag(PLANE);
	plane->setPosition(plane->getPosition() + touch->getDelta());
	if (touch->getDelta().x < 0)
		plane->setRotation3D(Vec3(0, 0, -10));
	else if (touch->getDelta().x > 0)
		plane->setRotation3D(Vec3(0, 0, 10));
	/*else
		plane->setRotation3D(Vec3(0, 0, 0));*/
}
void GamePlane::touchEnded(cocos2d::Touch*, cocos2d::Event*)
{
	auto plane = (PlaneSprite*)getChildByTag(PLANE);
	plane->setRotation3D(Vec3(0, 0, 0));
}

void GamePlane::checkEnemy()
{
	for (int i = 0; i < BULLE; ++i)
	{
		if (bullets[i]->Using())
		{
			for (int j = 0; j < ENEMY; ++j)
			{
				if (enemy[j] > 0)
				{
					auto pos1 = enemy[j]->getPosition();
					auto pos2 = bullets[i]->getPos();
					if (fabs(pos1.x - pos2.x) < 40 && fabs(pos1.y - pos2.y) < 30)
					{
						enemy[j]->die();
						bullets[i]->die();
					}
				}
			}
		}
	}
}
#include "BulletNode.h"

USING_NS_CC;

bool Bullet::init()
{
	if (!Node::init())
		return false;
	return true;
}

Bullet* Bullet::createBullet(cocos2d::Point pos)
{
	auto bullet = new Bullet();
	if (bullet && bullet->init())
	{
		bullet->autorelease();
		bullet->initBullet(pos);
	}
	else
	{
		delete bullet;
		bullet = NULL;
	}
	return bullet;
}

void Bullet::initBullet(cocos2d::Point pos)
{
	isUsing = false;
	bullet = Sprite::create("bullet.png");
	bullet->setPosition(pos);
	auto blink = Blink::create(1, 3);
	auto action = RepeatForever::create(blink);
	bullet->runAction(action);
	addChild(bullet);
}

void Bullet::shoot(Point pos)
{
	bullet->setPosition(pos);
	auto moveto = MoveTo::create((660 - pos.y)*0.25 / 660, Vec2(pos.x, 660));
	bullet->runAction(moveto);
	isUsing = true;
}

void Bullet::die()
{
	bullet->stopAllActions();
	bullet->setPosition(0, 660);
	setUnUse();
}
#include "EnemyBullet.h"

USING_NS_CC;

bool EnemyBullet::init()
{
	if (!Node::init())
		return false;
	return true;
}

EnemyBullet* EnemyBullet::createBullet(cocos2d::Point pos)
{
	auto bullet = new EnemyBullet();
	if (bullet && bullet->init())
	{
		bullet->autorelease();
		bullet->initBullet(pos);
	}
	else
	{
		delete bullet;
		bullet = NULL;
	}
	return bullet;
}

void EnemyBullet::initBullet(cocos2d::Point pos)
{
	bullet = Sprite::create("enemybullet.png");
	bullet->setScale(0.3);
	setPosition(pos);
	isUsing = false;
	addChild(bullet);
}

void EnemyBullet::shoot(Point pos)
{
	isUsing = true;
	auto moveto = MoveTo::create((pos.y + 80)*3.0 / 800,Vec2(pos.x,-80));
	setPosition(pos);
	runAction(moveto);
}
#include "EnemyPlane.h"

USING_NS_CC;

bool EnemyPlane::init()
{
	if (!Sprite::init())
		return false;

	type = 0;
	setPosition(-100, -100);

	return true;
}

void EnemyPlane::start(int type, int posx)
{
	this->type = type;
	switch (type)
	{
	case 1:
		setDisplayFrame(SpriteFrame::create("enemy1.png", Rect(0, 0, 256, 256)));
		setScale(0.5f);
		blood = 25;
		break;
	case 2:
		setDisplayFrame(SpriteFrame::create("enemy2.png", Rect(0, 0, 256, 256)));
		setScale(0.25f);
		blood = 10;
		break;
	case 3:
		setDisplayFrame(SpriteFrame::create("enemy3.png", Rect(0, 0, 220, 220)));
		setScale(0.1f);
		blood = 5;
		break;
	default:
		break;
	}
	setPosition(posx, HEIGH + 80);
	auto moveto = MoveTo::create(18 / type, Vec2(posx, -80));
	auto rotate = RotateTo::create(18 / type, Vec3(0, 0, 1800));
	auto spawn = Spawn::create(moveto, rotate, NULL);
	runAction(spawn);
}

void EnemyPlane::die()
{
	type = 0;
	setPosition(-100, -100);
}
#include "PlaneSprite.h"

USING_NS_CC;

bool PlaneSprite::init()
{
	if (!Sprite::init())
		return false;
	return true;
}

PlaneSprite* PlaneSprite::createPlane(cocos2d::Point pos)
{
	auto plane = new PlaneSprite();
	if (plane && plane->init())
	{
		plane->autorelease();
		plane->initPlane(pos);
	}
	else
	{
		delete plane;
		plane = NULL;
	}
	return plane;
}
void PlaneSprite::initPlane(cocos2d::Point pos)
{
	setDisplayFrame(SpriteFrame::create("plane1.png",Rect(0,0,340,335)));
	setScale(0.3);
	setAnchorPoint(Vec2(0.5, 0.5));
	setPosition(pos);

	auto animation = Animation::create();
	animation->addSpriteFrameWithFile("plane1.png");
	animation->addSpriteFrameWithFile("plane2.png");
	animation->setLoops(-1);
	animation->setDelayPerUnit(0.2f);
	auto animate = Animate::create(animation);
	runAction(animate);
}

源码下载

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

通过SmtpClient发送Exchange会议邮件

看到C#中调用Outlook API 发起会议 ,这个完全可以用SMTP方式实现的,下面我的项目中使用的代码: 对于.NET而言,从2.0开始,发邮件已经是一件...

1779
来自专栏我和未来有约会

flash读取XML 背景自动适应大小

var bg = new Object(); bg.bgdoc_xml = new XML(); bg.parse_XML = function() {    ...

1915
来自专栏吴伟祥

Spring4 @Configuration源码

442
来自专栏王磊的博客

C# 控制台应用程序输出颜色字体[更正版]

首先感谢院子里的“yanxinchen”,之前的方法是通过c#调用系统api实现的,相比之下我的有点画蛇添足了,哈哈。 最佳解决方案的代码: static vo...

3518
来自专栏布尔

加强的alert confirm js自定义对话框 多个按钮 ICO自定义

效果图: ? <html> <head>     <title>对话框</title>     <link rel="stylesheet" type="tex...

20810
来自专栏linux驱动个人学习

高通msm8909耳机调试

1、DTS相应修改: DTS相关代码:kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi: 1 s...

5245
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

742
来自专栏函数式编程语言及工具

Akka(31): Http:High-Level-Api,Route rejection handling

   Route 是Akka-http routing DSL的核心部分,使用户能比较方便的从http-server的角度筛选http-request、进行se...

2157
来自专栏杨建荣的学习笔记

生产环境sql语句调优实战第五篇(r2笔记41天)

今天在生产环境中发现一条sql语句尽管走了主键索引,但是查询还是很慢。 sql语句类似下面的形式: SELECT /*+ index (bl1_cyc_paye...

3358
来自专栏增长技术

Android 图片功能模块相关库

If you want to let your users choose crop ratio dynamically, just do not call wi...

542

扫描关注云+社区