View Full Version : Random movement for ghosts in Pacman
spidy
January 21st, 2008, 03:04 AM
Hi,
Can anyone explain me how to create random movement for ghosts in pacman?
http://oos.moxiecode.com/tut_07/index.html
I found the tutorial here to be incomplete.
A game similar to
http://www.mediakitchen.co.uk/chomper1.html
Plzzzzzzzzz reply.
Thanks
robotz
January 21st, 2008, 07:59 AM
I remember the first ever pacman game I made (back on the Atari ST) used some really simple movement. Basically I would set a direction for each ghost. For example the red ghost would always turn left at a junction, the blue one would always turn right, etc. After each level I reversed this setting.
You'd be surprised how effective such a simple movement pattern is! It's also dead easy to code, so a good starting point while you work on zoned base directions :)
spidy
January 21st, 2008, 11:09 AM
Thanks a lot Robotz! I have another doubt.How should I program movement for ghosts once a pill is consumed by Pacman?
ArmoredSandwich
January 21st, 2008, 01:16 PM
Thanks a lot Robotz! I have another doubt.How should I program movement for ghosts once a pill is consumed by Pacman?
Look up A*
http://www.policyalmanac.org/games/aStarTutorial.htm
spidy
January 22nd, 2008, 09:26 AM
@Sandwich: Path finding wud be too complex at this stage for me to implement.I am figuring out some random movement for ghosts.:sad:.Plzzz help me
@Robotz: Can you elaborate your approach.A rough pseudo code would be great.I want to add a certain degree of randomness in my game.Thanks a lot
ArmoredSandwich
January 22nd, 2008, 12:05 PM
@Sandwich: Path finding wud be too complex at this stage for me to implement.I am figuring out some random movement for ghosts.:sad:.Plzzz help me
I have no other idea on how you could do it so that it would be dynamic. Maybe something with backtracking the steps it took. Another way would be just to hardcode it, but that wouldn't be dynamical. The truth is that the ghosts in pacman aren't random, they have true ai to seek and destroy you.
Anyways, I can understand A* is too difficult for now. What I don't understand is why you wouldn't do it like the example you gave.
Also, getting the random AI shouldn't be too difficult IF you are able to detect when it can't go on (reached an end). Are you?
spidy
January 22nd, 2008, 01:58 PM
I have no other idea on how you could do it so that it would be dynamic. Maybe something with backtracking the steps it took. Another way would be just to hardcode it, but that wouldn't be dynamical. The truth is that the ghosts in pacman aren't random, they have true ai to seek and destroy you.
Anyways, I can understand A* is too difficult for now. What I don't understand is why you wouldn't do it like the example you gave.
Also, getting the random AI shouldn't be too difficult IF you are able to detect when it can't go on (reached an end). Are you?
One of the observation that I made in both the examples is that the AI changes the direction even before it hits the wall.How should I tackle that?
ArmoredSandwich
January 22nd, 2008, 03:44 PM
One of the observation that I made in both the examples is that the AI changes the direction even before it hits the wall.How should I tackle that?
Count the steps (number of times the movieclip changes from tile) and at a certain number of steps (random) change the direction. Or (not as good, imo) time it and change direction after a certain period of time(random).
I'm not too sure what you're trying to ask here though, English is not my main language.
robotz
January 22nd, 2008, 07:01 PM
One of the observation that I made in both the examples is that the AI changes the direction even before it hits the wall.How should I tackle that?
Well I said that a ghost should change direction at a junction. A junction point is any block that has an alternative route on it (i.e. a cross-roads, t-junction, etc) as well as just a end wall piece.
A* is overkill for Pacman in a really big way! :) the original arcade game used nothing as complex as that! It was actually really quite simple in the way it worked, but oh so effective.
It's heavily based on timers.. the ghost will go through patterns of chasing you vs. scattering, so as to stop them all bunching up or making their pursuit relentless. The timers are based on a number of things, from the amount of uneaten dots, to the level you are on. Google around, you'll get a list easily.
The AI of the ghosts is actually really basic:
Red will go for Pacman's current location. He'll pick whatever the shortest route is either horizontal or vertical. He turns in whatever direction will immediately reduce the distance between him and Pacman.
Pink - Does exactly the same as Red, but his target is 4 squares ahead of the direction Pacman is facing. This is why if you are facing away from him he'll chase you (but turn and face him and you can often distract him)
Blue - He uses 2 coordinates, the location 2 squares ahead of Pacman, and the location of the red ghost. Take the position of the red ghost to pacman, and keep going twice the distance, that is where Blue is heading.
Orange - If further than 8 tiles away from Pacman he uses Reds logic. Incredibly, if he's within 8 tiles of Pacman he heads for his home corner (bottom left) ! If you are in the bottom left, he heads away from you (until you leave this area) then returns there. Thus he only appears to follow you if you are between him and the lower left part of the map.
This stuff is based on a thread I remember reading on the Atari Age website years ago, it was utterly fascinating - search their forum and you'll no doubt find the full version. Failing that the above should give you plenty to go on! Implement this and you'll have one hell of a good Pacman game on your hands.
oldmanwinter
January 23rd, 2008, 12:16 PM
i wouldn't think it would be too hard, just put little nodes at each corner/turning-point and use something similar to the following code:
for(i=1;1<=number_of_nodes;i++)
{
if((ghost._x==_root["node"+i]._x)and(ghost._y==_root["node"+i]._y))
{
//change course based on Math.random
}
}
hehe, i guess the course change is the more complicated part... how do you keep track of your ghosts movement? do they have velx/vely variables or do you just use an integer to keep track of which direction you want them to move in (e.x. 1 for up, 2 for down, 3 for right, 4 for left)? because you'll probably want to assign variables to each node which say which directions the ghost is able to move in (for example, if you have an L shaped corner, the ghost won't be able to move left or down). for the code which changes their course at random, you'd want to somehow multiply the random number (which is a real number between 0-1 as you probably know) by the number of available directions the ghost can move in, then assign each available direction to a number between 1 and the number of available directions, and Math.floor your random number to determine which direction your ghost will go in. i'm not sure how i would code that exactly, but i wouldn't think it'd be too hard to figure out. that's how i'd do it at least, there could very well be a more efficient or easier way to do it.
tarwin
January 24th, 2008, 01:05 AM
I find giving your ghost some kind of ferocity helps. When you hit a junction get a random number (say you get 0.7) and check that against your ferocity (> 0 < 1) and if it's bigger then if you can move towards the player, otherwise choose a random direction.
Let's you adjust how much they follow you in an intuitive way. Make it wander aimlessly = 0, make it follow your character (yes I know not smartly, but what kind of ghost is going to do an A* anyway) set it to 1.
robotz
January 24th, 2008, 09:31 AM
All I would add is don't make the ghosts TOO smart. The problem the original team who created Pacman had was making it intelligent enough to be a challenge, but forgiving enough to not overwhelm the player. That is why the ghosts go through states of scattering and ambush/attack - make it relentless and it's just no fun at all. I've seen numerous Flash pacman games and they all suffered from that problem (none of them had accurate ghost movements)
spidy
January 25th, 2008, 09:26 AM
As I m making a tile based game(Pacman) I referred the moxie site mentioned in my first post.The tutorial is incomplete..fla files are provided .Can anyone explain me the idea behind that script?
Thank you everyone for your replies so far.But plzzzz reply.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.