haven’t really put much thought into this just throwing it out there. You could figure out where the player is in relationship to your ghosts or whatever (ie. up, down, right, or left). Then tell it to go in the direction that is farther, so if you are up 2 right 4 from an enemy it will head right until right is no longer the farthest distance direction when it gets to the next avaliable path. As far as the actual code to do this I don’t have the time to try it right now but hopefully my idea can get you started.
That’s very complicated. Actually, I wouldn’t know where to start to do that. So you might want to check at gamasutra, they have top quality articles about that kind of things.
Oh wow… AI… Now you’re gteting into some of the nitty gritty stuff… This is what I love to work with… Not really… I love working with physics more… hehehe
Check this out… I’m going to put this in a simple algorithm… I built two small AI’s for you… One is for a stupid monster… And Another is for a smart monster…
By the way… The only times you’d have to call these functions is at an intersection… If you have a regular turn… Just put a small script saying if you hit this mc you have a 50/50 chance of turning back or continuing…
// Algorithm One
// Stupid AI
decision = random number 1-100;
if(decision <=15)
{
make monster move right;
}
if(decision > 15 && decision <= 30)
{
make monster move left;
}
if(decision > 30 && decision <= 45)
{
make monster move up;
}
if(decision > 45 && decision <= 60)
{
make monster move down;
}
if (decision > 60)
{
check player position...
make monster move towards character...
}
Allright… That was just an algorithmm… Basically… If you wnat the monster to be smarter… Just change the rations… Right now I have it… 60 % he just wanders around… And 40 % he makes a good decision… As for making him smart smart… Put this… 20% stupid move and 80% move towards the player… That would be hell-a more difficult… hehehe…
Hope this helps… Okay… Have I proved I know my stuff about video game making yet guys? lol Peace…
Allright… This is what I meant by moving towards the player…
If you didn’t notice… I like giving someone algorithsma dn not code… It let’s them do some exploring and feel good if they get it… or if they try hard enough to…
You are correct though… And the pathfidning is all about adding blank movie clips on your game board actually… At every intersection in your pac man game… Put a small blank movie clip in the center of each one… Name all these movie clips the same…
Call it : intersection
Now… Take a look at the following code…
// This algorithm code shall be placed inside of each of the
// Monster Movie Clips.. And should be checked every single
// frame. onClipEvent (enterFrame).. That pretty much...
// Now.. Place the whole AI code inside of an if condition..
// Basic AI Code
mID = 1; // Each monster should have their own ID
if(this.hitTest(_root.intersection)
{
// I'm being lazy on this first one... Even though I should
// Know it off the top of my head.. I still forget how to
// set the ceilings and stuff by heart... Soo many programming
// languages stuck in one head will do that.. *lol*
decision = random number 1-100;
if(decision <=15)
{
xspeed[mID] = -5; // Go Left
yspeed[mID] = 0;
}
if(decision > 15 && decision <= 30)
{
xspeed[mID] = 5; // Go Right
yspeed[mID] = 0;
}
if(decision > 30 && decision <= 45)
{
xspeed[mID] = 0;
yspeed[mID] = -5; // Go Up
}
if(decision > 45 && decision <= 60)
{
xspeed[mID] = 0;
yspeed[mID] = 5; // Go Down
}
if (decision > 60)
{
// This is where the tough coding comes in... Basically
// Test to see where the player is located in terms
// of this monster.
testx = this._x - _root.player._x;
testy = this._y - _root.player._y;
// This then asks you.. Taking the absolute value of the x
// y finds.. Which one is smaller.. Cause that would mean
// that the location to the x side or the y side would be
// closer to the monster and that is where you want to
// the monster go if he is chasing the player
if(math.abs(testx) < math.abs(testy))
{
if(testx < 0)
{
xspeed[mID] = -5; // Go Left
yspeed[mID] = 0;
}
else
{
xspeed[mID] = 5; // Go Right
yspeed[mID] = 0;
}
}
else
{
if(testy < 0)
{
xspeed[mID] = 0;
yspeed[mID] = -5; //Go Up
}
else
{
xspeed[mID] = 0;
yspeed[mID] = 5; //Go Down
}
}
}
}
Allright allright… Wow… It wasn’t that simple after all was it? No way… hehe… Actually… This is a veyr simple AI script… But for simple games… it works out remarkably
If you’d like to make the monster’s harder… Just take the decisions… And divide it up by 10 instead of 15…
OK, but you’re supposing that all intersections are cross-roads, and that there can be no wall between the enemy and the player in your algorithm, no? :-\
I totally forgot about that when I did this on here… And I even worked it out on paper…
*laughing really hard now… Okay… sorry about that pom! lol…
Jesus… You’re right actually… I’m sorry… here is All of the code… You know… I thought it looked rather thin to me also…
// This algorithm code shall be placed inside of each of the
// Monster Movie Clips.. And should be checked every single
// frame. onClipEvent (enterFrame).. That pretty much...
// Now.. Place the whole AI code inside of an if condition..
// Basic AI Code
mID = 1; // Each monster should have their own ID
if(this.hitTest(_root.intersection)
{
// I'm being lazy on this first one... Even though I should
// Know it off the top of my head.. I still forget how to
// set the ceilings and stuff by heart... Soo many programming
// languages stuck in one head will do that.. *lol*
decision = random number 1-100;
if(decision <=15)
{
xspeed[mID] = -5; // Go Left
yspeed[mID] = 0;
}
if(decision > 15 && decision <= 30)
{
xspeed[mID] = 5; // Go Right
yspeed[mID] = 0;
}
if(decision > 30 && decision <= 45)
{
xspeed[mID] = 0;
yspeed[mID] = -5; // Go Up
}
if(decision > 45 && decision <= 60)
{
xspeed[mID] = 0;
yspeed[mID] = 5; // Go Down
}
if (decision > 60)
{
// This is where the tough coding comes in... Basically
// Test to see where the player is located in terms
// of this monster.
testx = this._x - _root.player._x;
testy = this._y - _root.player._y;
// Allright.. here is where I screwed up the first time..
// I keep forgetting about the possibility for different
// directions.. Soo... here is the right code... SORRY!
whichway = ["0","0","0","0"];
// Whichway will hold which ways are okay to use...
// Next line here tells which ways are good by an
// invisible movie clip named : blanktester
// This should have alpha 0% but be a box about 8
// by 8 pixels
_root.blanktester._x = this.monster._x - 20;
_root.blanktester._y = this.monster._y;
if(_root.blanktester.hitTest(_root.wall))
{
whichway[0] = 1;
}
_root.blanktester._x = this.monster._x + 20;
if(_root.blanktester.hitTest(_root.wall))
{
whichway[1] = 1;
}
_root.blanktester._x = this.monster._x;
_root.blanktester._y = this.monster._y - 20;
if(_root.blanktester.hitTest(_root.wall))
{
whichway[2] = 1;
}
_root.blanktester._y = this.monster._y + 20;
if(_root.blanktester.hitTest(_root.wall))
{
whichway[3] = 1;
}
// Now we have a set of whichway the pac man can go..
// Now here is the tough part... Taking that... Along
// with what we got.. And making the monster move...
// Arrange how you want to test.. From best to worst...
testorder = ["0","0","0","0"];
if(math.abs(testx) < math.abs(testy))
{
if(testx < 0)
{
testorder = ["1","2","3","4"];
}
else
{
testorder = ["2","1","3","4"];
}
}
else
{
if(testy < 0)
{
testorder = ["3","4","1","2"];
}
else
{
testorder = ["4","3","1","2"];
}
}
// And now.. The final if statements... This is what we
// were waiting for... Phew.. This could probably be
// optimized.. But getting it to work right now is the key
// thing eh? hehe... Final checks done NOW.
for(i=0;i<=3;i++)
{
if(whichway[testorder*] == 1)
{
if (testorder* == 1)
{
xspeed[mID] = -5; // Go Left
yspeed[mID] = 0;
}
if(testorder* == 2)
{
xspeed[mID] = 5; // Go Right
yspeed[mID] = 0;
}
if(testorder* == 3)
{
xspeed[mID] = 0;
yspeed[mID] = -5; // Go Up
}
if(testorder* == 4)
{
xspeed[mID] = 0;
yspeed[mID] = 5; // Go Down
}
}
}
}
}
Okay… After backtracking through all of this… There is probbaly an easier way to do this… But for right now… It’s about the best way I cna spit out… I’ll optimize it sometime later with a different idea then…
Peace Out… Thanks for helping point it out pom… i can’t believe I forgot to put that in… lol
Thanks marz your nice to whip up a cool example.
And when im done looking at it i will.
Ask what parts in the code i dont undertand and ask you.
By the way were the hell did you learn it.
Maybe everone can help here.
I think homer guy had a website i dont remember.,
Because im making a car game(Without AI) Then add it later.
So peace out.
Video Game Programming I get from a combination of these…
Advanced Placement Physics
Advanced Placement Calculus and Geometry
Working up the ladder of programming languages… I think it comes more natural for me to look at a game now and can say… Okay… I udnerstand how that wokrs… And likewise and etcetera…
I have no clue how I come up with some of this stuff… lol… Basically… Just kepe working with it… And play lotsa video games… But while you play… try and figure out small algorithms (not code) to work with… Then work with it…