0

我正在 Node.js/Socket.IO 中创建一个小游戏,需要一些关于创建 AI 的建议。下面的代码是我想出的一个真正快速的示例,但它是如此之快,玩家甚至看不到敌人在客户端移动。我是否采用这种方法是正确的,还是有更好的方法我应该这样做?

谢谢!

var random;

setInterval(function() {
    random = Math.round(Math.random() * 200);
    move(random, random);
    console.log("Moving player");
}, 10000)

var move = function(targetX, targetY) {
    if (x < targetX) {
        while (x < targetX) {
            x++;
            sendNewCoordinates(x, y);
        }
    } else if (x > targetX) {
        while (x > targetX) {
            x--;
            sendNewCoordinates(x, y);
        }
    } else if (y < targetY) {
        while (y < targetX) {
            y++;
            sendNewCoordinates(x, y);
        }
    } else if (y > targetY) {
        while (y > targetX) {
            y--;
            sendNewCoordinates(x, y);
        }
    }
};

var sendNewCoordinates = function(newX, newY) {
    socket.sockets.emit("move enemy", {x: newX, y: newY});
};
4

1 回答 1

1

这实际上是一个非常好的AI!随机化运动之间的间隔是一种非常简单、常用的技术。我很好奇,很想试试你在做什么!不过要注意的一件事是确保人工智能不是太好

您可以在代码中实现的另一件事是让您的 AI“瞄准”稍微远离目标的一点。例如:

var move = function(targetX + randomX, targetY + randomY)

您还可以使用目标在移动之前的位置来预测它的前进方向。

var xChange = (targetX2 - targetX1)/(timeInterval1);
var yChange = (targetY2 - targetY1)/(timeInterval1);
var move = function(targetX + xChange * timeInterval2, targetY + yChange * timeInterval2)

其中 timeInterval1 是两个目标位置之间的时间间隔,timeInterval2 是您当前位置和下一个位置之间的时间间隔。

关键是不要让 AI 对玩家来说太难。;)

于 2014-12-25T03:08:42.930 回答