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

相关文章

来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2797
来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

5288
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.5K7
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3338
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

3050
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

39510
来自专栏ASP.NETCore

ASP.NET Core 整合Autofac和Castle实现自动AOP拦截

除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninjec...

764
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

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

Silverlight第三方控件专题

这里我收集整理了目前网上silverlight第三方控件的专题,若果有所遗漏请告知我一下。 名称 简介 截图 telerik 商 RadC...

4435
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

3017

扫码关注云+社区