# Movement in pacman

This topic is 4920 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am unsure about how to do movement in pac-man. Because the background has to have some sort of tiling for it for the ai to work. I have my A* working and I want to create a simple pac-man clone but obviosly it would be crap moving tiles at a time unless they were really small. How is this done? Do I have it the 'board] of an array of say 30 by 30 tiles and the charachter has a real xpos and ypos and then offsets that change and once the offsets are greater than 30 then the xpos or ypos change? And then I have the enemies search for the real x and y? Is this a good way to do this. I couldn't find any understandable source code for pacman and am unsure how to get moving right. Any explaination would be appreciated, thanks vbuser

##### Share on other sites
you can keep two sets of coordinates - one for which tile a character is in, and one for how far across/down within that tile they are. use A* on the large tiles to decide where to go.
or, keep all coordinates in the high resolution coordinates, and get the tile they are in by dividing by the tile size.

##### Share on other sites
I've made two pacman clones and both times I used the "how far across/down within that tile they are" method. Basically they worked like this:

My Pacman has 7 variables: TileX,TileY,MovementX,MovementY,Direction, a movement timer and an animation timer.

The map in the first game consisted of a 20*20 grid with each tile being 24*24 pixels. When pacman isn't moving(stuck) the direction is set to 0. Now say you want 1 to represent right,2 is left, 3 is up and 4 is down. So when the user presses right his direction is set to 1 and etc.

//this is for tile-by-tile movement. //normally he shouldn't stop on every tile but I wanted to make //it simple so I left out the collision code that would normally stop himif(MoveTimer.Intervals>0){MoveTimer.Reset();switch(Direction){case 1:MovementX++;if(MovementX>=24){TileX++;MovementX=0;Direction=0;}break;case 2:MovementX--;if(MovementX<=-24){TileX--;MovementX=0;Direction=0;}break;case 3:MovementY--;if(MovementY<=-24){TileY--;MovementY=0;Direction=0;}break;case 4:MovementY++;if(MovementY>=24){TileY++;MovementY=0;Direction=0;}break;default: break;}}

X position=(TileX*24)+MovementX
Y position=(TileY*24)+MovementY

That's the just of it. Now of course you'll want to add collision detection with the walls and ghosts. Also be sure to implement some sort of time based movement as there's no real way to guarantee a certain framerate.

##### Share on other sites
Thanks for the repy I am going to get that working and hopefully get a good working pac-man clone.
Cya,
vbuser

##### Share on other sites
Ok I got movement down for the pacman fine but when I try getting the ghosts to work it goes funny. It will work fine but every few mins a pac man will just keep moving because it does not hit its target. I have it move the x and y offsets if it is not at the target and if it is it will take the first move on the moveList vector and use that as the target. It will work until a cetain point and then the ghost will bump up and down until I go close then it will follow me again. There is no need for collision since the pathfinding already does that. But It just puzzles me how this is happening. Its not too often but it does happe. Well here is the source if anyone wants to help.
void Ghost::Update(){    // See if we reached next tile    if(movementx >= 32){ xpos = targetx; movementx = 0; }    if(movementx <= -32){ xpos = targetx; movementx = 0; }    if(movementy >= 32) { ypos = targety; movementy = 0; }    if(movementy <= -32) { ypos = targety; movementy = 0; }        if(xpos == targetx && ypos == targety && moves.size() > 0)  // We are at target so find next tile    {        int xchange = moves[0].xcoord - xpos;   // Find which way the move is        int ychange = moves[0].ycoord - ypos;                if(xchange == 0)            targetx = xpos;        if(ychange == 0)            targety = ypos;                    // Change the direction        if(xchange == -1)        {            dir = Left;     // Move left            targetx = xpos - 1;            targety = ypos;        }        if(xchange == 1)        {            dir = Right;    // Move right            targetx = xpos + 1;            targety = ypos;        }        if(ychange == -1)        {            dir = Up;       // Move up            targety = ypos - 1;            targetx = xpos;        }        if(ychange == 1)        {            dir = Down;     // Move down            targety = ypos + 1;            targetx = xpos;        }        moves.erase(moves.begin()); // Erase that move    }        if((xpos != targetx || ypos != targety) && moves.size() > 0) // Not at target x    {        if(dir == Left)     // If not at target update x offset            movementx -= 1;        if(dir == Right)            movementx += 1;        if(dir == Up)            movementy -= 1;        if(dir == Down)            movementy += 1;    }}

After 2 hours of testing it and trying to fix it I am going to take a break.
Cya later,
vbuser

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633299
• Total Posts
3011262
• ### Who's Online (See full list)

There are no registered users currently online

×