pacman - navigating through the tiles

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)

I don’t really have time to study ur code right now, but what I would do is just have a char variable called direction, set to ‘l’, ‘r’, ‘u’, or ‘d’, since pacman can never stop there’s no need for ‘n’, and since he can’t change directions mid-tile, only at intersections, I would store another char variable called userDir that is taken from key input, and is always changeable (because you can tell pacman to turn mid-tile, he just won’t do it yet) then when he reaches the intersection set direction to userDir. If userDir stays the same pacman will also therefore keep going in that direction. of course check for collision right after setting direction to userDir. Every repaint update his movement and the direction of his image based on the direction variable, not based on userDir. When checking for collision, don’t write a separate test for every direction, all you need to do in a tile-based world is say something like if(tiles[x][y] == false) movePacman(); where tile is your world, stored in boolean form (wall == true, space == false) or of course whatever form you want, and where x and y are the coordinates of pacman’s goal tile (only do this right at the intersection). This is just what I would do, but I think it’s pretty efficient, it shouldn’t take up any time at all. Not sure if that helped or not…

yes i think i got it much working much better right now.

i realized how to do everything after u said that he can only turn at intersections, so i just calculate whehter or not he is on only one tile, and if so then handle direction changes or dot eating actions, also before i do that i check to see if the next requested direction is opposite that of the current direction since then he can go back the way he came.

thanks… now i have to go to getting enemies to move without them getting caught in circular loops lol…