hi,
im making a pacman game, and im having some trouble figuring out how the pacman should navigate through the tiles. the way i have it setup now, is that their is a variable called nextDir which is like a queue for the next direction. it is set when the user presses on of the keys. then in a method called canMove(incX, incY). here is that method:
if (nextDir != currDir || nextDir != NONE) {
if (nextDir == LEFT && canMove(-2,0)) currDir = nextDir;
if (nextDir == RIGHT && canMove(2,0)) currDir = nextDir;
if (nextDir == UP && canMove(0,-2)) currDir = nextDir;
if (nextDir == DOWN && canMove(0,2)) currDir = nextDir;
}
this is before the draw method, that calls the canMove method.
int _x = (pacmanR.x)/40;
int _y = (pacmanR.y)/40;
int mapTile = _y*20+_x;
pacmanR.x+=incX;pacmanR.y+=incY;
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-1))) {
if (MazeHandler.getMazeTileType(mapTile-1) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+1))) {
if (MazeHandler.getMazeTileType(mapTile+1) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+20))) {
if (MazeHandler.getMazeTileType(mapTile+20) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-20))) {
if (MazeHandler.getMazeTileType(mapTile-20) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-21))) {
if (MazeHandler.getMazeTileType(mapTile-21) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile-19))) {
if (MazeHandler.getMazeTileType(mapTile-19) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+21))) {
if (MazeHandler.getMazeTileType(mapTile+21) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
if (pacmanR.intersects(MazeHandler.getMazeTileBounds(mapTile+19))) {
if (MazeHandler.getMazeTileType(mapTile+19) == MazeHandler.WALL) {
pacmanR.x-=incX;pacmanR.y-=incY;
return false;
}
}
pacmanR.x-=incX;pacmanR.y-=incY;
return true;
}
so that is what im doing right now. i know there has got to be a better less time consuming more efficient way, but i cant figure it out. can anyone give me a few pointers?
thanks. (if u need anymore of the code to see what im doing just say so :P)