cocos2dx 2048

#include "GameScene.h"
#include "CardSprite.h"
#include "ScoreBoard.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

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

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

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("1.mp3");

	auto size = Director::getInstance()->getVisibleSize();

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = CC_CALLBACK_2(GameLayer::touchBegan, this);
	listener->onTouchMoved = CC_CALLBACK_2(GameLayer::touchMoved, this);
	listener->onTouchEnded = CC_CALLBACK_2(GameLayer::touchEnded, this);
	auto dispacher = Director::getInstance()->getEventDispatcher();
	dispacher->addEventListenerWithSceneGraphPriority(listener, this);

	auto background = LayerColor::create(Color4B(255, 255, 255, 255));
	addChild(background);

	gameBoard = ScoreBoard::createBoard(2048, 120, 90, "Game", Vec2(15, size.height-100),Color4B(2,168,209,255));
	addChild(gameBoard);
	highBoard = ScoreBoard::createBoard(0, 90, 90, "high", Vec2(150, size.height - 100), Color4B(209, 168, 168, 255));
	addChild(highBoard);
	scoreBoard = ScoreBoard::createBoard(0, 90, 90, "score", Vec2(255, size.height - 100), Color4B(50, 100, 209, 255));
	addChild(scoreBoard);

	for (int i = 0; i < HCARDS; ++i)
	{
		for (int j = 0; j < VCARDS; ++j)
		{
			cards[i][j] = CardSprite::createCard(0, 90, Vec2(90 * i + CARDBORDER / 2, 90 * j + 80));
			addChild(cards[i][j]);
		}
	}
	win = false;
	cardsNum = 0;
	score = 0;
	RandomCard();
	RandomCard();

	return true;
}

bool GameLayer::touchBegan(cocos2d::Touch* touch, cocos2d::Event*)
{
	CCLOG("began");
	beginPos = touch->getLocation();//GL
	bNeedCheck = true;
	return true;
}
void GameLayer::touchMoved(cocos2d::Touch* touch, cocos2d::Event*)
{
	auto currentPos = touch->getLocation();
	auto xoffset = currentPos.x - beginPos.x;
	auto yoffset = currentPos.y - beginPos.y;
	if (bNeedCheck)
	{
		if (xoffset > XOFFSET && abs(yoffset) <= YOFFSET)
		{
			if (moveRight())
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("1.mp3");
				RandomCard();
				bNeedCheck = false;
			}
		}
		else if (xoffset < -XOFFSET && abs(yoffset) <= YOFFSET)
		{
			if(moveLeft())
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("1.mp3");
				RandomCard();
				bNeedCheck = false;
			}
		}
		else if (yoffset > YOFFSET && abs(xoffset) <= XOFFSET)
		{
			if(moveUp())
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("1.mp3");
				RandomCard();
				bNeedCheck = false;
			}
		}
		else if (yoffset < -YOFFSET && abs(XOFFSET) <= XOFFSET)
		{
			if(moveDown())
			{
				CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("1.mp3");
				RandomCard();
				bNeedCheck = false;
			}
		}
		scoreBoard->setScore(score);
	}	
}
void GameLayer::touchEnded(cocos2d::Touch*, cocos2d::Event*)
{
	CCLOG("end");
	beginPos = Vec2::ZERO;
	bNeedCheck = false;
}

bool GameLayer::moveLeft()
{
	bool hasMove = false;
	CCLOG("Left");
	for (int i = 0; i < VCARDS; ++i)
	{
		for (int j = 0; j < HCARDS; ++j)
		{
			for (int k = j + 1; k < HCARDS; ++k)
			{
				if (cards[k][i]->getNumber() > 0)
				{
					if (cards[j][i]->getNumber() <= 0)
					{
						cards[j][i]->setNumber(cards[k][i]->getNumber());
						cards[k][i]->setNumber(0);
						--j;
						hasMove = true;
					}
					else if (cards[j][i]->getNumber() == cards[k][i]->getNumber())
					{
						cards[j][i]->setNumber(cards[k][i]->getNumber()*2);
						cards[k][i]->setNumber(0);
						score += cards[j][i]->getNumber();
						--cardsNum;
						hasMove = true;
						if (cards[j][i]->getNumber() == 2048)
							win = true;
					}
					break;//退出 for (int k = j + 1; k < HCARDS; ++k)
				}
			}
		}
	}
	return hasMove;
}
bool GameLayer::moveRight()
{
	bool hasMove = false;
	CCLOG("Right");
	for (int i = 0; i < VCARDS; ++i)
	{
		for (int j = HCARDS-1; j >=0; --j)
		{
			for (int k = j - 1; k >=0; --k)
			{
				if (cards[k][i]->getNumber() > 0)
				{
					if (cards[j][i]->getNumber() <= 0)
					{
						cards[j][i]->setNumber(cards[k][i]->getNumber());
						cards[k][i]->setNumber(0);
						++j;
						hasMove = true;
					}
					else if (cards[j][i]->getNumber() == cards[k][i]->getNumber())
					{
						cards[j][i]->setNumber(cards[k][i]->getNumber() * 2);
						cards[k][i]->setNumber(0);
						score += cards[j][i]->getNumber();
						--cardsNum;
						hasMove = true;
						if (cards[j][i]->getNumber() == 2048)
							win = true;
					}
					break;//退出 for (int k = j + 1; k < HCARDS; ++k)
				}
			}
		}
	}
	return hasMove;
}
bool GameLayer::moveUp()
{
	bool hasMove = false;
	CCLOG("Up");
	for (int i = 0; i < HCARDS; ++i)
	{
		for (int j = VCARDS-1; j >= 0; --j)
		{
			for (int k = j - 1; k >= 0; --k)
			{
				if (cards[i][k]->getNumber()>0)
				{
					if (cards[i][j]->getNumber() <= 0)
					{
						cards[i][j]->setNumber(cards[i][k]->getNumber());
						cards[i][k]->setNumber(0);
						++j;
						hasMove = true;
					}
					else if (cards[i][j]->getNumber() == cards[i][k]->getNumber())
					{
						cards[i][j]->setNumber(cards[i][j]->getNumber() * 2);
						cards[i][k]->setNumber(0);
						score += cards[i][j]->getNumber();
						--cardsNum;
						hasMove = true;
						if (cards[i][j]->getNumber() == 2048)
							win = true;
					}
					break;//退出 for (int k = j - 1; k >= 0; --k)
				}
			}
		}
	}
	return hasMove;
}
bool GameLayer::moveDown()
{
	bool hasMove = false;
	CCLOG("Down");
	for (int i = 0; i < HCARDS; ++i)
	{
		for (int j = 0; j < VCARDS; ++j)
		{
			for (int k = j + 1; k < VCARDS; ++k)
			{
				if (cards[i][k]->getNumber()>0)
				{
					if (cards[i][j]->getNumber() <= 0)
					{
						cards[i][j]->setNumber(cards[i][k]->getNumber());
						cards[i][k]->setNumber(0);
						--j;
						hasMove = true;
					}
					else if (cards[i][j]->getNumber() == cards[i][k]->getNumber())
					{
						cards[i][j]->setNumber(cards[i][j]->getNumber() * 2);
						cards[i][k]->setNumber(0);
						score += cards[i][j]->getNumber();
						--cardsNum;
						hasMove = true;
						if (cards[i][j]->getNumber() == 2048)
							win = true;
					}
					break;//退出 for (int k = j - 1; k >= 0; --k)
				}
			}
		}
	}
	return hasMove;
}

bool GameLayer::RandomCard()
{
	if (cardsNum >= HCARDS*VCARDS)
		return false;
	srand(time(NULL));
	int i = -1;
	int j = -1;
	do
	{
		i = rand() % 4;
		j = rand() % 4;
		if (cards[i][j]->getNumber() == 0)
		{
			cards[i][j]->setNumber(2);
			break;
		}
	} while (true);
	++cardsNum;
	return true;
}
#include "CardSprite.h"

USING_NS_CC;

bool CardSprite::init()
{
	if (!Sprite::init())
		return false;
	return true;
}

CardSprite* CardSprite::createCard(int number, int size, Vec2 pos)
{
	auto card = new CardSprite();
	if (card && card->init())
	{
		card->autorelease();	
		card->initCard(number, size, pos);
	}
	else
	{
		delete card;
		card = NULL;
	}
	return card;
}

bool CardSprite::initCard(int number, int size, cocos2d::Vec2 pos)
{
	this->number = number;
	bg = LayerColor::create(Color4B(216, 210, 214, 255), size - CARDBORDER, size - CARDBORDER);
	bg->setAnchorPoint(Vec2(0, 0));
	bg->setPosition(pos);
	if (number > 0)
	{
		label = Label::create(String::createWithFormat("%d", number)->getCString(),"Arial",36);
	}
	else
	{
		label = Label::create("", "Arial", 36);
	}
	label->setPosition(Vec2(size / 2 - CARDBORDER, size / 2 - CARDBORDER));
	bg->addChild(label);
	addChild(bg);

	return true;
}

int CardSprite::getNumber()
{
	return number;
}
bool CardSprite::setNumber(int num)
{
	number = num;
	if (number <= 0)
	{
		label->setString("");
		bg->setColor(Color3B(216, 210, 214));
	}
	else
	{
		label->setString(String::createWithFormat("%d", number)->getCString());
		if (number == 2)
		{
			bg->setColor(Color3B(255, 246, 247));
			label->setColor(Color3B(33, 33, 7));
		}
		else if (number == 4)
		{
			bg->setColor(Color3B(255, 218, 163));
			label->setColor(Color3B(255, 255, 255));
		}
		else if (number == 8)
		{
			bg->setColor(Color3B(255, 144, 91));
		}
		else if (number == 16)
		{
			bg->setColor(Color3B(255, 101, 46));
		}
		else if (number == 32)
		{
			bg->setColor(Color3B(255, 80, 46));
		}
		else if (number == 64)
		{
			bg->setColor(Color3B(254, 56, 60));
		}
		else if (number == 128)
		{
			bg->setColor(Color3B(6, 194, 240));
		}
		else if (number == 256)
		{
			bg->setColor(Color3B(5, 140, 240));
		}
		else if (number == 512)
		{
			bg->setColor(Color3B(4, 140, 200));
		}
		else if (number == 1024)
		{
			bg->setColor(Color3B(3, 112, 154));
		}
		else if (number == 2048)
		{
			bg->setColor(Color3B(2, 98, 131));
		}
	}
	return true;
}
#include "ScoreBoard.h"

USING_NS_CC;

bool ScoreBoard::init()
{
	if (!Sprite::init())
		return false;
	return true;
}

ScoreBoard* ScoreBoard::createBoard(int score, int width, int high, char* title, cocos2d::Vec2 pos, cocos2d::Color4B color)
{
	auto board = new ScoreBoard();
	if (board && board->init())
	{
		board->autorelease();
		board->initBoard(score, width, high, title, pos,color);
	}
	else
	{
		delete board;
		board = NULL;
	}
	return board;
}
bool ScoreBoard::initBoard(int score, int width, int high, char* title, cocos2d::Vec2 pos, cocos2d::Color4B color)
{
	this->score = score;
	bg = LayerColor::create(color, width, high);
	bg->setAnchorPoint(Vec2(0, 0));
	bg->setPosition(pos);
	addChild(bg);
	this->title = Label::create(title, "Arial", 30);
	this->title->setPosition(width / 2, high *3 / 4);
	bg->addChild(this->title);
	label = Label::create(String::createWithFormat("%d", score)->getCString(), "Arial", 40);
	label->setPosition(width / 2, high / 4);
	bg->addChild(label);

	return true;
}

int ScoreBoard::getScore()
{
	return score;
}
bool ScoreBoard::setScore(int score)
{
	this->score = score;
	label->setString(String::createWithFormat("%d", score)->getCString());
	if (score <= 1000 && score > 100)
		label->setSystemFontSize(30);
	else if (score > 1000)
		label->setSystemFontSize(20);
	return true;
}

代码下载

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

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏吴小龙同學

Android小知识库

这份是我工作以来,总结的小知识库,有些知识点现在看来太LOW了,把还觉得有用的分享出来! Android 6.0 Dialog text doesn’t ...

37613
来自专栏向治洪

android获取状态栏高度

公司项目中使用了popWindow,并且需求指明了popWindow必须是和Activity类似的全屏模式,并且从页面底部弹出! 当popWindow所有设...

2366
来自专栏肖蕾的博客

Android自定义控件之九宫格

1135
来自专栏Android知识点总结

1-VIII--ViewPager的基本使用

903
来自专栏项勇

[Android笔记7]之通过DatePickerDialog,TimePickerDialog调用系统时间设置

2043
来自专栏编程之路

羊皮书APP(Android版)开发系列(十五)Android 循环滚动,自动轮播广告展示

1335
来自专栏向治洪

高仿今日头条(1)

本片博客主要实现今日头条的主题框架。后面我会慢慢完善,欢迎大家持续关注 git下载地址:https://github.com/xiangzhihong/topN...

2847
来自专栏向治洪

仿qq最新侧滑菜单

为了后续对这个项目进行优化,比如透明度动画、背景图的位移动画,以及性能上的优化。 我把这个项目上传到github上面,请大家随时关注。 github地址 htt...

3449
来自专栏吴小龙同學

googlesamples之easypermissions使用

使用 1、 app/build.gradle 1 2 3dependencies { compile 'pub.devrel:easypermissi...

3176
来自专栏转载gongluck的CSDN博客

cocos2dx 象棋

/******************************************************************* * Copyrigh...

2819

扫码关注云+社区