当前位置: 移动技术网 > IT编程>网页制作>Html5 > HTML5 Canvas编写五彩连珠(5):寻路

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

2018年12月27日  | 移动技术网IT编程  | 我要评论
上节主要做了动画的实现,感觉还是比较有意思的。游戏的性能好不好,重绘应该比较重要吧,菜鸟瞎想了下 呵呵。 本节就要做对泡泡的操作,上节后面提到了点击泡泡后泡泡要做出闪动响应,那我们我们如何获得被点击

上节主要做了动画的实现,感觉还是比较有意思的。游戏的性能好不好,重绘应该比较重要吧,菜鸟瞎想了下 呵呵。
本节就要做对泡泡的操作,上节后面提到了点击泡泡后泡泡要做出闪动响应,那我们我们如何获得被点击了哪个泡泡呢?
其实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 < 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)); }
代码虽然写的比较傻,但这种方式不得不说好就一个字:)


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

[javascript]
move: function (bubble, target) { var path = this.search(bubble.x, bubble.y, target.x, target.y);
if (!path) { //显示不能移动s alert("过不去");
return;
} //map开始播放当前泡的移动效果
//两种实现方式,1、map按路径染色,最后达到目的地
2、map生成一个临时的bubble负责展示,到目的地后移除
//console.log(path);
var me = this;
var name = "move_" + bubble.x + "_" + bubble.y;
var i = path.length - 1;
var color = bubble.color;
game.play(name, function () { if (i < 0) { game.stop(name);
return;
 } path.foreach(function (cell) { me.setbubble(cell.x, cell.y, null); });
var currentcell = path[i];
me.setbubble(currentcell.x, currentcell.y, color);
i--;
 }, 50);
}, search: function (x1, y1, x2, y2) {
var history = []; var goalcell = null;
var me = this; getcell(x1, y1, null);
if (goalcell) { var path = [];
var cell = goalcell;
while (cell) { path.push({ "x": cell.x, "y": cell.y });
cell = cell.parent;
} return path;
} return null;
function getcell(x, y, parent) { if (x >= me.bubbles.length || y >= me.bubbles.length) return;
if (x != x1 && y != y2 && !me.isempty(x, y)) return; for (var i = 0;
i < history.length; i++) { if (history[i].x == x && history[i].y == y) return;
} var cell = { "x": x, "y": y, child: [], "parent": parent };
history.push(cell);
if (cell.x == x2 && cell.y == y2) { goalcell = cell;
return cell;
} var child = []; var left, top, right, buttom;
//最短路径的粗略判断就是首选目标位置的大致方向 if (x - 1 >= 0 && me.isempty(x - 1, y)) left = { "x": x - 1, "y": y };
if (x + 1 < me.bubbles.length && me.isempty(x + 1, y)) right = { "x": x + 1, "y": y };
if (y + 1 < me.bubbles.length && me.isempty(x, y + 1)) buttom = { "x": x, "y": y + 1 };
if (y - 1 >= 0 && me.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) cell.child.push(getcell(c.x, c.y, cell)); } return cell;
} }, 

 move: function (bubble, target) {
var path = this.search(bubble.x, bubble.y, target.x, target.y);
if (!path) {
//显示不能移动s alert("过不去");
return;
} //map开始播放当前泡的移动效果
//两种实现方式,1、map按路径染色,最后达到目的地
2、map生成一个临时的bubble负责展示,到目的地后移除
//console.log(path);
var me = this;
var name = "move_" + bubble.x + "_" + bubble.y;
var i = path.length - 1;
var color = bubble.color;
game.play(name, function ()
{ if (i < 0) { game.stop(name);
return;
} path.foreach(function (cell) { me.setbubble(cell.x, cell.y, null);
});
var currentcell = path[i];
me.setbubble(currentcell.x, currentcell.y, color);
i--;
}, 50);
}, search: function (x1, y1, x2, y2) { var history = [];
var goalcell = null;
var me = this;
getcell(x1, y1, null);
if (goalcell) { var path = [];
var cell = goalcell;
while (cell) { path.push({ "x": cell.x, "y": cell.y });
cell = cell.parent;
} return path;
} return null;
function getcell(x, y, parent)
{ if (x >= me.bubbles.length || y >= me.bubbles.length) return;
if (x != x1 && y != y2 && !me.isempty(x, y)) return;
for (var i = 0;
i < history.length;
i++) { if (history[i].x == x && history[i].y == y) return;
} var cell = { "x": x, "y": y, child: [], "parent": parent
};
history.push(cell);
if (cell.x == x2 && cell.y == y2) { goalcell = cell;
return cell;
} var child = [];
var left, top, right, buttom;
//最短路径的粗略判断就是首选目标位置的大致方向 if (x - 1 >= 0 && me.isempty(x - 1, y)) left = { "x": x - 1, "y": y };
if (x + 1 < me.bubbles.length && me.isempty(x + 1, y)) right = { "x": x + 1, "y": y };
if (y + 1 < me.bubbles.length && me.isempty(x, y + 1)) buttom = { "x": x, "y": y + 1 };
if (y - 1 >= 0 && me.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) cell.child.push(getcell(c.x, c.y, cell)); } return cell;
}
},

 看下动画效果

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

 摘自  君之蘭
 

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网