cocos2dx 消灭星星

#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();
	}
}
#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);
}

代码下载

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏运维

cronie所依赖的包

CentOS6.3 x86_64 最小化安装版安装cronie yum -y install cronie 通过这个命令可得出安装cronie所得依赖包如下

612
来自专栏Jerry的SAP技术分享

如何处理错误消息Please install the gcc make perl packages

Please install the gcc make perl packages from your distribution。

1353
来自专栏后端之路

SpringBoot实现Mybatis多数据源方案

背景 目前报表导出需要多数据库的数据,因此我们需要做Mybatis多数据源的配置 我们之前使用Spring的AbstractRoutingDataSource ...

6947
来自专栏一个默默无闻的工程师的日常

nova boot vm with '--nic net-id=xxxx, v4-fixed-ip=xxx' failed

973
来自专栏三杯水

CentOS 7.5安装部署Jewel版本Ceph集群

1、修改cpeh.conf文件 注意mon为奇数,如果为偶数,有一个不会安装,另外设置好public_network,并稍微增大mon之间时差允许范围(默认为...

612
来自专栏Netkiller

Spring boot with Hive

本文节选自《Netkiller Java 手札》 摘要: spring boot 1.5.6 + hive 2.3.0 + hadoop 2.5.0 + hb...

8938
来自专栏编码前线

在Android logcat中打印FFmpeg调试信息

在FFmpeg源码中的ffmpeg.c文件的main函数中有avlogsetcallback的调用,而logcallback_null是个空的回调函数,一个思路...

451
来自专栏运维

CentOS6.3 x86_64 最小化安装后安装man

[root@localhost yum.repos.d]# yum -y install man Loaded plugins: fastestmirror,...

762
来自专栏运维

CentOS 7.5安装部署Jewel版本Ceph集群

1、修改cpeh.conf文件 注意mon为奇数,如果为偶数,有一个不会安装,另外设置好public_network,并稍微增大mon之间时差允许范围(默认为0...

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

容灾半自动化的实现思路(二) (r7笔记第93天)

容灾的半自动化的部分,自己写了下面的脚本,也算是一个基本实现,因为时间仓促,还是存在一些不足,稍后完善 整个切换的步骤分为三部分,第一部分是备份当前备库的配置文...

2328

扫码关注云+社区