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

相关文章

来自专栏linux驱动个人学习

高通msm8909耳机调试

1、DTS相应修改: DTS相关代码:kernel/arch/arm/boot/dts/qcom/msm8909-qrd-skuc.dtsi: 1 s...

9735
来自专栏叁金大数据

EmguCV学习——视频与图片互转

其实视频转图片在上篇文章中已经有些眉目了,其实就是按帧读取视频,然后把帧保存就ok。然后自己再加个进度条美化一下。。。这代码简单易懂,还是直接上代码吧。

1931
来自专栏叁金大数据

EmguCV学习——简单算法 差分与高斯

公司项目需要检测运动物体,我对opencv也没啥研究,google了好久看了好多方法,最简单的就是差分与高斯背景建模了。

1463
来自专栏一个会写诗的程序员的博客

ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor as super class

ClassMetadataReadingVisitor has interface org.springframework.asm.ClassVisitor a...

1055
来自专栏一个会写诗的程序员的博客

查看域名服务器命令: $ dig -t NS xvideos.com

; <<>> DiG 9.8.3-P1 <<>> -t NS xvideos.com ;; global options: +cmd ;; Got answ...

3K2
来自专栏游戏杂谈

CURLcode的定义

http://curl.haxx.se/libcurl/c/libcurl-errors.html

2642
来自专栏GIS讲堂

openlayers2渐变色渲染

在前文中,讲到了oL2中唯一值渲染的实现方式,在本文讲述ol2中渐变色渲染的实现方式。

1452
来自专栏我的博客

Nginx配置pathInfo

server { listen 80; server_name blog.phpfs.com; ...

3577
来自专栏菩提树下的杨过

Silverlight Telerik控件学习:TreeView数据绑定并初始化选中状态、PanelBar的Accordion效果、TabPanel、Frame基本使用

实际开发中控件的数据源肯定是动态绑定的,不可能在xaml里写死item项。既然要绑定,就先来几个实体类: ? 上面是类图,各类的代码如下:  Business...

2958
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

1.2K8

扫码关注云+社区