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 条评论
登录 后参与评论

相关文章

来自专栏WindCoder

Canvas初实现拍照小游戏

由于实现的比较简单,且在部分机型上会出现点小问题,此处仅作为js代码的记录,暂不打算写相关教程。

532
来自专栏CreateAMind

基于ROS和gazebo对gym中robotics扩展

This work presents an extension of the initial OpenAI gym for robotics using ROS...

922
来自专栏xingoo, 一个梦想做发明家的程序员

Spark踩坑——java.lang.AbstractMethodError

百度了一下说是版本不一致导致的。于是重新检查各个jar包,发现spark-sql-kafka的版本是2.2,而spark的版本是2.3,修改spark-sql-...

920
来自专栏张善友的专栏

Div Scroll Bar (用层模拟滚动条)

对Div的盒模型以及Css控制定位都不熟,所以遇到了不少BT问题……好在最终突破了种种困难,基本实现了自己想要的效果。 说明: 最大的突破是通过了 xhtml...

20010
来自专栏ml

HDUOJ---(4708)Herding

Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J...

33310
来自专栏CreateAMind

条件GAN重大改进!cGANs with Projection Discriminator

https://github.com/pfnet-research/sngan_projection

1032
来自专栏转载gongluck的CSDN博客

cocos2dx 连连看

#include "GameLink.h" #include "CountDownBar.h" USING_NS_CC; Scene* GameLink::...

2815
来自专栏数据结构与算法

BZOJ 3053: The Closest M Points(K-D Tree)

1500
来自专栏小樱的经验随笔

“玲珑杯”ACM比赛 Round #12题解&源码

我能说我比较傻么!就只能做一道签到题,没办法,我就先写下A题的题解&源码吧,把官方给出的题解贴出来! A -- Niro plays Galaxy Note ...

3557
来自专栏码匠的流水账

聊聊SecurityContextPersistenceFilter

本文主要研究下SecurityContextPersistenceFilter的作用。

552

扫码关注云+社区