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

相关文章

来自专栏一棹烟波

图像添加径向畸变

通常摄像机的镜头都会有镜头畸变,尤其是广角镜头,在做图像处理中往往会通过摄像机标定获取镜头的畸变系数,然后进行畸变校正。而在某些特殊的情况下,你可能会需要往图像...

2206
来自专栏软件工程师成长笔记

高德,百度,Google地图定位偏移以及坐标系转换

一般用国际GPS纪录仪记录下来的经纬度,通过GPS定位拿到的原始经纬度,Google和高德地图定位的的经纬度(国外)都是基于WGS-84坐标系的;但是在国内是不...

1853
来自专栏HT

根据矩阵变化实现基于 HTML5 的 WebGL 3D 自动布局

在数学中,矩阵是以行和列排列的数字,符号或表达式的矩形阵列,任何矩阵都可以通过相关字段的标量乘以元素。矩阵的主要应用是表示线性变换,即f(x)= 4 x等线性函...

2005
来自专栏懒人开发

(5.6)James Stewart Calculus 5th Edition:The Logarithm Defined as an Integral

我们凭借直觉,知道 指数函数,对数函数 为 反函数。 这里我们对它简单证明(略),并且确定一下对应的区域。

793
来自专栏生信小驿站

ggplot2 easyplot①

ggplot2.stripchart是一个易于使用的函数(来自easyGgplot2包),使用ggplot2绘图系统和R软件生成条带图。 条形图也被称为一维散点...

1095
来自专栏贾志刚-OpenCV学堂

使用OpenCV实现偏斜文档校正

使用OpenCV实现偏斜文档校正 纸质文档扫描中经常会发生扫描出来的图像有一定角度的偏斜,对后期的文档信息化OCR提取造成很大的干扰,导致OCR识别准确率下降从...

4197
来自专栏落影的专栏

iOS开发-OpenGLES进阶教程

教程 OpenGLES入门教程1-Tutorial01-GLKit OpenGLES入门教程2-Tutorial02-shader入门 OpenGLES入门...

3019
来自专栏视觉求索无尽也

算法:动态规划(DP)入门实践

832
来自专栏谦谦君子修罗刀

贝塞尔曲线之爱琴海 -- 定不负相思意

前言: 一个人有多不正经,就有多深情。一个程序员有多闷,就代表ta有多骚。 我不等山无棱,不等夏雨雪,不等天地合。因为即使江水为竭,即使冬雷震震,我都在你一...

34210
来自专栏图形学与OpenGL

6.5编程实例-立方体透视投影

GLint winWidth = 600, winHeight = 600; //设置初始化窗口大小

851

扫码关注云+社区