前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cocos2dx 消灭星星

cocos2dx 消灭星星

作者头像
_gongluck
发布2018-03-08 14:45:13
1.1K0
发布2018-03-08 14:45:13
举报
代码语言:javascript
复制
#include "GamePopStar.h"
#include "StarSprite.h"

USING_NS_CC;

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

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

	addStars();
	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(GamePopStar::touchBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
	scheduleUpdate();

	return true;
}

void GamePopStar::addStars()
{
	srand(time(NULL));
	for (int w = 0; w < WIDTH; ++w)
	{
		for (int h = 0; h < HEIGH; ++h)
		{
			stars[w][h] = StarSprite::createStar(rand() % 5 + 1, w, h);
			addChild(stars[w][h]);
		}
	}
}

bool GamePopStar::touchBegan(cocos2d::Touch* touch, cocos2d::Event*)
{
	auto pos = touch->getLocation();
	if (pos.x < 40 * WIDTH && pos.y < 40 * HEIGH)
	{
		int x = pos.x / 40;
		int y = pos.y / 40;
		if (stars[x][y]->getType() != 0)
		{
			//stars[x][y]->deleStar();
			delStarsFrom(stars[x][y]->getType(), x, y);
			starsDrop();
			if (isOver())
				exit(-1);
		}
	}
	return false;
}

void GamePopStar::delStarsFrom(int type, int w, int h)
{
	if (type == 0)
		return;
	
	bool canDel = false;
	
	if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
	{
		delStarsFromRight(type, w - 1, h);
		canDel = true;
	}
	if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
	{
		delStarsFromLeft(type, w + 1, h);
		canDel = true;
	}
	if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
	{
		delStarsFromUp(type, w, h - 1);
		canDel = true;
	}
	if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
	{
		delStarsFromDown(type, w, h + 1);
		canDel = true;
	}

	if (canDel)
		stars[w][h]->deleStar();
}

void GamePopStar::delStarsFromUp(int type, int w, int h)
{
	stars[w][h]->deleStar();
	if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
		delStarsFromRight(type, w - 1, h);
	if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
		delStarsFromLeft(type, w + 1, h);
	if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
		delStarsFromUp(type, w, h - 1);
}
void GamePopStar::delStarsFromDown(int type, int w, int h)
{
	stars[w][h]->deleStar();
	if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
		delStarsFromRight(type, w - 1, h);
	if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
		delStarsFromLeft(type, w + 1, h);
	if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
		delStarsFromDown(type, w, h + 1);
}
void GamePopStar::delStarsFromLeft(int type, int w, int h)
{
	stars[w][h]->deleStar();
	if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
		delStarsFromLeft(type, w + 1, h);
	if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
		delStarsFromUp(type, w, h - 1);
	if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
		delStarsFromDown(type, w, h + 1);
}
void GamePopStar::delStarsFromRight(int type, int w, int h)
{
	stars[w][h]->deleStar();
	if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
		delStarsFromRight(type, w - 1, h);
	if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
		delStarsFromUp(type, w, h - 1);
	if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
		delStarsFromDown(type, w, h + 1);
}

void GamePopStar::starsDrop()
{
	//下落
	for (int w = 0; w < WIDTH; ++w)
	{
		for (int h = 1; h < HEIGH; ++h)
		{
			int num = 0;
			for (int i=0; i < h; ++i)
			{
				/*if (stars[w][i]->getType() == 0)
				{
					auto temp = stars[w][i];
					stars[w][i] = stars[w][h];
					stars[w][h] = temp;
					break;
				}*/
				if (stars[w][i]->getType() == 0)
					++num;
			}
			auto temp = stars[w][h];
			stars[w][h] = stars[w][h - num];
			stars[w][h - num] = temp;
			//stars[w][h]->move(1, w, h);
		}
	}
	//左移
	for (int w = 1; w < WIDTH; ++w)
	{
		for (int i = 0; i < w; ++i)
		{
			if (stars[i][0]->getType() == 0)
			{
				for (int h = 0; h < HEIGH; ++h)
				{
					auto temp = stars[i][h];
					stars[i][h] = stars[w][h];
					stars[w][h] = temp;
				}
				break;
			}
		}
	}

	for (int w = 0; w < WIDTH; ++w)
	{
		for (int h = 0; h < HEIGH; ++h)
		{
			stars[w][h]->move(0.5,w,h);
		}
	}
}

bool GamePopStar::isOver()
{
	for (int w = 0; w < WIDTH; ++w)
	{
		for (int h = 0; h < HEIGH; ++h)
		{
			auto type = stars[w][h]->getType();
			if (type == 0)
				continue;
			if (w - 1 >= 0 && stars[w - 1][h]->getType() == type)
			{
				return false;
			}
			if (w + 1 < WIDTH && stars[w + 1][h]->getType() == type)
			{
				return false;
			}
			if (h - 1 >= 0 && stars[w][h - 1]->getType() == type)
			{
				return false;
			}
			if (h + 1 < HEIGH && stars[w][h + 1]->getType() == type)
			{
				return false;
			}
		}
	}
	return true;
}

void GamePopStar::update(float dt)
{
	static int w = 0;
	static int h = 0;

	if (w < WIDTH && h < HEIGH)
	{
		stars[w][h]->move(0.1*(w+1)/(h+1), w, h);
		++w;
	}
	else if (h < HEIGH)
	{
		w = 0;
		++h;
	}
	else
	{
		unscheduleUpdate();
	}
}
代码语言:javascript
复制
#include "StarSprite.h"

USING_NS_CC;

StarSprite* StarSprite::createStar(int type, float x, float y)
{
	auto star = new StarSprite();
	if (star && star->init())
	{
		star->autorelease();
		star->initStar(type, x, y);
	}
	else
	{
		delete star;
		star = NULL;
	}
	return star;
}

bool StarSprite::init()
{
	if (!Layer::init())
		return false;
	return true;
}

void StarSprite::initStar(int type, float x, float y)
{
	this->type = type;
	star = Sprite::create(String::createWithFormat("card%d.png", type)->getCString());
	//star->setPosition(20 + 40 * x, 20 + 40 * y);
	star->setPosition(20 + 40 * x, 20 + 40 * 16);
	addChild(star);
}

void StarSprite::deleStar()
{
	this->type = 0;
	star->setSpriteFrame(SpriteFrame::create("card0.png", Rect(0, 0, 40, 40)));
}

void StarSprite::move(float dt, int w, int h)
{
	auto move = MoveTo::create(dt, Vec2(w * 40 + 20, h * 40 + 20));
	star->runAction(move);
}

代码下载

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年07月13日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档