HTML 5游戏制作之五彩连珠(寻路)

开发 前端
其实Canvas也是html的一个元素而已,所以我们可以给Canvas加click事件。来查看click时鼠标的坐标,这样就等得出点击了map的哪个位置。我们给game增加一个click方法,当Canvas点击时调用此方法。

上节主要做了动画的实现,感觉还是比较有意思的。游戏的性能好不好,重绘应该比较重要吧,菜鸟瞎想了下呵呵。

本节就要做对泡泡的操作,上节后面提到了点击泡泡后泡泡要做出闪动响应,那我们我们如何获得被点击了哪个泡泡呢?

其实Canvas也是html的一个元素而已,所以我们可以给Canvas加click事件。来查看click时鼠标的坐标,这样就等得出点击了map的哪个位置。

我们给game增加一个click方法,当Canvas点击时调用此方法。

要实现的效果是: 当Canvas时被点击时有几种可能:

1、没点到map  那就不作响应;2、点到了泡泡,那该泡泡要做出响应(闪);3、如果之前有点击过其他的泡泡,则取消之前的泡泡的响应(clicked.stop),如果之前的泡泡是自己,则不作响应。并把clicked作为自己,以体后面的操作;4、如果点击到的是空格,如果之前点击了泡泡,那就尝试移动这个泡泡,如果clicked为null(之前没泡泡)那就不作任何响应。如果可以移动,则取消闪动,并清除clicked,开始移动。 

  1. onclick: function (e) {   
  2.     var px = e.offsetX - game.map.startX;   
  3.     var py = e.offsetY - game.map.startY;   
  4.     if (px < 0 || py < 0 || px > game.map.width || py > game.map.height) {   
  5.         return;   
  6.     }   
  7.     var x = parseInt(px / game.cellWidth);   
  8.     var y = parseInt(py / game.cellWidth);   
  9.    
  10.     var bubble = game.map.getBubble(x, y);   
  11.     if (bubble.color) {   
  12.         if (this.clicked) {   
  13.             //同一个泡不做反映   
  14.             if (this.clicked.x == x && this.clicked.y == y) {   
  15.                 return;   
  16.             }   
  17.             this.clicked.stop();   
  18.         }   
  19.         this.clicked = bubble;   
  20.         bubble.play();   
  21.     }   
  22.     else {   
  23.         if (this.clicked) {   
  24.             this.clicked.stop();   
  25.             //移动clicked   
  26.             game.map.move(this.clicked, bubble);   
  27.         }   
  28.     }   
  29.     //console.log("x:" + x + " y:" + y);   
  30. }, 

寻路的代码还没写,因为这个需要考虑怎么实现。 我绞尽脑汁终于想到了一个办法。暂且撇开游戏的代码,单独实现下两点的寻路代码。
先给定一个棋盘,假如如下:

1 1 1 1 1
0 0 1 0 1
0 0 1 0 1
1 0 0 1 1

要想从 最下面一行中间的点(2,3)移动到左上角的(0,1),该如何设计呢?

一个棋子能否移动,要看他相邻的4个子是否为0,如果是0则可以移动。 所以我们可以通过递归来获得所有相连的0的记录。 这个记录用树结构来存储,直到我们无法继续探测为0的格子或到达目的地。 我们把当前的棋子的格子设为 root,他相邻的棋子是他的孩子。这样的话,我们会得到一棵树的结果如下:

是不是?这样的画我们就可以直接看到了整个路径(2,3 -> 1,3 -> 1,2 -> 0,2 -> 0,1)。思路很清晰,只要递归构建子节点就ok了。代码如下:

  1. var map = [   
  2. [1, 1, 1, 1, 1],   
  3. [0, 0, 1, 0, 1],   
  4. [0, 0, 1, 0, 1],   
  5. [1, 0, 0, 1, 1]   
  6.     ];   
  7.     
  8.     var history = [];   
  9.     var goal = { "x": 0, "y": 1 }   
  10.     var goalNode = null;   
  11.     var getNode = function (x, y, parent) {   
  12.         if (x >= map.length || y >= map.length) {   
  13.             return;   
  14.         }   
  15.     
  16.         if (map[y][x] == 1) {   
  17.             return;   
  18.         }   
  19.     
  20.         var hasNode = false;   
  21.         history.forEach(function (n) {   
  22.             if (n.x == x && n.y == y) {   
  23.                 hasNode = true;   
  24.                 return;   
  25.             }   
  26.         });   
  27.     
  28.         if (hasNode) {   
  29.             return;   
  30.         }   
  31.         var node = { "x": x, "y": y, "parent": parent, child: [] };   
  32.         history.push(node);   
  33.     
  34.         if (node.x == goal.x && node.y == goal.y) {   
  35.             goalNode = node;   
  36.             return node;   
  37.         }   
  38.         if (x - 1 >= 0 && !map[y][x - 1]) {   
  39.             node.child.push(getNode(x - 1, y, node));   
  40.         }   
  41.         if (y - 1 >= 0 && !map[y - 1][x]) {   
  42.             node.child.push(getNode(x, y - 1, node));   
  43.         }   
  44.         if (x + 1 < map.length && !map[y][x + 1]) {   
  45.             node.child.push(getNode(x + 1, y), node);   
  46.         }   
  47.         if (y + 1 < map.length && !map[y + 1][x]) {   
  48.             node.child.push(getNode(x, y + 1, node));   
  49.         }   
  50.         return node;   
  51.     }   
  52.     console.log(getNode(2, 3));   
  53.     console.log(goalNode); 

我加了一个parent,就是指向父亲的指针,那样就不用再去遍历这棵树了。可以直接从goalNode的结果得到整个路径:) 虽然偷懒,但也是要复出代价的,因为这样走的路径不是最短路线,比较傻,怎么选择最优路线呢? 最笨的方法就是 把所有的路径都得到(深度优先遍历树N遍- -)然后比较。这个显然效率不高。开始我也不知道效果会这么差,等一运行(你运行下就知道了),我发现,是代码写的不好(废话)。因为我们每次的判断顺寻都是 左上右下,这样路径总是这个方向探索,而最优的路径应该是朝目标点的方向探索。 由于是递归查找,所以,对当前的node和目标node进行坐标的方向判断,然后调整判断顺序,这样是得到的才是比较短的路径。

  1. var child = [];   
  2. var left, top, right, buttom;   
  3. //最短路径的粗略判断就是首选目标位置的大致方向   
  4. if (x - 1 >= 0 && map.isEmpty(x - 1, y))   
  5.     left = { "x": x - 1, "y": y };   
  6. if (x + 1 < map.length && map.isEmpty(x + 1, y))   
  7.     right = { "x": x + 1, "y": y };   
  8. if (y + 1 < map.length && map.isEmpty(x, y + 1))   
  9.     buttom = { "x": x, "y": y + 1 };   
  10. if (y - 1 >= 0 && map.isEmpty(x, y - 1))   
  11.     top = { "x": x, "y": y - 1 };   
  12.     
  13. if (x > x2) {   
  14.     if (y > y2)   
  15.         child = [left, top, right, buttom];   
  16.     else if (y < y2)   
  17.         child = [left, buttom, right, top];   
  18.     else 
  19.         child = [left, top, right, buttom];   
  20. }   
  21. else if (x < x2) {   
  22.     if (y > y2)   
  23.         child = [right, top, left, buttom];   
  24.     else if (y < y2)   
  25.         child = [right, buttom, left, top];   
  26.     else 
  27.         child = [right, top, left, buttom];   
  28. }   
  29. else if (x == x2) {   
  30.     if (y > y2)   
  31.         child = [top, left, right, buttom];   
  32.     else if (y < y2)   
  33.         child = [buttom, left, right, top];   
  34. }   
  35.     
  36. for (var i = 0; i < child.length; i++) {   
  37.     var c = child[i];   
  38.     if (c) node.child.push(getnode(c.x, c.y, node));   

代码虽然写的比较傻,但这种方式不得不说好就一个字:)

既然寻路已经实现了,那么下面就交给map了,map来负责让泡泡走起来。其实就是根据路径给泡泡着色- - ,代码也不复杂。

  1. move: function (bubble, target) {   
  2.     var path = this.search(bubble.x, bubble.y, target.x, target.y);   
  3.     if (!path) {   
  4.         //显示不能移动s   
  5.         alert("过不去");   
  6.         return;   
  7.     }   
  8.     //map开始播放当前泡的移动效果   
  9.     //两种实现方式,1、map按路径染色,最后达到目的地 2、map生成一个临时的bubble负责展示,到目的地后移除   
  10.     //console.log(path);   
  11.     var me = this;   
  12.     var name = "move_" + bubble.x + "_" + bubble.y;   
  13.     var i = path.length - 1;   
  14.     var color = bubble.color;   
  15.     game.play(name, function () {   
  16.         if (i < 0) {   
  17.             game.stop(name);   
  18.             return;   
  19.         }   
  20.         path.forEach(function (cell) {   
  21.             me.setBubble(cell.x, cell.y, null);   
  22.         });   
  23.         var currentCell = path[i];   
  24.         me.setBubble(currentCell.x, currentCell.y, color);   
  25.         i--;   
  26.    
  27.     }, 50);   
  28. },   
  29. search: function (x1, y1, x2, y2) {   
  30.    
  31.     var history = [];   
  32.     var goalCell = null;   
  33.     var me = this;   
  34.    
  35.     getCell(x1, y1, null);   
  36.     if (goalCell) {   
  37.         var path = [];   
  38.    
  39.         var cell = goalCell;   
  40.         while (cell) {   
  41.             path.push({ "x": cell.x, "y": cell.y });   
  42.             cell = cell.parent;   
  43.         }   
  44.    
  45.         return path;   
  46.     }   
  47.     return null;   
  48.    
  49.     function getCell(x, y, parent) {   
  50.         if (x >= me.bubbles.length || y >= me.bubbles.length)   
  51.             return;   
  52.         if (x != x1 && y != y2 && !me.isEmpty(x, y))   
  53.             return;   
  54.    
  55.         for (var i = 0; i < history.length; i++) {   
  56.             if (history[i].x == x && history[i].y == y)   
  57.                 return;   
  58.         }   
  59.    
  60.         var cell = { "x": x, "y": y, child: [], "parent": parent };   
  61.         history.push(cell);   
  62.    
  63.         if (cell.x == x2 && cell.y == y2) {   
  64.             goalCell = cell;   
  65.             return cell;   
  66.         }   
  67.    
  68.         var child = [];   
  69.         var left, top, right, buttom;   
  70.         //最短路径的粗略判断就是首选目标位置的大致方向   
  71.         if (x - 1 >= 0 && me.isEmpty(x - 1, y))   
  72.             left = { "x": x - 1, "y": y };   
  73.         if (x + 1 < me.bubbles.length && me.isEmpty(x + 1, y))   
  74.             right = { "x": x + 1, "y": y };   
  75.         if (y + 1 < me.bubbles.length && me.isEmpty(x, y + 1))   
  76.             buttom = { "x": x, "y": y + 1 };   
  77.         if (y - 1 >= 0 && me.isEmpty(x, y - 1))   
  78.             top = { "x": x, "y": y - 1 };   
  79.    
  80.         if (x > x2) {   
  81.             if (y > y2)   
  82.                 child = [left, top, right, buttom];   
  83.             else if (y < y2)   
  84.                 child = [left, buttom, right, top];   
  85.             else 
  86.                 child = [left, top, right, buttom];   
  87.         }   
  88.         else if (x < x2) {   
  89.             if (y > y2)   
  90.                 child = [right, top, left, buttom];   
  91.             else if (y < y2)   
  92.                 child = [right, buttom, left, top];   
  93.             else 
  94.                 child = [right, top, left, buttom];   
  95.         }   
  96.         else if (x == x2) {   
  97.             if (y > y2)   
  98.                 child = [top, left, right, buttom];   
  99.             else if (y < y2)   
  100.                 child = [buttom, left, right, top];   
  101.         }   
  102.    
  103.         for (var i = 0; i < child.length; i++) {   
  104.             var c = child[i];   
  105.             if (c) cell.child.push(getCell(c.x, c.y, cell));   
  106.         }   
  107.         return cell;   
  108.     }   
  109. }, 

试玩地址:http://zhengliangjun.sinaapp.com/colorline.html

后面剩下的就是判断如何消除、加分、防止误操作之类的内容了。

原文链接:http://www.cnblogs.com/mad/archive/2012/03/18/2404660.html

【编辑推荐】

  1. HTML 5游戏制作之五彩连珠(预览)
  2. HTML 5游戏制作之五彩连珠(画图)
  3. HTML 5游戏制作之五彩连珠(设计)
  4. HTML 5游戏制作之五彩连珠(动画)
  5. HTML 5游戏制作之五彩连珠(试玩)
责任编辑:张伟 来源: 君之蘭的博客
相关推荐

2012-05-17 13:45:35

HTML5

2012-05-17 14:45:34

HTML5

2012-05-18 13:11:09

HTML5

2012-05-18 13:07:04

HTML5

2012-05-18 14:05:53

HTML5

2010-08-12 22:35:24

IBM培训

2011-11-30 15:14:32

HTML 5

2017-07-26 15:59:51

寻路算法Dijkstra游戏

2019-09-11 15:20:21

华为

2012-06-07 15:29:31

HTML5

2021-03-26 07:06:40

Windows 10Windows操作系统

2012-05-15 13:57:41

HTML5

2012-01-10 16:37:46

乐团

2012-03-29 09:18:47

HTML5WEB

2019-09-12 10:10:10

Vim编辑器代码

2020-04-22 10:01:26

Vim编辑器代码

2012-08-13 14:17:35

算法代码

2013-08-27 14:20:09

游戏应用图标ASO应用商店优化

2014-12-30 17:13:51

HTML5

2012-05-30 13:49:52

HTML5
点赞
收藏

51CTO技术栈公众号