有没有一种方法可以轻松跟随尾巴的位置?如果可以的话,您可以将头部的目标设置为与尾巴的位置相同。如果你不能轻易地标记你尾巴的位置,那么它会更复杂。
如果你知道蛇有多长(我假设你知道,因为蛇可以遵循snakeLength
大小正好的矩形路径)并且它是矩形的,那么你应该能够进入一个继续循环直到 endOfGame == true 的状态, 例如。
divSnakeLength = snakeLength / 4; (giving you the length of each side of the rectangle)
distanceToApple = divSnakeLength / 2;
applePosition = this.getApplePosition();
Position[] rectangleEdges = new int[2][2];
rectangleEdges[0] = {applePosition.x - distanceToApple,
applePosition.y + distanceToApple};
rectangleEdges[1] = {applePosition.x + distanceToApple,
applePosition.y + distanceToApple};
rectangleEdges[2] = {applePosition.x + distanceToApple,
applePosition.y - distanceToApple};
rectangleEdges[3] = {applePosition.x - distanceToApple,
applePosition.y - distanceToApple};
//now we have the four corners of the rectangle
while(!endOfGame){
foundGoal = false;
setGoal(rectangleEdges[0]);
while(!foundGoal(rectangleEdges[0]));
setGoal(rectangleEdges[1]);
while(!foundGoal(rectangleEdges[1]));
setGoal(rectangleEdges[2]);
while(!foundGoal(rectangleEdges[2]));
setGoal(rectangleEdges[3]);
while(!foundGoal(rectangleEdges[3]));
}
我希望这会在你的路上轻推你。