【发布时间】:2022-01-24 01:20:44
【问题描述】:
我正在使用 Javascript 画布开发自定义吃豆人游戏。我有不同的课程,一个是给玩家的,一个是给敌人的,另一个是给棋盘上的墙的。 在我的墙类中,我有以下代码:
class Wall {
constructor(x, y, width, height, color, gate){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
this.gate = gate;
};
draw(){
ctx.lineWidth = 3;
ctx.strokeStyle = this.color;
ctx.strokeRect(this.x, this.y, this.width, this.height);
};
在我的敌人类中,我有以下代码:
class Enemy {
constructor(img, x, y){
this.img = img;
this.x = x;
this.y = y;
this.width = 36;
this.height = 36;
this.speedX = 0.5;
this.speedY = 0.5;
this.dx = 0;
this.dy = 0;
this.distance = 0;
this.angle = 0;
};
draw(){
ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
};
checkcollision(object) {
return (
this.x < object.x + object.width &&
this.x + this.width > object.x &&
this.y < object.y + object.height &&
this.y + this.height > object.y
);
};
moveRight(objects) {
this.x += this.speedX;
objects.forEach(object => {
if (this.checkcollision(object)) {
this.x -= this.speedX;
} else if (this.x >= 976){
this.x = 0;
};
});
};
moveLeft(objects) {
this.x -= this.speedX;
objects.forEach(object => {
if (this.checkcollision(object)) {
this.x += this.speedX;
} else if (this.x <= 0){
this.x = 975;
};
});
};
moveUp(objects) {
this.y -= this.speedY;
objects.forEach(object => {
if (this.checkcollision(object)) {
this.y += this.speedY;
};
});
};
moveDown(objects) {
this.y += this.speedY;
objects.forEach(object => {
if (this.checkcollision(object)) {
this.y -= this.speedY;
};
});
};
updateAngleX(player, objects){
this.dx = player.x - this.x;
this.dy = player.y - this.y;
this.distance = Math.sqrt((this.dx*this.dx) + (this.dy*this.dy));
this.angle = Math.atan2(this.dy,this.dx);
this.x += Math.cos(this.angle) * this.speedX;
objects.forEach(object => {
if (this.dead === false || object.gate === false){
if (this.checkcollision(object)) {
if (this.dy > 0){
this.y += this.speedY;
this.x -= Math.cos(this.angle) * this.speedX;
if (this.checkcollision(object)){
this.y -= this.speedY;
};
} else if (this.dy < 0){
this.y -= this.speedY;
this.x -= Math.cos(this.angle) * this.speedX;
if (this.checkcollision(object)){
this.y += this.speedY;
};
};
};
};
});
};
updateAngleY(player, objects){
this.dx = player.x - this.x;
this.dy = player.y - this.y;
this.distance = Math.sqrt((this.dx*this.dx) + (this.dy*this.dy));
this.angle = Math.atan2(this.dy,this.dx);
this.y += Math.sin(this.angle) * this.speedY;
objects.forEach(object => {
if (this.dead === false || object.gate === false){
if (this.checkcollision(object)) {
if (this.dx > 0){
this.x += this.speedX;
this.y -= Math.sin(this.angle) * this.speedY;
if (this.checkcollision(object)){
this.x -= this.speedX;
};
} else if (this.dx < 0){
this.x -= this.speedX;
this.y -= Math.sin(this.angle) * this.speedY;
if (this.checkcollision(object)){
this.x += this.speedX;
};
};
};
};
});
};
};
但是我有一些问题,因为敌人在找到如下图的角落时会卡住:
就像敌人应该沿着顶部或正确的路径继续迫害,但它被困在角落里。 如何更改我的功能,以便他们找到播放器的正确路径?谢谢。
将整个代码添加到 codepen 以在此处检查:https://codepen.io/SpaSniper/pen/bGooKrP
【问题讨论】:
-
我们需要有关您要解决的问题的更多信息。
-
@Vektor 我添加了 Wall 类,您还需要什么信息?
-
你能否更好地澄清“敌人找到角落时被卡住”?
-
@Vektor 我在帖子中添加了一张图片来解释它。
-
使游戏基于图块,暂时移除墙壁,然后尝试使用@Johnny 提到的 Dijkstra 算法向玩家前进。在向代码添加更多复杂性之前,尝试让基本的东西正常工作。
标签: javascript function path-finding pacman