背景:
上一节,我们已经把消灭星星的界面搭建好了,流程也跑通了。 这一篇涉及到程序的算法,也许是最难的部分了,理解起来需要多花点时间,而且我提供的算法未必就是最好的,如果读者有更优更好的算法,希望分享出来,我可以链接到你那里。大概的思路是这样的,第一次点击一个星星,立刻寻找四周相同颜色的,第二次点击,消除他们并产生粒子效果;接着星星数组重新走位掉落,补全空缺;然后还要检测纵行是否出现空缺,有的话,合并到一起;最后必须检测死局;大概如此。
ps:
1 这是一个系列博文,代码不会一下子全部放出来,每写一篇放出相应的代码。因为笔者也是抽空编一点程序,然后写一篇博文,断断续续的,没有整块时间;
2 代码是基于javascript语言,cocos2d-x游戏引擎,cocos2d-x editor手游开发工具完成的;
3 运行demo需要配置好cocos2d-x editor,暂不支持其他工具。demo是跨平台的,可移植运行android,ios,html5网页等。
源代码下载:
请到代码集中营下载(第二篇算法):http://blog.makeapp.co/?p=319
不同平台下的效果图:(windows、html5、android)
代码分析:
主要集中在MainLayer.js下面的分析
第一步,10*10星星群检测触摸事件,通过this.sameColorList.length可以判断是第一次触摸还是第二次触摸 ;
@@ >1表示第二次触摸,这里又有分支,触摸的是刚才同一颜色区域还是其他区域?如果是原来颜色区域,删除this.removeSameColorStars(),如果不是原来颜色区域,恢复原状,然后新的检测
@@ <=1表示第一次触摸 直接检测颜色相同区域
- MainLayer.prototype.onTouchesBegan = function (touches, event) {
- var loc = touches[0].getLocation();
- this.ccTouchBeganPos = loc;
- for (var i = 0; i < this.starTable.length; i++) {
- var sprites = this.starTable[i];
- for (var j = 0; j < sprites.length; j++) {
- var pSprite0 = sprites[j];
- if (pSprite0) {
- var ccRect = pSprite0.getBoundingBox();
- if (isInRect(ccRect, this.ccTouchBeganPos)) {
- if (this.sameColorList.length > 1) {
- if (this.sameColorList.contains(pSprite0)) {
- cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.broken, false);
- this.removeSameColorStars();
- } else {
- for (var k = 0; k < this.sameColorList.length; k++) {
- if (this.sameColorList[k]) {
- this.sameColorList[k].runAction(cc.ScaleTo.create(0.1, 1));
- }
- }
- this.checkSameColorStars(pSprite0);
- if (this.sameColorList.length > 1) {
- cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.select, false);
- }
- }
- } else {
- this.checkSameColorStars(pSprite0);
- if (this.sameColorList.length > 1) {
- cc.AudioEngine.getInstance().playEffect(PS_MAIN_SOUNDS.select, false);
- }
- }
- break;
- }
- }
- }
- }
- };
第二步,建立单个星星的四个方向检测,上下左右,把颜色相同的放在一个数组里面,回调这个数组;其实最后用这个函数的时候主要是判断数组的大小;数组大于1,说明四周有相同颜色的;
- MainLayer.prototype.checkOneStarFourSide = function (sprite) {
- if (sprite == null) {
- return;
- }
- // cc.log("checkOneStarFourSide");
- var fourSideSpriteList = [];
- var color = sprite.starData.color;
- var col = sprite.starData.indexOfColumn;
- var row = sprite.starData.indexOfRow;
- //up
- if (row < 9) {
- var upSprite = this.starTable[col][row + 1];
- if (upSprite != null && upSprite.starData.color == color) {
- fourSideSpriteList.push(upSprite);
- }
- }
- //down
- if (row > 0) {
- var downSprite = this.starTable[col][row - 1];
- if (downSprite != null && downSprite.starData.color == color) {
- fourSideSpriteList.push(downSprite);
- }
- }
- //left
- if (col > 0) {
- var leftSprite = this.starTable[col - 1][row];
- if (leftSprite != null && leftSprite.starData.color == color) {
- fourSideSpriteList.push(leftSprite);
- }
- }
- //right
- if (col < 9) {
- var rightSprite = this.starTable[col + 1][row];
- if (rightSprite != null && rightSprite.starData.color == color) {
- fourSideSpriteList.push(rightSprite);
- }
- }
- return fourSideSpriteList;
- }
第三步,检测相同颜色区域,这里的算法比较复杂;有两个数组this.sameColorList和newSameColorList,前者是全局星星数组,后者是每次扩展新加入的星星;比如这样情况,一个星星左右上有相同的星星,上面的上面还有一个星星,总共五个相同星星:三次检测情况是this.sameColorList为1---4----5 ,而newSameColorList为1--3--1,各种曲折,读者好好理解下;
- MainLayer.prototype.checkSameColorStars = function (sprite) {
- if (sprite == null) {
- return;
- }
- this.sameColorList = [];
- this.sameColorList.push(sprite);
- var newSameColorList = [];
- newSameColorList.push(sprite);
- //by logic ,check the same color star list
- while (newSameColorList.length > 0) {
- for (var i = 0; i < newSameColorList.length; i++) {
- var fourSide = this.checkOneStarFourSide(newSameColorList[i]);
- if (fourSide.length > 0) {
- for (var j = 0; j < fourSide.length; j++) {
- if (!this.sameColorList.contains(fourSide[j])) {
- this.sameColorList.push(fourSide[j]);
- newSameColorList.push(fourSide[j]);
- }
- }
- }
- newSameColorList.splice(i, 1);
- }
- }
- cc.log("sameColorList length==" + this.sameColorList.length);
- if (this.sameColorList.length > 1) {
- for (var k = 0; k < this.sameColorList.length; k++) {
- var simpleStar = this.sameColorList[k];
- if (simpleStar) {
- simpleStar.runAction(cc.ScaleTo.create(0.1, 1.08));
- }
- }
- }
- }
第四步 移除相同的星星,并产生粒子效果
- MainLayer.prototype.removeSameColorStars = function () {
- for (var k = 0; k < this.sameColorList.length; k++) {
- var simpleStar = this.sameColorList[k];
- if (simpleStar) {
- var col = simpleStar.starData.indexOfColumn;
- var row = simpleStar.starData.indexOfRow;
- this.starTable[col].splice(row, 1, null);
- this.rootNode.removeChild(simpleStar);
- if (sys.platform != 'browser') {
- var starParticle = cc.StarParticle.create(this.rootNode, (36 + col * this.starSize), (36 + row * this.starSize), "spark");
- starParticle.runAction(cc.Sequence.create(cc.DelayTime.create(0.8), cc.CleanUp.create(starParticle)));
- }
- }
- }
- this.sameColorList = [];
- this.fallStar();
- }
第五步 星星掉落 填充空缺,主要是如果一个地方有空缺,就把它上面的星星位置和数据交换,用到数组的方法splice,可到网上查看js数组的一些方法应用
- MainLayer.prototype.fallStar = function () {
- for (var i = 0; i < this.starTable.length; i++) {
- var sprites = this.starTable[i];
- var length = sprites.length;
- for (var j = 0; j < length; j++) {
- var pSprite0 = sprites[j];
- if (pSprite0 == null) {
- var k = j + 1;
- while (k < length) {
- var upSprite = sprites[k];
- if (upSprite != null) {
- upSprite.starData.indexOfColumn = i;
- upSprite.starData.indexOfRow = j;
- this.starTable[i].splice(j, 1, upSprite);
- this.starTable[i].splice(k, 1, null);
- k = length;
- var flowTime = 0.2;
- var fallAction = cc.MoveTo.create(flowTime, cc.p(36 + i * this.starSize,
- 36 + j * this.starSize));
- upSprite.runAction(fallAction);
- }
- k++;
- }
- }
- }
- }
- this.deadStar();
- // this.combineStar();
- }
第六步 合并星星,如果最底部有空缺,星星必须向左合并,这里笔者调试有问题,时间匆忙 来不及修改,读者可以自行研究修改;不解释了
- MainLayer.prototype.combineStar = function () {
- for (var m = 0; m < this.starTable.length; m++) {
- var mSprite0 = this.starTable[m][0];
- if (mSprite0 == null) {
- if (m == (this.starTable.length - 1)) {
- for (var j = 0; j < this.starTable[m].length; j++) {
- this.starTable[m].splice(j, 1, null);
- }
- }
- else {
- for (var i = (m + 1); i < this.starTable.length; i++) {
- // this.starTable.splice((i - 1), 1, this.starTable[i]);
- for (var j = 0; j < this.starTable[i].length; j++) {
- var pSprite0 = this.starTable[i][j];
- this.starTable[i - 1].splice(j, 1, pSprite0);
- if (pSprite0 != null) {
- pSprite0.starData.indexOfColumn = (i - 1);
- var col = pSprite0.starData.indexOfColumn;
- var row = pSprite0.starData.indexOfRow;
- var moveAction = cc.MoveTo.create(0.1, cc.p(36 + col * this.starSize,
- 36 + row * this.starSize));
- pSprite0.runAction(moveAction);
- }
- }
- }
- }
- }
- }
- this.deadStar();
- }
第七步 游戏到最后 会发生死局情况,程序自动判断消除;这里主要是循环检测每一个星星,如果所有的星星四周都没有相同星星的时候,就确认为死局,程序自动消除星星
- MainLayer.prototype.deadStar = function () {
- var isDead = true;
- for (var i = 0; i < this.starTable.length; i++) {
- var sprites = this.starTable[i];
- var length = sprites.length;
- for (var j = 0; j < length; j++) {
- var pSprite0 = sprites[j];
- if (pSprite0 != null) {
- if (this.checkOneStarFourSide(pSprite0).length > 0) {
- isDead = false;
- return;
- }
- }
- }
- }
- if (isDead) {
- for (var jj = 9; jj >= 0; jj--) {
- for (var ii = 0; ii < 10; ii++) {
- var pSprite0 = this.starTable[ii][jj];
- if (pSprite0 != null) {
- var delay = 4 + 0.3 * ii - 0.4 * jj;
- pSprite0.runAction(cc.Sequence.create(
- cc.DelayTime.create(delay),
- cc.CleanUp.create(pSprite0)
- ));
- var starParticle = cc.StarParticle.create(this.rootNode, (36 + ii * this.starSize), (36 + jj * this.starSize), "spark");
- starParticle.runAction(cc.Sequence.create(cc.ScaleTo.create(0, 0),
- cc.DelayTime.create(delay), cc.ScaleTo.create(0, 1), cc.DelayTime.create(0.8),
- cc.CleanUp.create(starParticle)));
- }
- }
- }
- }
- }
基本的流程就是这样 触摸——检测颜色——消除星星——掉落移动——合并星星——检测死局——结束 消除类的游戏思路都差不多是这样,把这个demo理解透了 任何消除类的游戏都很简单
cocos2d-x editor开发工具:
cocos2d-x editor,它是开发跨平台的手机游戏工具,运行window/mac系统上,javascript脚本语言,基于cocos2d-x跨平台游戏引擎, 集合代码编辑,场景设计,动画制作,字体设计,还有粒子,物理系统,地图等等的,而且调试方便,和实时模拟;
cocos2d-x editor 下载,介绍和教程:http://blog.csdn.net/touchsnow/article/details/19070665;
cocos2d-x editor官方博客:http://blog.makeapp.co/;
相关推荐
源代码使用AndEngine框架以Java编程语言编写. 完整,运作良好
pc版消灭星星源码,基于vb.net 设计
Cocos2d x 消灭星星源码 PopStar @更多@ http: cleopard download csdn net 13份WPF经典开发教程 http: download csdn net album detail 1115 C#资料合辑二[C#桌面编程入门篇] ...C#资料合辑一[C#入门篇] ...
消灭星星是一款手机上非常流行的消除类娱乐游戏,此次有了电脑版(非模拟器版),让大家可以在电脑上过把瘾了。游戏结束后,如果将自己的得分提交的网络上,可以在下次开局时得到额外的刷子和炸弹,消除更刺激。
PopStar(一起消灭星星)_210.rar小游戏源码CocosCreator源码下载料下载PopStar(一起消灭星星)_210.rar小游戏源码CocosCreator源码下载料下载PopStar(一起消灭星星)_210.rar小游戏源码CocosCreator源码下载料下载Pop...
2、关卡结束时剩余方块越少获得分数越高。3、炸弹功能:消除指定的一个方块。4、刷子功能:将四周方块的颜色转变为指定方块的颜色。5、每次游戏结束后,如果将自己的得分提交到网络上,可以在开局时得到额外的...
Qt 4.7的工程 和 android 手机 上消灭星星原理一样。QT 代码简短 易懂。
PopStar(一起消灭星星)_210.zip源码cocos creator游戏项目源码下载PopStar(一起消灭星星)_210.zip源码cocos creator游戏项目源码下载 1.适合个人学习技术做项目参考 2.适合学生毕业设计项目参考 3.适合小公司开发...
这个是自己业余时间用MFC写的一个模仿手机游戏PopStar的小程序,只有一些简单的功能
使用cocos2d-x3.2引擎开发的消灭星星源码demo,内容仅供参考和学习
iOS游戏应用源代码——andresbonilla-Balloon-Pop-Fever.zip
cocos2dx 消灭星星源码-popStar cocos精品小游戏源码 , 完整项目源码 是整个项目的压缩包 , 新建空项目直接导入到项目里面就可以运行了,无需其他操作。 适合二次开发和学习使用,都是经典游戏,需要其他游戏源码包...
cocos2d-x实现一个PopStar(消灭星星)游戏的逻辑分析及源码-附件资源
源代码包括二叉树的创建,先序遍历,中序遍历,后序遍历 crc 校验, 经典算法的加密和解密算法,huffman编码 队列的创建、插入、删除,堆栈的创建、pop和push操作, 图:----用邻接矩阵构造无向图-------- -------...
游戏解法程序,程序自动辨识iphone5/5s 等retina分辨率的PopStar游戏截图,将PNG格式的截图替换压缩包内的test.png即可,运行主程序popstar_Solver.m或popstar_SolverV2.m,出现图像后黑色方块代表要消除的,按任意...
PopStarCocos2d-x v2.2 PopStar Game#Cocos2d-x 开发《消灭星星》游戏用Cocos2d-x开发跨平台游戏,高仿《消灭星星》游戏。——和传统的消除类游戏不同,本游戏没有时间限制,只要两个相同颜色的星星就可以消除,以...
TestPopStar Test PopStar消灭星星
51CTO下载-运用C#实现POP3邮件接收程序源代码
cocoseditor-popstar消灭星星游戏源代码 开发工具:cocos2dx editor,它是开发跨平台的手机游戏工具,运行window系统上,javascript脚本语言,基于cocos2d-x跨平台游戏引擎, 集合代码编辑,场景设计,动画制作,...