我目前正致力于在我的蛇游戏中实现A *寻路算法。我理解算法是如何工作的,唯一困难的部分是实现算法。更具体地说,我试图让相邻节点到网格上的当前节点。我这样做的原因是因为我将能够遍历相邻节点(带有列表)并检查哪些具有最低的g和h成本。我创建了一个循环遍历相邻节点的x和y坐标列表的方法,然后创建一个保存这些坐标的新临时节点。然后我有一个默认的空nodeNeighborList,它跟踪所有相邻节点。在该方法中,临时节点变量(在方法中)然后被添加到列表中。
我的问题是,当我在run方法中调用此方法时,我得到一个NullPointerException,表示问题来自我添加临时节点的位置。
我在'game.nodeNeighboursList.add(tempNeighbours);'
收到错误这是我的代码 -
public class Nodes {
private Game game = new Game();
public double h,f;
public int x, y;
public Vector2 vector2;
public Nodes parent;
public Nodes(Vector2 v, double h) {
this.vector2 = v;
this.h = h;
}
public int getX() {
return this.x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
public int getDistanceFromTarget(int x1, int x2, int y1, int y2) {
return (int) Math.sqrt(Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2));
}
public Nodes getNeigbouringNodes(Nodes CurrentNode) {
int[] NeighbourX = {CurrentNode.getX()-20, CurrentNode.getX()+20, CurrentNode.getX(),
CurrentNode.getX(),CurrentNode.getX()-20, CurrentNode.getX()+20,CurrentNode.getX()-20,CurrentNode.getX()+20};
int [] NeighbourY = {CurrentNode.getY(),CurrentNode.getY(),CurrentNode.getY()+20,
CurrentNode.getY()-20,CurrentNode.getY()-20,CurrentNode.getY()-20,CurrentNode.getY()+20,CurrentNode.getY()+20};
for(int x = 0 ; x < NeighbourX.length ; x ++) {
for(int y = 0 ; y < NeighbourY.length; y++) {
Nodes tempNeighbours = new Nodes(new Vector2(NeighbourX[x],NeighbourY[y]),getDistanceFromTarget(
NeighbourX[x],Apple.x,NeighbourY[y],Apple.y));
game.nodeNeighboursList.add(tempNeighbours);
return tempNeighbours;
}
}
return null;
}
}
这是我的主类,run方法是调用'getNeibouringNodes()'的地方:
private int WIDTH = 800,HEIGHT = 800;
private int nodeMax = 800;
private ArrayList<Snake>snakeList;
private ArrayList<Apple>appleList;
private ArrayList<Nodes>nodeList;
public ArrayList<Nodes>nodeNeighboursList;
//private Nodes nodes;
private Snake snake;
public Apple apple;
private Vector2 nodeVector;
private Thread thread;
static int ticks = 0;
private Random rx;
private Random ry;
private Nodes node;
private int snakeSizeStart = 5;
private int size = 20;
private final int threadSpeed = 80;
private boolean right=false,left=false,up=false,down=false;
private int applePosScalar;
private int distance;
// look this up, and sorting nodes based on fcost
private Comparator<Nodes> NodeSorter = new Comparator<Nodes>() {
@Override
public int compare(Nodes arg0, Nodes arg1) {
if(arg1.getDistanceFromTarget(arg1.getX(), Apple.x, arg1.getX(), Apple.y) <
arg0.getDistanceFromTarget(arg0.getX(), Apple.x, arg0.getY(), Apple.y)) {
return 1;
}
if(arg1.getDistanceFromTarget(arg1.getX(), Apple.x, arg1.getX(), Apple.y) >
arg0.getDistanceFromTarget(arg0.getX(), Apple.x, arg0.getY(), Apple.y)) {
return -1;
}
return 0;
}
};
public void init() {
this.resize(WIDTH,HEIGHT);
this.setFocusable(true);
this.addKeyListener(this);
rx = new Random();
ry = new Random();
applePosScalar = WIDTH/size;
nodeVector = new Vector2(20,20);
snake = new Snake(0,0,size);
apple = new Apple(rx.nextInt(applePosScalar),ry.nextInt(applePosScalar),size);
//nodes = new Nodes(0,0,0,getDistance(0, Apple.x,0,Apple.y));
node = new Nodes(nodeVector,getDistance(nodeVector.getX(), Apple.x,nodeVector.getY(),Apple.y));
nodeList = new ArrayList<Nodes>();
appleList = new ArrayList<Apple>();
snakeList = new ArrayList<Snake>();
nodeNeighboursList = new ArrayList<Nodes>();
thread = new Thread(this);
thread.start();
}
public void update(Graphics g) {
paint(g);
}
public void run() {
for(;;) {
if(snakeList.size() == 0) {
snake = new Snake(snake.x,snake.y,size);
snakeList.add(snake);
}
snake.moveSnake();
System.out.println(nodeNeighboursList.size());
if(right) {
snake.x++;
}
if(left) {
snake.x--;
}
if(down) {
snake.y--;
}
if(up) {
snake.y++;
}
if(snake.x < 0 || snake.x > 39 || snake.y < 0 || snake.y > 39) {
snakeList = null;
System.exit(0);
}
// continuously add a snake to the list
snakeList.add(new Snake(snake.x,snake.y,size));
// if the above additions are larger than the snakeSizeStart, then remove a snake object from list
//Other wise there will be an infinite snake!
if(snakeList.size() > snakeSizeStart) {
snakeList.remove(0);
}
for(int i = 1 ; i < snakeList.size() ; i++) {
if(snakeList.get(0).hitBox().intersects(snakeList.get(i).hitBox())) {
//System.out.println("sdf");
}
}
if(snake.hitBox().intersects(apple.hitBox())) {
// add to the snake size, this means that the statement above (snakeList.add(new Snake)) will add a new snake
snakeSizeStart++;
appleList.add(new Apple(rx.nextInt(applePosScalar),ry.nextInt(applePosScalar),size));
}
getDistance(snake.x,apple.getX(),snake.y,apple.getY());
node.getNeigbouringNodes(node); // this is the method that gets the neighboring nodes
System.out.println("S" + nodeNeighboursList.size());
/*
I want the method to return the amount of neighboring nodes there are to the current one
*/
this.repaint();
try {
Thread.sleep(threadSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
如果有任何不清楚的地方,请不要犹豫,因为我很乐意清除任何事情。我真的很感激我能得到的任何帮助! 谢谢!