cocos2dx 消灭星星
时间:2022-05-03
本文章向大家介绍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);
}
- 华为专家 | 轻量化微服务测试实践
- Android Material Design系列之Navigation Drawer
- [原创]Fluent NHibernate之旅(四)-- 关系(下)
- 一次通过漏洞挖掘成功渗透某网站的过程
- 使用fuzzDB进行web安全测试
- Android Material Design系列之FloatingActionButton和Snackbar
- Fluent Nhibernate之旅(五)--利用AutoMapping进行简单开发
- Android Material Design系列之Toolbar
- Struts2 S2-020在Tomcat 8下的命令执行分析
- Struts2再曝S2-020补丁绕过漏洞 – 万恶的正则表达式
- 学习BlogEngine.Net解读笔记系列(一)
- Android面试系列之应用内多语言切换
- Android面试系列之AsyncTask
- Kali-Linux扩充弹药:Kali Linux metapackages
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法