当前位置:编程学习 > html/css >>

HTML5 Canvas编写五彩连珠(5):寻路

上节主要做了动画的实现,感觉还是比较有意思的。游戏的性能好不好,重绘应该比较重要吧,菜鸟瞎想了下 呵呵。
本节就要做对泡泡的操作,上节后面提到了点击泡泡后泡泡要做出闪动响应,那我们我们如何获得被点击了哪个泡泡呢?
其实Canvas也是html的一个元素而已,所以我们可以给Canvas加click事件。来查看click时鼠标的坐标,这样就等得出点击了map的哪个位置。
我们给game增加一个click方法,当Canvas点击时调用此方法。
要实现的效果是: 当Canvas时被点击时有几种可能:
1、没点到map  那就不作响应
2、点到了泡泡,那该泡泡要做出响应(闪)
3、如果之前有点击过其他的泡泡,则取消之前的泡泡的响应(clicked.stop),如果之前的泡泡是自己,则不作响应。并把clicked作为自己,以体后面的操作。
4、如果点击到的是空格,如果之前点击了泡泡,那就尝试移动这个泡泡,如果clicked为null(之前没泡泡)那就不作任何响应。如果可以移动,则取消闪动,并清除clicked,开始移动。

[javascript]
onclick: function (e) { var px = e.offsetX - game.map.startX;
var py = e.offsetY - game.map.startY;
if (px < 0 || py < 0 || px > game.map.width || py > game.map.height) { return; }
var x = parseInt(px / game.cellWidth);
var y = parseInt(py / game.cellWidth);
var bubble = game.map.getBubble(x, y);
if (bubble.color) { if (this.clicked) { //同一个泡不做反映 if (this.clicked.x == x && this.clicked.y == y) { return; } this.clicked.stop();
} this.clicked = bubble; bubble.play();
} else { if (this.clicked) { this.clicked.stop();
//移动clicked game.map.move(this.clicked, bubble);
} } //console.log("x:" + x + " y:" + y); },  onclick: function (e) { var px = e.offsetX - game.map.startX;
var py = e.offsetY - game.map.startY;
if (px < 0 || py < 0 || px > game.map.width || py > game.map.height) { return;
} var x = parseInt(px / game.cellWidth);
var y = parseInt(py / game.cellWidth);
var bubble = game.map.getBubble(x, y);
if (bubble.color) { if (this.clicked) { //同一个泡不做反映 if (this.clicked.x == x && this.clicked.y == y) { return; } this.clicked.stop();
} this.clicked = bubble; bubble.play();
} else { if (this.clicked) { this.clicked.stop();
//移动clicked game.map.move(this.clicked, bubble);
} } //console.log("x:" + x + " y:" + y); },
寻路的代码还没写,因为这个需要考虑怎么实现。 我绞尽脑汁终于想到了一个办法。暂且撇开游戏的代码,单独实现下两点的寻路代码。
先给定一个棋盘,假如如下:
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了。代码如下:

[javascript]
var map = [ [1, 1, 1, 1, 1], [0, 0, 1, 0, 1], [0, 0, 1, 0, 1], [1, 0, 0, 1, 1] ];
var history = [];
var goal = { "x": 0, "y": 1 }
var goalNode = null;
var getNode = function (x, y, parent) { if (x >= map.length || y >= map.length) { return;
 if (map[y][x] == 1) { return;
} var hasNode = false;
history.forEach(function (n) { if (n.x == x && n.y == y) { hasNode = true; return;
} });
if (hasNode) { return;
} var node = { "x": x, "y": y, "parent": parent, child: [] };
history.push(node);
if (node.x == goal.x && node.y == goal.y) { goalNode = node; return node;
} if (x - 1 >= 0 && !map[y][x - 1]) { node.child.push(getNode(x - 1, y, node));
} if (y - 1 >= 0 && !map[y - 1][x]) { node.child.push(getNode(x, y - 1, node));
} if (x + 1 < map.length && !map[y][x + 1]) { node.child.push(getNode(x + 1, y), node);
} if (y + 1 < map.length && !map[y + 1][x]) { node.child.push(getNode(x, y + 1, node));
} return node; } console.log(getNode(2, 3)); console.log(goalNode); 

 var map = [ [1, 1, 1, 1, 1], [0, 0, 1, 0, 1], [0, 0, 1, 0, 1], [1, 0, 0, 1, 1] ];
var history = [];
var goal = { "x": 0, "y": 1 } var goalNode = null;
var getNode = function (x, y, parent) { if (x >= map.length || y >= map.length) { return;
} if (map[y][x] == 1) { return;
} var hasNode = false; history.forEach(function (n) { if (n.x == x && n.y == y) { hasNode = true; return; } });
if (hasNode) { return;
} var node = { "x": x, "y": y, "parent": parent, child: [] };
history.push(node);
if (node.x == goal.x && node.y == goal.y) { goalNode = node; return node;
} if (x - 1 >= 0 && !map[y][x - 1]) { node.child.push(getNode(x - 1, y, node));
} if (y - 1 >= 0 && !map[y - 1][x]) { node.child.push(getNode(x, y - 1, node));
} if (x + 1 < map.length && !map[y][x + 1]) { node.child.push(getNode(x + 1, y), node);
} if (y + 1 < map.length && !map[y + 1][x]) { node.child.push(getNode(x, y + 1, node));
} return node; } console.log(getNode(2, 3)); console.log(goalNode);
     我加了一个parent,就是指向父亲的指针,那样就不用再去遍历这棵树了。可以直接从goalNode的结果得到整个路径:) 虽然偷懒,但也是要复出代价的,因为这样走的路径不是最短路线,比较傻,怎么选择最优路线呢? 最笨的方法就是 把所有的路径都得到(深度优先遍历树N遍- -)然后比较。这个显然效率不高。开始我也不知道效果会这么差,等一运行(你运行下就知道了),我发现,是代码写的不好(废话)。因为我们每次的判断顺寻都是 左上右下,这样路径总是这个方向探索,而最优的路径应该是朝目标点的方向探索。 由于是递归查找,所以,对当前的node和目标node进行坐标的方向判断,然后调整判断顺序,这样是得到的才是比较短的路径。

[javascript]
var child = [];
var left, top, right, buttom;
//最短路径的粗略判断就是首选目标位置的大致方向 if (x - 1 >= 0 && map.isEmpty(x - 1, y)) left = { "x": x - 1, "y": y };
if (x + 1 < map.length && map.isEmpty(x + 1, y)) right = { "x": x + 1, "y": y };
if (y + 1 < map.length && map.isEmpty(x, y + 1)) buttom = { "x": x, "y": y + 1 };
if (y - 1 >= 0 && map.isEmpty(x, y - 1)) top = { "x": x, "y": y - 1 };
if (x > x2) { if (y > y2) child = [left, top, right, buttom];
else if (y < y2) child = [left, buttom, right, top];
else child = [left, top, right, buttom];
} else if (x < x2) { if (y > y2) child = [right, top, left, buttom];
else if (y < y2) child = [right, buttom, left, top];
else child = [right, top, left, buttom];
} else if (x == x2) { if (y > y2) child = [top, left, right, buttom];
else if (y < y2) child = [buttom, left, right, top]; } for (var i = 0;
i < child.length;
i++) { var c = child[i];
if (c) node.child.push(getnode(c.x, c.y, node)); } 

var child = [];
var left, top, right, buttom;
//最短路径的粗略判断就是首选目标位置的大致方向 if (x - 1 >= 0 && map.isEmpty(x - 1, y)) left = { "x": x - 1, "y": y };
if (x + 1 < map.length && map.isEmpty(x + 1, y)) right = { "x": x + 1, "y": y };
if (y + 1 < map.length && map.isEmpty(x, y + 1)) buttom = { "x": x, "y": y + 1 };
if (y - 1 >= 0 && map.isEmpty(x, y - 1)) top = { "x": x, "y": y - 1 };
if (x > x2) { if (y > y2) child = [left, top, right, buttom];
else if (y

补充:web前端 , HTML 5 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,