cocos2dx 连连看

#include "GameLink.h"
#include "CountDownBar.h"

USING_NS_CC;

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

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

	auto map = TMXTiledMap::create("rec.tmx");
	map->setAnchorPoint(Vec2(0, 0));
	map->setPosition(0, 0);
	addChild(map);
	auto bg = Sprite::create("background.png");//LayerColor::create(Color4B(255, 255, 255, 255));
	bg->setAnchorPoint(Vec2(0, 0));
	bg->setPosition(0, 0);
	addChild(bg);
	mapLayer = map->layerNamed("Layer1");
	initBoard();

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(GameLink::touchBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
	position = Vec2(-1, -1);

	timeOut = false;
	time = FULLTIME;
	bar = CountDownBar::createCountDownBar(Vec2(0, 360), 480);
	addChild(bar);
	scheduleUpdate();

	auto button = MenuItemImage::create("button1.png", "button2.png",this,menu_selector(GameLink::findReach));
	auto menu = Menu::create(button, NULL);
	menu->setAnchorPoint(Vec2(0, 0));
	menu->setScale(0.4);
	menu->setPosition(560, 100);
	addChild(menu);

	return true;
}

void GameLink::initBoard()
{
	int num = 0;
	int cards[WIDTH*HEIGHT] = { 0 };
	for (int i = 0; i < WIDTH; ++i)
	{
		for (int j = 0; j < HEIGHT; ++j)
		{
			int id = mapLayer->getTileGIDAt(Vec2(i, j));
			if (mapLayer->getTileGIDAt(Vec2(i, j)) == 2)//id比Tile里面的大1
				++num;
		}
	}
	srand(::time(NULL));
	for (int i = 0; i < num / 2; ++i)
	{
		cards[i] = rand() % 5 + 1;
		cards[i + num / 2] = cards[i];
	}
	for (int i = 0; i < num; ++i)
	{
		int index = rand() % (num);
		int temp = cards[index];
		cards[index] = cards[i];
		cards[i] = temp;
	}
	//memcpy(this->cards, cards, sizeof(int)*WIDTH*HEIGHT);
	for (int x = 0; x < WIDTH; ++x)
	{
		for (int y = 0; y < HEIGHT; ++y)
		{
			if (mapLayer->getTileGIDAt(Vec2(x, y)) == 2)
			{
				sprites[x][y] = Sprite::create(String::createWithFormat("card%d.png", cards[--num])->getCString());
				this->cards[x][y] = cards[num];
			}
			else
			{
				sprites[x][y] = Sprite::create("card0.png");
				this->cards[x][y] = 0;
			}
			sprites[x][y]->setPosition(20+x*40, 20+y*40);
			addChild(sprites[x][y]);
		}
	}
}

bool GameLink::touchBegan(cocos2d::Touch* touch, cocos2d::Event*)
{
	auto pos = touch->getLocation();
	int x = pos.x / 40;
	int y = pos.y / 40;
	if (x < WIDTH && y < HEIGHT)
	{
		if (position != Vec2(-1, -1))
		{
			int i = (int)position.x / 40;
			int j = (int)position.y / 40;
			if (i != x || j != y)
			{
				if (cards[x][y] == cards[i][j] && canReach(x,y,i,j))
				{
					auto frame = SpriteFrame::create("card0.png", Rect(0, 0, 40, 40));
					sprites[x][y]->setSpriteFrame(frame);
					sprites[i][j]->setSpriteFrame(frame);
					sprites[i][j]->setOpacity(255);
					cards[x][y] = cards[i][j] = 0;
					position = Vec2(-1, -1);
				}
				else
				{
					sprites[i][j]->setOpacity(255);
					sprites[x][y]->setOpacity(180);
					position = pos;
				}
			}
		}
		else
		{
			position = pos;
			sprites[x][y]->setOpacity(100);
		}
	}
	return false;
}

bool GameLink::noCorner(int x1, int y1, int x2, int y2)
{
	if (x1 == x2)
	{
		auto start = y1 < y2 ? y1 : y2;
		auto end = y1 > y2 ? y1 : y2;
		for (int i = start + 1; i < end; ++i)
		{
			if (cards[x1][i] > 0)
				return false;
		}
		return true;
	}
	else if (y1 == y2)
	{
		auto start = x1 < x2 ? x1 : x2;
		auto end = x1 > x2 ? x1 : x2;
		for (int i = start + 1; i < end; ++i)
		{
			if (cards[i][y1] > 0)
				return false;
		}
		return true;
	}
	else
		return false;
}

bool GameLink::oneCorner(int x1, int y1, int x2, int y2)
{
	if (!(x1 != x2 && y1 != y2))
		return false;
	if (cards[x1][y2] == 0)
	{
		if (noCorner(x1, y1, x1, y2) && noCorner(x2, y2, x1, y2))
			return true;
	}
	else if (cards[x2][y1] == 0)
	{
		if (noCorner(x1, y1, x2, y1) && noCorner(x2, y2, x2, y1))
			return true;
	}
	else
		return false;
}

bool GameLink::twoCorner(int x1, int y1, int x2, int y2)
{
	/*if (!(x1 != x2 && y1 != y2))
		return false;*/
	for (int i = 0; i < WIDTH; ++i)
	{
		if (cards[i][y1] > 0)
			continue;
		if (noCorner(i, y1, x1, y1) && oneCorner(i, y1, x2, y2))
			return true;
	}
	for (int i = 0; i < HEIGHT; ++i)
	{
		if (cards[x1][i] > 0)
			continue;
		if (noCorner(x1, i, x1, y1) && oneCorner(x1, i, x2, y2))
			return true;
	}
	return false;
}

bool GameLink::canReach(int x1, int y1, int x2, int y2)
{
	if (noCorner(x1, y1, x2, y2))
		return true;
	else if (oneCorner(x1, y1, x2, y2))
		return true;
	else if (twoCorner(x1, y1, x2, y2))
		return true;
	else
		return false;
}

void GameLink::update(float dt)
{
	if (time > 0)
	{
		bar->barUpdate(time, FULLTIME);
		--time;
	}
	else
	{
		if (!timeOut)
		{
			timeOut = true;
		}
	}
}

void GameLink::findReach(Ref*)
{
	for (int i = 0; i < WIDTH; ++i)
	{
		for (int j = 0; j < HEIGHT; ++j)
		{
			if (cards[i][j] == 0)
				continue;
			for (int k = 0; k < WIDTH; ++k)
			{
				for (int l = 0; l < HEIGHT; ++l)
				{
					if (cards[k][l] == 0)
						continue;
					if (i == k && j == l)
						continue;
					if (cards[i][j] != cards[k][l])
						continue;
					if (canReach(i, j, k, l))
					{
						auto blink = Blink::create(5, 10);
						auto clone = blink->clone();
						sprites[i][j]->runAction(blink);
						sprites[k][l]->runAction(clone);
						return ;
					}
				}
			}
		}
	}
	return;
}
#include "CountDownBar.h"

USING_NS_CC;

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

CountDownBar* CountDownBar::createCountDownBar(cocos2d::Vec2 pos, int size)
{
	auto bar = new CountDownBar();
	if (bar && bar->init())
	{
		bar->autorelease();
		bar->initCountDownBar(pos, size);
	}
	else
	{
		delete bar;
		bar = NULL;
	}
	return bar;
}
bool CountDownBar::initCountDownBar(cocos2d::Vec2 pos, int size)
{
	bg = Sprite::create("barBG.png");
	bg->setAnchorPoint(Vec2(0, 1));
	bg->setScaleX(float(size) / 80);
	bg->setPosition(pos);
	addChild(bg);
	bar = Sprite::create("bar.png");
	bar->setAnchorPoint(Vec2(0, 0));
	bar->setPosition(0,0);
	bg->addChild(bar);

	return true;
}

void CountDownBar::barUpdate(int time, int full)
{
	bar->setScaleX((float)time / full);
}

代码下载

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏五毛程序员

五毛的cocos2d-x学习笔记05-场景与场景动画,动作

1686
来自专栏编程之旅

iOS开发——Core Graphics绘图

我们在搭建UI界面时,有很多时候,我们会用到iOS自带的绘图功能来完成一些界面的效果,很常用也很方便。今天我们在这里就一起讨论一下iOS的绘图功能。

622
来自专栏飞扬的花生

合并两个结构完全相同的DataTable

两个结构一模一样的DataTable如何合并? 例子:使用Winform进行演示,表2的数据为固定的,表1的数据可以动态添加,通过合并按钮合并表1和表2的数据到...

1635
来自专栏更流畅、简洁的软件开发方式

[自定义服务器控件] 第二步:下拉列表框。

前面发了一个文本框的,这回发一个下拉列表框。 一般在写自定义控件之前都要考虑一下原来的控件(系统代的)有什么优缺点,有哪些功能是我想要的,但是自带的控件没有提...

2306
来自专栏码匠的流水账

聊聊Color中的alpha值

color对象里头的alpha其实是指不透明度,其值范围为0-255,越大越不透明。 其通常对应opacity,这个就是单词语义表达的不透明度,其值范围[0,1...

402
来自专栏hightopo

电信网络拓扑图自动布局之总线

1064
来自专栏魂祭心

原 大数运算

3275
来自专栏更流畅、简洁的软件开发方式

基类、接口的应用——表单控件:一次添加、修改一条记录,一次修改多条记录。(上)

好久没发帖子了,又加了不少的功能呀。(图片仅是测试,不代表什么表情。) 本来我也想写一个2007的总结的,但是看到很多人都写了,我就不凑热闹了,写点和代码有关...

1725
来自专栏HT

电信网络拓扑图自动布局之总线

在前面《电信网络拓扑图自动布局》一文中,我们大体介绍了 HT for Web 电信网络拓扑图自动布局的相关知识,但是都没有深入地描述各种自动布局的用法,我们今天...

1738
来自专栏算法channel

用Word排版伪代码out了,推荐这个工具

多少次你看到别人的的伪代码排版如此完美而惊艳,心中不免好奇,怎么设计的?今天,和大家一起学习一款流行的排版利器:CTeX

1170

扫描关注云+社区