Pac-Man With Arcade Ghost AI Using GML In GameMaker Studio 2

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i am bradley sword associate professor of computer and information science at the college of dupage in glen ellen illinois and this video today is going to make a pacman clone and we're going to actually create and steal and use the actual arcade ai artificial intelligence and pop that into the game we're going to use game maker studio 2.3 and we're going to use game maker language we're not using any of the drag and drop stuff but before we get started with any of the actual gameplay and coding we have quite a bit of stuff to put into the game because i'm going to try to mimic everything as best as i can and so you can see here here is my layout for my room and oh let's that's interesting the tiles and all that but let's take a look at all the things that we need to create for this thing to work properly um so uh let's see the first thing i see here is i actually made in this part i'm doing i pre-baked this because this took me quite a while to get going there's a lot of tediousness and i haven't even added all of the tediousness to this yet but you can see i have three layers i have a my background layer which is using a black background i have an instances layer which i will describe here in a little bit i have tile layer and the tiles are actually drawn on top of everything else and so if i eliminate the tile layer you can see what i have going underneath here i only have these white walls and i created these walls so you say first thing off my room is 720p resolution i don't know why this styling is so anyway but the room is 720p i have the i have all of the graphics in here centered i have these uh whatever they call placards or something like that i have the side walls for the game i'm only doing this to make the game 720p because my students are going to need 720p resolution if i weren't doing something like this i probably would reduce it down i forgot if it's 480 or something like that or whatever it is whatever i got going for me so what i did was this is the actual level from miss pac-man this is the third level this is where the the uh pear and the banana come out to play for that game and so what i did was i created a wall sprite i made them 20 by 20 and i'm you know so every one of these sprites you got to make sure that our tolerance is our collision mask is set up so everything is in a good place but i used 20 by 20 sprite for that i used an object that is solid because we're going to be using move contact solids because you know that's my way of doing things but what's interesting is i didn't make them visible because they don't need to be they need to be solid and part of my game but i don't need to draw them because we're going to be drawing on top of them so there's no reason to spend that time and effort to have the cpu draw something that's never going to be seen so 20 by 20 and then i just you know i found the graphic and i just started working my way it was a little challenging let me show you that again it was to kind of get things going you know because it's a little you know say i got it working i got it i got it looking good but it did take some effort to get this to uh to be exactly like like the level was supposed to look like and so i put these placards immediately to the left and right and the reason why i'm gonna use this is i'm gonna eventually i'm gonna play with the bounding box on this so when you're moving and you collide with the bounding box it'll you know like pac-man can go all the way around to the other side i'm going to actually be using these to determine uh if pac-man needs to wrap around the screen horizontally so i and basically just to say you know just to show the instance grid and show everything by 20 20 by 20 i really did just go in here oh actually no i didn't actually i did it originally on the left hand side of the screen and then i shifted everything over to center it and so just that's why everything seems to be off by 10 pixels at a time and actually that might cause an issue later on when we go to add the dots but we'll see and say like things are not always easy like the drag and drop elements are there to help us but they're not always there to you know to make our lives perfect and so i just went ahead and just filled in and just basically just you know just use the uh use the tool to just put the objects in just you know put them in and whatever i had to do and then just put them in a nice order uh then i put the graphics for the graphics i have these just these are 720p height when it comes to this so it's the full the full height and then i just mirror image did or whatever i had to do to make it so that it looks you know it goes looks nice from the left side looks nice from the right side and there i have that and then what i did say the tile layer itself i went in you can see i created this image myself with my grand art skills you can see my you know you can see how great my art skills are there and so i went ahead and created all these different little tiles these are all 20 by 20 tiles and then i just went ahead and i filled in it didn't take too long i know you can do auto tiling and learn the brushes and things like that i did make an effort at that and things did not go well and for the moment i was like it'll take me more time to learn that right now than it is just to do this so i just feel i just i fiddled with this put the images in as best as i could and then i just went ahead and painted over and it you know it looks pretty good right so maybe it's a little more challenging to see but i have the lighter i have the lighter orange pinkish whatever color that is and then the darker color for the border and i just made sure that all the border it looks you know it looks like it's supposed to look all the way through and i say like so far i haven't found anything that looks out of place but the same thing goes with the graphics versus oh actually what this the oh yeah yeah yeah this is why when i do the grid this is why everything looks a little weird because i did an offset on the on the tiles to match up so that i'm drawing completely over the uh the wall objects themselves and so yes so you need to take the tile layer and shift the whole thing over 350 because that's what brings me into having having everything because you just say i remove it you know you can see i'm just going right over everything by doing things 350 over and having the instances shifted over you can't you don't shift them over in here but you use you use like the shift tool to take everything and move over and stuff like that that's how you would do things there okay so about seven minutes of talking blah blah blah in there and we are pretty much where we need to be start thinking about all the other parts upside i mean like that per se but now you know when i run the game it looks exactly like i'm expecting it to in 720p resolution and there you say there we go and i made with the 720p resolution and 20 pixels per vertically we get room for the top where we could put uh we could put scores and the bottom we could put how many pac-mans we have on reserve and then which uh which fruit is the current fruit and we're not gonna say we're not going to go too crazy on this but we're you know the word and my job is always to show you the basics and give you the uh give you the foundation to push forward and make things nice okay so there we go from there i'm gonna do pac-man miss pac-man first and so what i did was i found a pac-man sprite for all things this one is not hard to do i found this maybe not that sprite well you can see there's a lot of different images and i just pulled it in i can let me just to show you here there this this one right here miss pac-man and um so all good there and oh yep so just and i just brought it in and the thing was though because my sprites came in 32 by 32 which you would expect from an old video game i went ahead and when i when i pulled out all the individual images i i upscaled everything to 40 by 40. so all my pacman sprites here's my pac-man down i'm num num num num here's my pac-man up arms left here's my pac-man right here's my pac-man up all of them are 40 by 40. and for the moment i made i went ahead and i manually changed all the bounding boxes to be the full image we might go back and change that in a little bit we'll see how it looks when we go to put this into the actual game and so there we go there so 40 by 40 full images and then the one thing i did here was i did tinker with the frames per second so i don't have to write code normally i'm against that but as long as everything has to be consistent for now uh i think it i think it'll work for us 10 so 10 frames per second and we'll see maybe we'll have to fix that and then we can fix that in code if we have to that's what miss pac-man will look like while she's walking along eating all the dots okay so let's move on to putting pac-man into action and it's very similar to a lot of the other objects we've been doing especially with the move contact solids and that um a little different because as long as ms pac-man you know pac-man miss pac-man or pac-man doesn't stop in the middle of a corridor you have to basically it'll stop when it hits a wall but it won't stop in between anything else so you have to be careful with that and we'll find a way to do that right now okay so we need a pac-man object i'm using miss pac-man but pac-man whatever whatever you're using um not a sprite delete that and i keep forgetting everything's in alphabetical order all right so and i'm going to use the pacman right sprite for this get ourselves going here and so putting this object in the room not be overly challenging there we go nope it goes down here there and actually that works out nicely okay so it's all centered nicely and then if i run the game let's see how fast pac-man's biting i mean let's see that's not bad again maybe we maybe we changed that if you're not happy with it but for right now everybody's good alrighty i think that's the way pac-man starts the game i'm not gonna go look at it but we'll just say that okay and so we're working that out and then we're gonna say on create we're to say oops little thing isn't okay we're going to say image speed is zero so only when you move around will it allow you to keep biting because otherwise you'd be biting into the wall and they're like that's not how things go so this looks pretty good there there's my pac-man okay and now we can start applying step events and then just for getting the key presses going i'm going to set directions because you can go one of four different ways if keyboard check and i'm just going to use the arrow keys you can use whatever you want of course um virtual key if i press left then my direction will be 180. and i'm just going to use four and you can i'm not going to fight out how what do i do if two or more buttons are pressed at the same time i'm not going to worry about that just yet oh what i did nothing i didn't do anything wrong and i got to go back okay left and right obviously right is zero and then up and up is uh 90 and down is 270. and then all i'm gonna do here is say move contact solid and i know i have my move slide solid but since we're only going to be moving one cardinal direction at a time you don't need that slide solid function this will work just fine and i say direction and my pacman speed i'm going to put i'm going to put pacman speed here and so i'm going to set that up in the create event and what's going to happen here i can change this information but not this and so i'm going to say what numbers am i going to use i'm going to use 4 and since everything moves 20 by 20 the only thing you can do you can set to anything that's multiple or divide is divisible into so i can say one i can say two i can say four i can say five i can say ten and that's just to keep the math easy so that these when i do you know if i do this if i do one twenty times you know twenty times one is twenty two times ten four times five five times four ten times two whenever i go a certain number of frames i will hit and i will and i will move basically like the frogger game i'll move it one tile over so that i don't have to do any extra math to see if i have to kind of realign myself so that i can go then i can change direction that might be it outside of the animation there's pac-man up left up right left and there you go oops okay so there's a little something here if i'm going left or right and i try to press up i i'm stopped so i do have to write a little bit of code to handle that but otherwise everything is kind of in place you know like i should be able to press just right and then hold down down and then when i get to there i should be able to slide on in so there's a couple little minor details here that we have to work out but otherwise things are looking pretty you know pretty good here so i just have to add an extra check to every one of these and again it is tedious but the whole you know we write the code now and then we never you know you know set it and forget it kind of thing so if i do a um place meeting and i say i'm trying to go left so if i just try to move one pixel to the left or you can you know two just keep it you could probably move a whole square over but it's just a matter of just being nice about it you don't have to do too much crazy stuff and i say okay if oops with a wall is false then go that way and let me let's see about this i can so i go up oops i yeah you can't see of course bring this over okay so i go over to here and i go up and i'm holding left and there i go it did work i know it's hard you know it's impossible for you to see that but i'm holding left and it doesn't stop me because there's a wall there so it's just a matter of changing this little thing up a little bit in all the four directions so right of course is just a positive all right and then this changes oops this changes to be positive or negative in the y direction and this goes to be a positive in the y direction okay so now trying this out right works upworks downworks and left works so now i don't have to go crazy having with with skill i can totally work this out of course this isn't going to work yet we're going to have to work that out in a second okay i think this is i think we're happy here right pac-man is moving around she's looking pretty good when we get to the dots i'm going to add a little bit of gameplay that you've maybe never noticed about pac-man because let's face it pac-man is how many years old now and uh you are way younger than pac-man for the most part okay so i got that working for us and so what i can do here is um go ahead here now and let's play with the sprites and instead of hard coding this right now i'm going to go into the pacman object and i'm going to i'm going to say these are the sprites just in case you want to change things later on you don't have to go in and change i'm going to get so i'm going to say left sprite right sprite all this stuff left sprite equals and i call it pacman left spr okay left right up down left right up down okay so then let me just borrow this so i can because i always forget since i'm talking and thinking and doing so the speed is already set and then this is all good yep and it's it's unused so you don't have to worry about that so much and then go into the step event borrow this and say okay since for the brief moment i can say sprite index equals left sprite okay now i don't oops now oops do i don't need this anymore now that i know my failing short term memory don't get old folks all right so let's see i'm going right then i'm going up i'm going down let's see what that looks like now left right oops and i did forget about the uh setting the animation speed but at least the pac-man this pac-man looks good going left going right up and down oh yeah so let's fix that up and say image speed equals one just remember at least uh i changed everything myself so i don't so i can still i can make it one and not feel bad about it and i feel weird about it no there we go pac-man's biting now and like i was discussing earlier the only problem is that when i hit the wall pac-man continues to bite and i'm not going to worry about that for this video there's a lot of other things to worry about if you're that's a design issue if you don't like pac-man eating like that then we can then we can talk about fixing that up but if you don't really necessarily care then it's not a big deal but pac-man for sure now or miss pac-man totally walk around and do what she's got to do okay looking good so let's fix up this idea of the when we do this going around and so it's really those are just collision events for the most part here so if i collide with the left side i want to add some component to this and i want to say x plus equals and i'll say 480 let's just i don't know if that's right so let's just try it out if i go over to here so four eight probably needs a little more than 480 so let me just let me go ahead and duplicate this for collision with right side x minus equals and then let's say let's just tinker with this to kind of get the feel correct i just want to do this i just want to play with this so that i i can test both ways there we go and there we go so i'm thinking the right the sprite for the right hand side is different so let's see because nothing wasn't working and then my right hand side yep again everything gets screwed up here over and over and over again i'm going to have to manually go in and fix this up and just for the sake of right now i'm just going to go two stanzas oops 7 19 and 2 239 again just to test everything out then we can tinker but it's way too close right so i probably want to start setting this and change the sprite the bounding box of this so that it doesn't cover any of the black part so i just need to change the left i don't know what number oops wrong way sorry oh no on the the right one that's fine just not in too much oh jeez i don't know 20 30. i wish it i wish i didn't have black here be a little oh i could see it there maybe that'll work let's see what happens you don't don't want it to be too too close to there because you want to make it look like it's going off the screen a little bit okay oops that's not that broke doing that now oh well whatever i did the wrong i did i did the wrong testing let me go over here and fix this up oops excuse me sorry and so the right hand side for this is going to be daniel let's see bring it down to 219. let's see what that does but i want to make sure pac-man gets drawn behind it so let's just see what happens when we get over here yep i don't know did it do it again shoot hard to know so let's see um our pac-man sprites i didn't go ahead and change any of the uh i didn't change any of the uh any of the uh centering or anything like that so everything's upper left-hand centering so what i can actually do to make this right is play around with it so give me a second here i don't want to waste your time give me a second here to play with the settings and i'll tell you what i did to make it look okay so i got it all figured out here so what i did i went back into the sprites and i actually removed 40 pixels from each of the two sides so for this the left hand sprite i made it from zero to 200 for the right hand sprite i made it from 40 to 240 basically so there's 40 pixels on each side and then what i did was i moved the pac-man to be drawn basically above so pac-man will be drawn before these the pac-man will look to be kind of disappearing behind these placards on the sides while it goes around and so when that when it comes down to the code what i did was say when i went off on the left-hand side i just said okay when i finally touched the placard the bounding box for the placard i say oh go over to the right side find the right side object and get its x value and now you're going that way and it's going you're going left while you're doing it so you're just going to continue to move left and everything works going to the right was a little more challenging and this is to say okay when i when i go off the side of the right find the x value for the left hand object and add the sprite with and then because so you got to make sure you go past the sprite on the right hand side so those all of those things combined get you to where we are now and pac-man is except for one little thing here moving getting pac-man to stay still until you touch touch him or her and so now i go across and you can see there now it looks it looks a lot better it looks a lot more professional looks a lot more like it's supposed to be when i go across from side to side and it and say pac-man doesn't get you know the the black of the placard gets drawn above the pac-man on top of the pac-man character so we don't have to worry about it you know not looking good if you if you don't like that kind of thing it's a little tricky maybe depending on whatever you can play with the depth settings of pac-man 2. you could go ahead and just say if you know let's say depth equals and just use a positive one here that will also do it if you don't want to go ahead and change your objects inside of here and so i mean you won't see any difference but it'll say oops actually you will oh boy pacman's drawn at one i thought it would be the other way around let me try negative one that was interesting maybe they have changed i've seen they've kind of changed the depth settings before but let's see about this yep apparently no it doesn't matter what i change the depth settings to how interesting i wonder why that is so anyway forget that and for right now just keep it easy figuring out whatever and just keep the pac-man above the the side right and the side left inside of the properties window over here there's a lot of walls and stuff like that you could reduce that if you really wanted to if that ever becomes a problem for you but now let me just bring it back that was weird upsetting stuff i spelled depth wrong i don't know what happened i wasn't paying attention 100 i suppose but you can see that's back to where we expect it to be okay so the last thing to do or two things to do one is i want to make sure that when pacman starts you don't start walking and or moving until the character moves you so i'm just going to say direction equals i'm going to say i'm just going to say 45 and the reason for that is and 45 is the direction pac-man will never go and it will be modified the first time you touch these it'll be modified so as long as if direction does not equal 45 then i can move so this kind of fixes the issue i was having earlier with pac-man moving to the right immediately and now um what is going on um such place meeting place meeting i'm not keyboard checking not keyboard checking no keyboard checkings if direction does not equal 45 where the heck did i put oh my god where the heck is my code going didn't i put it in they put the wrong object they put in the wall anyway oh um i did not put it in the right place my bad it belongs in the create i said create i just didn't do it but there we go that should handle it now sorry guys so there we go pac-man is staying still not moving until i press any key and then then everything moves and i can't stop him and stop him or her anymore and now that's perfectly fine that i think that's perfectly legitimate thing to do pac-man can still go off the screen do all the things the last thing we do right now is we got to add the dots and it's good this is tedious so going ahead and it's tedious because of everything the offsets of things let me just make let me just try it out here let me just see what the room looks like and what we've got going for us i haven't created the dot object yet but i have a sprite it's 40 by 40 for right now and i did go ahead and collision mask this to make sure it's only the dot that's triggers a collision because you don't need it to be the entire area but we do need this area in some shape or form to make it easy for us to put this in the room so coming back here and say okay let me create that dot object with the dot sprite and try to put one in the room and see what happens you can see it's not a hundred percent correct it's everything because of the way the the shifting at least on mine everything went everything's off by 10 pixels so one way or another we need to fix that and just to make it easy and you're like well why don't i make it why don't i just set the grid settings to 10 like you could do that but then every time you go to add a dot you're going to be you're going to be it's going to be so tedious four five see what i'm saying five six seven eight nine and that's how you know that's that's where the object should be but you're like well what if i just went ahead now that i know this go back into my dot sprite and what i can do here is i can resize the canvas and shift everything over i believe i can do this 10 pixels so there we go there i might i'd probably have to go back in because of game maker being game maker these days go back into this and fix this up everything's been moved over so seven seven twelve twelve oops seventeen wait seven seven oh seven seventeen and 12 22. there we go there is our entire bounding box now and so now when i go to put these in the room let me put this back to its original 20 by 20 grid now when you put these in here it could be a lot easier for us you can see now i don't have to think as much and it's not it's tedious but les it's less tedious than it was prior so give me a couple minutes to fix up the dots you go ahead and do that yourself i will not make you watch and listen to me talk about dots while i'm clicking and talk about my life's philosophy or anything like that i will just bring you back here in a moment when i have everything set up all right all the tediousness is complete and this is the level i just went ahead and opened up what it you know what the original level looked like and i put all the dots around the only thing i'm not sure of and i'd have to go back and double check and and practice and play the game over and over did i put two dots in one space i'm almost confident i did because it's still tricky no matter what however you set this up but i have i have everything the way it's supposed to be going and so the last thing i want to do is uh it's not as easy as just collision destroy because there's actually a game play element in here where if you eat a dot you actually pause a frame pac-man actually does stop briefly for that one frame it eats and so that you know so it's one of those things if the ghosts are chasing you down you don't want to get in the way of the dots because then they'll slow you down while you're trying to escape so i'm just going to go ahead into pac-man into my step event and this before anything else here is where i'm going to put that statement and i'm going to say if this is basically my my collision event here if place meeting x comma y comma if where i am right this moment um is touching a dot so dot equals true oh and in this case oh yeah i forgot about that this place meeting function is great when i care about do i touch a dot yes or no but it's absolutely horrendous when it figures out specifically which dot of all the hundreds or whatever is there so i want to use the instance place function instead and so same the same signature when it comes to x y and dot but what i can say here if ins does not equal no one then then i did collide with a dot and what i can do here is i can do i can do a return just to make the return will kick me out of the step event and i'll never do any of this down here for that one frame so when i collide with the dot basically it's a pause the character will not hit this move contact solid so i don't have to do any worries and so then what i can also do here is say with or maybe i can do it now they've changed things up a little bit instance destroy imps so i can destroy that specific dot when i go ahead and do this and uh one tiny little extra thing i did change is apparently when i was looking at this pac-man and miss pac-man faces the left when it starts not the right so this is looking pretty good so let's see how this works you can see pac-man does go a little bit faster when she's not touching the dots you can see you could it's if you're playing it you can feel it you can feel that slight reduction on the speed as she's going around and doing the things she's doing so this isn't things aren't perfect per se like maybe we should go ahead and change the bounding box slightly i'm not going to for today but i shouldn't be able to eat that dot i can't point you can't see me pointing but i shouldn't be able to eat the dots like that one right here the one that was right above here because i didn't i technically didn't touch it so what i would need to do is just to tinker with the bounding box a little bit and i can use a collision rectangle to account for that rather than a well i'll just do it let's just say let's just do a co instead of instance place i can use collision rectangle to rectify the situation i just have to it's more complicated though so i have to say collision rectangle okay so i have to say my pacman is at x y but maybe if i say maybe x plus eight or yeah x plus eight y plus eight but that is the upper left hand corner of the the the collision rectangle i'm trying to get get a hold of here and if i say x plus 32 y plus 32 remember my guy is 40 by 40. so this basically kicks out 8 pixels in each of the directions and then i still want the dot do i want precise no and do i want not me no that doesn't apply true or false probably would work here and so now this should take care of it should maybe maybe not my luck has been decreasing for a while now and there you go and now you see that that dot doesn't get eaten even though i go i'm going around so we just have to test it to make sure i didn't screw up in both up down left and right directions but left and right seems to be working because that dot right above me and right below me are okay and so let's see we can try up here and down here and say yep the dots get eaten perfectly fine i slow down perfectly fine as i'm doing that and the dots don't get eaten you do have to go ahead and do that so that works for it and if you really wanted to tinker you could play with figure out exactly how far to modify that the offsets to make this thing work but there's really no reason to worry about that we have it working with using this collision rectangle that kind of you know it adds some precision to that collision and it and we we're dictating the collision rectangle and not game maker when it comes to using the actual collision detection okay so pac-man miss pac-man she animates he eats dots she go moves around she goes off the screen at least for just this basic bare bones demonstration today everything seems to be working fine so now we're ready to go on and start adding ghosts and i'm going to add one ghost to this for now and then just to show you how just to get the general ghost in here just to make sure it's working give it random movement and then later we will go ahead and look at how the real ai works and give four different ghosts four different personalities and show you how that all goes so i win my level while i talk but now you can see that we're looking good here i am ready to move on all right so you can see the ghost here i'm gonna save all of the heavy lifting when it comes to setting up animations for the ghosts until after i get this thing working with getting the ghost to move around and not have to worry about things like that and like getting stuck and things like that so i just used the blue ghost animation that i have from my sprites you can use whatever excuse me excuse me whatever you want and so i have a sprite i have them in there and then it's kind of similar to the way pacman works but kind of not and so let me go through the code i have the code here and so on a create event oh let me stop this okay so you can say just like before our speeds need to be uh divisible into or you know divide evenly into 20 or 40. so these are very good numbers and just just to make sure everything is safe i set the speed to 2 and i said direction is 180. so when when this when the game first starts up the ghost should move to the left at 2 pixels per frame and so and i used the same exact code as the pac-man object for collision with side left collision with side right so then when the character finds the edge of the screen and goes off it'll wrap around horizontally just like pac-man will and so the step event this is where things get a little a little iffy here so let me just talk to you about what is happening and let me adjust my code since i got this thing working forgot about this and so what happens is we have to test and say we could make this more efficient but rule number one is always get it working and then rule number two is if you care make it work better so right now i'm just checking on a frame by frame basis since this is a step event i'm saying hey let's try to see if there's an opening above me below me to my left and to my right and so i'm setting up an array of choices and which is empty to start and i'm saying that i off the bat i have no choices but i should always have at least one choice which is you'll you'll see as things go you would think it'd be behind you but if i just randomly pick behind me half the time i'll go and it'll and i had it working earlier and it was just the character would just go back and forth and back and forth and back and forth the character never truly really moved anywhere because of all because you could just go left then right then right then left and right right right left left right left right left and so it didn't really play out very nicely and so all i do here is for each direction so notice this is you know this is the ultimate one this is checking to go to the up this is checking to go down and they're all the same slightly different so let me just go let me just write this let me just comment this in here 180 is left and 0 is right and that's why i have the first if check in here i say because i because if i'm going up i can't go backwards that's one of the rules of pacman is when i hit with the decision points and all that the ghosts pacman can all the time but the ghosts cannot go back from where they came they cannot just do a 180 and go backwards so this is this is how i make sure that the ghost doesn't do that i say okay if the direction is that the the ghost is going right now is anything but down then i can go up so that means so that's perfectly fine when i hit at you know so if the direction isn't 270 then cool i can do that and what i can do is check to see if there's a wall above me x y minus two wall and if there isn't add to the array and that's all this is doing is saying okay choices at the zero location is going to be 90 degrees and i'm going to increase the choices because i have now now i have one choice and i'm just going through and adding all of the different directions using the same logic if i'm trying to go down well i can't go back up again so if i'm you know as long as i'm not going up i can go down and i just check now below me y plus 2 and i add it to the list you can't really hard code it because you have to because you could have technically i guess if you want to think about it anywhere up to from zero from one to three choices as long as you don't have any dead ends in your room the character will be able to move on to and pick a choice because when when the ghost gets to here don't mind the the difference here with the but if he gets to here and it's going this way you can choose one of the three different ways because there i can go left i can go up i can go down but i can't go i could technically go backwards but i really can't because i just ruled out that i'm not allowed to do that because i'm already going to the left i can't do a 180. so that's all that's happening here just on a case-by-case basis just checking on a step-by-step basis as well which of the four directions i can go can i go up can i go down can i go left can i go right and then i'm setting the direction to one of those choices i'm using i random of choices minus one because you know if there's three choices that means that they're in the array it's 0 1 and 2 to choose from and i random is an inclusive choose function or an inclusive random function so number of choices minus 1. if you forget this minus 1 your game will crash because it'll access an array element it's not supposed to and game maker will have a fit about that so it sets the direction right now i said in in a couple minutes we will add the actual final ai to this but right now when it gets to a decision point when it hits like an edge and go well should i go up or down or left it'll pick one of the available options randomly and just and then just continue moving in that direction so this is what happens with that code in place and i'm not going to sit around forever and a day because this thing is completely random oops i got the ghost i've got the ghost uh on top of or below the dots is that cool or not i don't know it depends on how you think about it right if the ghost is see-through partially see-through but we can fix that but not right this moment but you notice it hits the decision point it never goes back and there it goes around and it it'll take an hour for the thing to get back over here i already tried it out earlier waiting and waiting and waiting because the random number generator is exactly the same uh the same values every time but you can see the ghost continues either continues on it depends on the choices right there's either there's either two choices or like it gets to here there's one choice if he's one either way it can only go left if he's trying to go up and he cannot go down if he's going to the right excuse me because the direction you came from is not is not allowed there really as much as you you're going to will it it's going to take forever for it to get over here but it does come wrap around just like the pac-man character does so that is the thought process oh what in the world how did pac-man i don't even know what i okay that's a bug we'll have to fix that that's an interesting bug what in the world happened i must have hit the button at the exact wrong moment and just got shot out of here or something i don't know but and i can't move at this point pac-man is stuck but anyway but that's the whole thought process figure out which directions you can go and then it's real easy if there's only one direction to choose from but it gets to be a little choosier if there's two or more and that's where the ai comes in which one will it choose so that it's it follows whatever we're trying to do is it's smart is it timid is it is it scared you know like what what is the behavior and depending on which direction you choose you can totally get that behavior because if you played pac-man enough you know that the ghosts have distinct personalities but again what's interesting here is that the code up to this moment in time up to there we don't really have to change this we may have to change the order of operations depending on which you know which directions win out when there's like a like a tie or something like that when it comes to you know like if i want two different directions and they both get me to the same place at the same time but everything up to there should be almost a hundred percent the same if if not the same and it's only when we get to this line of code that we have to change our thought process to do more math to figure out a better way to choose than randomly so that's where our the final step of this is going to go okay it's time to push forward a little bit now we have a random ghost it was blue a minute ago i spent some time and i turned them into a red ghost i'll describe everything but other than that the ghost pretty much does the same kind of stuff i did end up having some issues with the ghost moving around let me just let me just kind of go through a little bit of everything here so i created the sprites for the ghosts again everything's 40x40 i have two images two animation frames for the down and the left and the right and the up all four directions 40 by 40. i went in and i changed the collision mask at least for right now to be full image and we need that for the movement maybe not for collision with the detect if we eat pac-man or what but for right now i'm not worried about that we'll have to get we'll cross that bridge when we get there but just like pac-man has the four animations so does the ghost and then i also changed this maybe i did maybe i didn't depending on it i changed it so much maybe i put it back to how it was but i changed the sprite or the the bounding box settings 0 202 0 7 19 for my version of pacman and if you're not using the exact coordinates as i am for things then you don't really have to worry but i was getting that weird pac-man bug and i was getting bugs where the ghost got stuck in walls and things like that but by fixing all this up i think i've pretty much alleviated those issues same with going uh slide this the rights the right sprite here where i just change things up and i start at 38 instead of whatever i might have been changing whatever it might have been before so those sprites get added in i'm not using that blue the blue ghost the eaton ghost anymore and so now i can actually go into the objects and i i say because i changed the this here i just said forget it i'm because you know it's one of those things where i this is this i've been working this video over a couple of days now and i do this and that i come back and i forget what i'm doing so for right now i just hard coded instead of having my my my collision events from earlier i just hard coded my values and i say okay if i press the if i hit if i collide with the thing on the left just move my x value to 9 29 and what is that where's all the where's the room for this hello come on there we go so like 929 is right here right on the edge right on the edge of a boundary here so and then with this it it won't collide because i changed the bounding box and everything moves so i said when you're when you're seeing the game in action it hopefully doesn't take too long to get some one of them to cross the boundary you will see that there's eight of them for god's sakes come on let's go and nope 50 50 shot nope every what are the odds 55 there it is and you can see them going across and the same will apply going to the right hand side i'm going to wait i'll wait 10 more seconds at the most here come on getting it nope getting there nope that's the problem with randomness right so it's it's it's fun but nope nope nope nope anyway so it just i use the value for the other side of this if you collide on the right-hand side just say the x is 311. the band of course and so the ghost i did the same for the ghost i just changed that up so that the x is 929 and the x is 311 and then what i did here for the ghost is in the create event i added an array called images and the reason i did this is because this is just one ghost i'm this is the red ghost it's the easiest ghost to do the ai for so that's why i picked it and and so i said here are my images this one faces right up left down because this is the zero direction this is 90 this is 180 this is 270. if you take those direction numbers divide by 90 you get 0 1 2 and 3. so this so i'll be able to do this very easily when it comes to just figuring out which image to draw at any given time and you saw it already works because i already proved it to you i showed it are you working and so uh the only the thing is i also played with depth settings so that the ghosts and the pac-man are always on top of the dots especially the ghosts and then i also made it so that the wall or that the wall the the side things are above every or below everything else i'm saying above everything else which one was it depth negative three whichever it is you can see well when you run it you can see that the ghosts don't really look like they're but they do look like they're over it maybe i just i put things in the wrong order but again that's an easy fix if you play with the depth settings if you like it this way then you don't have to touch it but if you want it the other way you can go ahead and do that too anyway so just just saying so what i'm doing here outside of the depth settings is i'm using this images so let's see red ghost step event and so down here i just added one piece of code and i also added an if check around what we were doing earlier because this shouldn't it's one of those things this should never happen where the number of choices of which how many directions the character can go in any frame should be zero i'm sorry should be above zero because as long as there's no dead ends there's always a direction for the character to go but however earlier on i was having some issues and the game was crashing so i just put through this around here so if you end up with a bug situation you don't change the direction so the character especially if they're outside the bounds if they're you know wrapping around the screen they don't have to worry about moving so i just and then this is all i have to all this is all i added here and i just said okay take my direction divide that number by 90 because it's going to be any of the four cardinal directions apply that to the images in array and supply that as the sprite index and as you see it works that's all everything else is exactly the same as we've done before and so just one more time going with that you can see that the the four ghost sprites now that maybe pay attention a little more you can see that the eyes are facing the direction they're going either right up left or down the sprites look like they're supposed to and i can i can move around and do the things i'm doing and the pac-man goes off the screen perfectly normal and you can't you can't ruin it like you could earlier i don't know what i was doing to ruin it but you can't ruin it like you could like uh earlier so now so that is how i got the red ghost going at least kind of exactly to the same place as the blue ghost because there's it's still random but the one thing you might notice here on the pac-man character miss pac-man here is there's a red x and so the final thing i did for our ghost is to give the play or give you as a developer the idea of where this ghost is trying to get to at any given frame i took this over i said draw self draw red a jaw draw a red x on pac-man's position and that goes for every ghost so every red ghost will be pointing right on where pac-man is because for you know this ghost the ai is supposed to be this way and how do i know the ai is supposed to be this way this video right here by retro game mechanics explained is exactly what i'm talking about this wonderful 19 minute video pretty much tells you everything that went into the design of the ai for the original pacman game and so i'm not going to go into the details that's up to you i will explain the details but i'm not going to go through with the reasoning behind everything but just to say that at 6 42 they describe the red ghost here and how the how it's going to work he's got the simplest way of doing it and basically the red ghost is just trying to create a advertisement for disney plus and so i can skip my ad here and come back and you can see that the red ghosts are always just trying to latch onto wherever pac-man is and trying to find their way there so there's a couple other rules along the way but let's get this red ghost from being randomized to finding the best path to pac-man no matter what so another little detail here is that we were i changed this up on the red ghost and according to the rules just if you watch the video we try to go up before we try to go left before we try to go down before we try to go right so if there's a tie like if we're trying to figure out two different ways to go and one of them and two of them tie then this is the tie breaker so if it would the up will go before left and so forth and so on and so this is i'm taking i haven't touched the code just yet but but basically instead of running through this what i can do is set up a couple more variables and i can say new direction and i'll say equals negative one just in case i don't remember down below i talked a couple minutes ago about the fact that this number of choices could be greater than zero and i don't actually need this anymore these choices and the number of choices because this new direction is going to handle it for me and then the distance to pac-man is going to handle everything else and i'm going to make this a just a hugely large number there's no way this you know just to make it so that when i try at things i try to find out how far away i am from pac-man then everything's going to turn out fine so i can get rid of this boom let me just get rid of this in all four places we're going to replace this with the new code and if the new code works out everything should be happy okay and then and then down here to say if new direction i'm sorry if new direction does not equal negative one instead of using choices random or whatever we're going to say direction is new direction if this if this works out properly here right oops if equal if does if new direction does not equal negative one it's so we're kind of working our way in here and so this will break of course if i try to run this in the moment or will it no let's see nope doesn't break but all the ghosts go to the left because that's how they started and now they don't know how to do anything else because we broke all the code we took all the code from inside of here and figured out what was happening okay so now what i want to do is i want to if this is up i want to see how far it is from pac-man to the ghost if he were to go up one square basically is what's going to happen here so there's a function i'll call it dist here and it's called distance to point or let's see distance two point or just um let's see point there's point distance i want a generic i want to use it generically because i want to say okay my i'm going up so my x value but my y value minus 20 pixels that's my value but how about pacman's x how about pac-man's why and then is there another one nope that's it and i say okay so how how far is it from pac-man to me if i went up one square because that we're just testing to see is that going to be is that going to make it an easier time to get to pac-man if the distance and it's just a as the bird flies as the crow flies kind of estimate here and you know you could tinker with this i think this is going to work i haven't i'm doing this one live but i say now if the distance is less than the distance to pac-man then my direction is going to be remember it's the opposite here so my new i'm sorry my new direction is going to be equal to 90 and my distance is going to be equal to this dist distance the pac-man is going to be equal to dist and we're just going to pop this in in inside of those blank if statements four times now because we just have to try each of the four directions try going up and if you can then if if it makes you closer then go up then try to go left the left means to try to go negative 20 in the y direction and then this would be one 180 and then the same with dist and say tie breaker it has to be less than less than or equal isn't going to work so just trying this another time with 90. this this should make it 270 if it works but if this is going down this is going to be a plus on the y and then the same just to finish up here if i do this right this should be if i'm going 180 this is a minus on the x let me make sure i did plus something oops minus x try to go left is a plus here sorry about that okay that's the fun of this is you'll you'll know right now right away if you screw things up let's see let's see up is y minus 20. left is x plus no left is x minus 20. oh yeah i'm talking myself into a problem here down is y plus 20 right is x plus 20. i don't left and right what's the difference right and so if it's not 180 it's gonna be a zero and let's see what happens i got eight ghosts coming after me now here they come they got me they're all following that pattern and you can see now they're in a they're in a holding pattern here because they're they're finding the best way to get to me on every game frame here they come now they got me and then they're coming around again how do they get me coming up let's see wherever i go they will get me now instead of being completely random the ghosts are they they got me oh that one don't so now just wherever i go go come get me guys there's no way out here they come they're gonna get me no matter where i go so that's that's the red ghost that's the easiest ai is because you know it's exactly they're trying to go exactly where you are and there's no way out there's no way you know you can just run away but you can't hide pretty cool stuff right so let me go back to the code one more time just to make sure you guys have that so we can push forward and we can start generalizing this because now that we have this code it's really just a matter of figuring out where the pacman is right here and then figuring out where i am i'm trying to go and then the code is going to be pretty much similar pretty much the same inside of here it's just the ghosts are going to be slightly different when it comes to that okay so here it is so new direction is negative one i maybe i don't even need new direction i could just set it but i'm not going to worry about that but it's just to say what we're changing inside of here is to say okay if i try to go the direction i'm going will it give me a shorter path than anything else that i know up to that point because the whole point of this is to find the shortest path you know pathfinding kind of algorithm give me the shortest path from point a to point b and on a frame by frame basis and it knows if i try to go left or right or up or down it knows so again if i'm going up minus in the y if i'm going left minus in the in the x i'm going down plus in the x uh yeah plus in the y and i'm going right i go plus in the x okay so that works out for red ghost and now part of this is the tediousness i'm gonna set up all four ghosts and get all their graphics going and all of that stuff and we're gonna set up all four of the ais we got one down and three to go okay so i went ahead and i i got animations for all of the pink ghosts the down the left the right and the up again all 40 by 40. oh boy i do have to fix this up game maker trying to be nice to me zero zero thirty nine thirty nine i gotta use the full images again for everything just to make sure everything is pixel perfect and we don't have any issues with the movement around the screen 0 0 39 39 all four of them and it's the same basic rules as what we've been doing we're going to change things up just slightly uh zero three oh that one's fine zero zero thirty nine thirty nine zero zero thirty nine thirty nine zero zero thirty nine zero and zero zero thirty nine thirty nine okay so now i can go ahead and duplicate the red ghost and it can be pink and i can go ahead and just give it the the new pinky sprite and give it the left eyes to start in the create i can go into this guy in a chain and just change up just change it to pink for everything and at this point even if i just go ahead and i just go in here and i replace half of our ghosts with pinky ghosts the graphics everything should be the same ai wise right it's just um the only difference should be the colors of the ghost we can test this out working i thought i crashed it or something so here i go so let's see down right down left up okay and so right now the or the the pink ghost and the red ghosts are happy in their little world the graphics see how quick it was to change up the graphics wasn't that wasn't pretty slick that's all it took was a couple you know just a couple seconds to change up the graphics on that at least when it came to the code getting the graphics from the artist that's a whole other story but anyway so coming back so we're good now and so now it's just a matter of figuring out where the where the ghost is searching on for the pac-man character and let me show you how to do that okay so for the the going back to the video right around the seven minute mark they describe what the ai is and for the ai for the pink ghost is the pink ghost is always trying to go basically four squares ahead of wherever pac-man is so if she's facing here then you're supposed to like one two three four they're trying to go here and it depends on where pac-man is facing left right up or down and if you watch the video you'll see that there's either a little bug or just a little something in the code if the if pacman is going up facing up you go four up and four over to the left and that's just that's just the way it goes so that's the target position but all the other code stays the same like try to go left try to go right try to go up try to go down but instead of going against pacman's position what we can do here is set up an array of stuff here and i can say okay um i'll just say i'll say i'll call this x direction the x directions xdirs um array equals and i say okay and then i'm going to use ydirs for the directions it's like so i don't have to write a bunch of if statements to handle this i could say okay in the zero direction to the right you're going to like pac-man's going to go 8 positive 80 in the x and 0 in the y if i'm trying to go to the right i'm trying to go up i'm going to do like i was describing before whatever for whatever reason up is negative 80 and negative 80 for y for both x and y that's where pac-man or that's where all the ghosts are trying to go and so for for left you're trying to go minus 80 in the x and you're trying to go zero in the y and for down you go zero and you go positive eighty you don't have to use the plus signs i just i just have it just in case okay so that's how things are looking here and so what i want to do is figure out from this what direction pac-man is going and then figure out some offset and i'm going to i'll say bears there here there there so just put all these i did just so these are local variables and not member variables and so what i want to do to finish this up here is just let me steal this first just to prove this is working and let me go into the draw event for this make sure this is pinky here okay in the pinky ghost and so here we go so i have my x direction y direction and so what i can say is i can say var dx equals and i can say x dirs and then kind of like i did i showed you earlier i can take the pacman's direction and divide it by 90. and i can do the same for the the dy and use dy pac-man direction and now what i can do here instead of this is where what i'm this is where i'm drawing the thing but instead of drawing it halfway halfway i can add this dx value to here this offset and add this dy value here and let me oops did i change in no and then what i can try to do let me see if i'm allowed to draw a set color let me see if there's a if there's a c pink there's no c pink oh no and so what is pink but i don't know what what is pink let me look that up and i will get back to you in one second so i am told that this is pink 254 184 198 when it comes to rgb values and so i'm just going to say take pic pacman's position move over 15 then use the dx value and then use pac-man's y plus 15 plus d y and put an x there and you will see when i get this going and it will come out correctly you can see the red is always going to be right on pac-man and and as i was saying before if i'm going right it's a head of pac-man four squares if i'm going left it's four squares down it's four squares but only when i'm going up is it four squares up and four squares to the left but other than that i just all i have to do now is change this up as i said this works i these are correct values so now all i have to do is go back into the step event and use these things over here kind of like i did before here let me go let me go in one more time and steal these these two lines of code to figure this out once you have this working you can eliminate the draw event so you don't like i don't have to worry about it making like be efficient inside there because i'm going to eliminate that in a little bit anyway but over here i say okay there's my exters there's my widers and so now all i have to do is just go in here and just say plus dx plus d y and yes it's a little tedious but we'll copy this the rest of the way because the other the other ghosts basically kind of do this as well plus dx plus d y all four of them make sure he does plus dx plus d y and so you can see how the ghost has a different personality they're not necessarily trying to get you but they're trying to get in front of you and so the one ghost is trying to get you one is trying to go in front of you like to block off where you would think you would want to go next so okay so here we go there so let's see they're trying to get to that tile up above see how the the orange ghosts i'm sorry they're fine i keep calling whatever you see the pink ghosts keep going around because that x can you see that x in the corner there that's where they're trying to get to so like in this one it's going to be all weird because the pink ghosts are trying their best to get to that x that's in the pen so they're going to do all sorts of crazy stuff to get there whatever it takes and so now maybe now they'll circle around it and the red ghosts are on to me because you know they get to me and then they're they double back around next time around so that's pretty cool we got our we got two of the four ghosts here the ai and we we can see on the screen maybe use a font to show a bigger x or something but for right now i think i'm happy with that to know that that's what's happening here so all the pink again all the pink ghosts don't actually try to get me they just try to get to that square below me so the pink ghosts oops that one got me there but they but in in essence it might be it's more of an accident if they get you because they're not trying to get you they're trying to get to the square ahead of you so we got two ghosts down two to go all right moving on to the orange ghost apparently i'm learning that one of my monitors colors are completely different than the other because the orange does not look the same but that's a weird yellowish color over here on my monitor but i did the same thing i have the ghost down the ghost left the ghost right and the ghost up sprite i had to change all the collision masks 0 0 39 39 everything's upper left hand corner centered two frames of animation every one of my ghosts is running at 10 frames per second maybe even pac-man is no pac-man's running at 30. and again you can adjust these things as you see it but i've got my orange ghost and what i did this time was i duplicated the red ghost because most of the work here if you know you know looking at that video is using that as a as a placement here so i just took the orange ghost i updated the images to be orange ghost right up left and down because i just created them so in this case the orange ghost would would work exactly like the red ghost except the colors would be a little different because it's using different images but as of right now the code is identical you know the code to actually move the character around and the only difference here if you go and look at that video again right around the same point as the other thing as the other ghosts is that if the if you are within eight tiles which is like basically 160 pixels of pac-man you will try to jump right in on them but as soon as you get that close you get more timid and you try to you try to move what they call in the in the video into scatter pattern so i just have to go ahead and just fix this up up here and just say my this dx value is going to be pac-man's x and then my d-y value is going to be pac-man y hey man okay and then i just have for the moment i just have to go in here and just change this to dxdy so right now nothing is really changing i'm like at the end i'm called i'm doing what's called refactoring i'm changing my code and at the end of the day even though i changed my code the behavior ends up being exactly the same if i run this just to make sure then the orange ghost should go exactly like the red ghost follow right behind him and the pink ghost don't have to worry too much about he'll go around in circles do what he needs to do so after that little change everything is still good i said the pink ghost does what it wants but the orange ghost follows right behind the red one because they both have identical code to get me to get them to the same spot and so i'm good there so then all i need left here is just to say if let's let's see point distance what's the distance between two points and if it's less than 160 pixels we'll fill in that gap there in a second then my dx value is going to be something my d y value is going to be something and i just have to look at my game and i go that ghost goes down to this position down here so something like this let's say three 350. let's see let's just use this corner point 350 680. oops 350 680. keep talking to myself 350 680 50 60 80. and so what's the dis the point distance the point distance is since i'm in the orange ghost now i say okay x comma y comma pac-man comma x come a pac-man comma y how much distance is there between those two points me my position and the pac-man's distance distance and if it's less than 160 then my what i'm trying my my goal tile is the lower left-hand corner and that's just going off what the video tells me and if this isn't true if the distance is further away then this stays put so let's see let's see if this changes anything here orange yep it already does orange ghost is doing different things he locks in on me but then he gets timid he does not go at you see how he's not going after me because he for that brief moment he's going after me but then he goes the scattered pattern because he's trying to get down here and then when he comes back around he goes oh i can actually get closer to pac-man but then up can't do it and so so all the ghosts so far are getting different personalities it's pretty cool right so three down i saved the more the most complicated one for last it's not i mean it's it's complicated but it's not crazy complicated but now you can see let's see that that orange goes oh i just needed to draw the x let me just get let me draw the x on him here go in here let me change this up so it's a little easier to see i think i can get orange i think orange is a color here yep orange is a color and just for this i'll just move it over a little bit on this on the screen so we can see it maybe maybe you can see it maybe you can cause pac-man but the orange it's there and then once you get close there's this oh never mind i need that dx and that d y okay here let me go back uh duh right silly me silly me i'm just just for the sake because again when we do this the dev code generally falls away immediately or at least pretty close to immediately so once this is all said and done and i'm happy with this i can erase all this code this does not have to be proficient efficient in any way when it comes to what's going on here um dx comma d y and i'll just say dx plus 15 or i'll just leave it alone and okay so there's my or you can see now you can see the orange and you can see it moving it's trying to get pac-man but now he's trying to go back to the lower left-hand corner trying to get pac-man trying to go to that lower left-hand corner and so it's it's pretty cool that's why the ghost you know looks like he's coming after you but then he's like oh maybe maybe i'll rethink it but again there's only so many paths these ghosts can go so it makes sense that they would you know depending on where you are and depending on certain factors would go ahead and uh and still go over pac-man even though they're not directly trying to do that okay so one last ghost that little blue ghost and we and i'm gonna call it i mean you can do all the scoring and lives and all the other stuff that that's that's something that you guys should be able to at least start pushing your way toward okay so let's do the blue ghost okay so i finished everything up for the blue ghost again the same thing goes forty by forty zero to thirty nine zero to thirty nine ten fps two animation frames there's my blue goes down there's my blue ghost left there's my glue blue ghost right and there's my blue ghost up so the only time this time this is the more complicated one so i'm going to go into the blue ghost now go into my create event and change up i say all my images are changed the blue ghost blue ghost blue ghost blue ghost and then my step event this is where things are going to change so the rule is we got to look two tiles ahead of us so i'll say 40 instead of four tiles it's two tiles so each tile in our game is 20 by 20 instead of 8x8 like the game did because we made everything larger to accommodate uh just the the larger monitor larger screen and so this this does not have to change what is my x position what is my y what is my direction and so this fixes up and says oh the dx and the d y so this this is the same rule two tiles in front of pac-man instead of four so then d dxdy right now dxdy is however you want it is two tiles ahead of pacman okay so then what we do is then we we figure out a line we draw a basically draw a line from i'll just call it d dx and i basically draw a line from [Music] red to the red ghost to that spot the dx so it's essentially red ghost dot x minus it's always from i believe you know don't hold me this 100 since it's been a while and i'm just talking and it's early in the morning for me that the red ghost 10 o'clock but anyway kids been up for a while so rope red ghost.x from the end point to the beginning point something like this and then the y position would be the same thing where i say okay take my red ghost subtract off my stuff here that's my dy value and and then instead of adding this thing i should subtract it and if i'm wrong i'll be off by a little bit but we'll try it out so dx minus equals ddx and dy minus equals i'm sorry the other way around d dx minus equals dx and no dx minus equals ddx and dy minus equals ddy and i just have to go in here and instead of adding this thing to pac-man that that is just the position i'm trying to get to is that dx and d-y kind of like i did before for the other so it was kind of like a little hybrid uh hybrid code here it was kind of like the red ghost kind of like the the pink ghost so that's and but you don't want to have to write that out every time yourself so that's why a little copy paste as long as you kind of have a feeling for where you're going with things saves you a lot of hassle hopefully this saves a lot of hassle uh let me steal let me borrow all of this code so i can do the draw event so i can see if that's exactly what i'm supposed to be doing dxdy let's see where this goes and i'll use blue color instead of whatever it is that was pink let's see what happens and if it doesn't work then we'll figure it out okay so let's see blue ghost where is that blue x hmm yeah it's not working a hundred percent like i expected so i'm going to that blue x isn't anywhere on the screen so i'm going to do a little research here and i'll let you know what i did wrong and then we'll fix it up and then blue ghost will be doing what it's supposed to be all right just a little math i didn't write it out so if i wrote it out it would have been perfect the first time so here is the step event this is the code that we're discussing here the dx and the dy value is what we're what we're trying to get to so we again two squares ahead i have my arrays this time around i changed this up so i could say okay the target dx dy the point the target point dxdy is my pacman plus that offset so i can get to the exact square and then what i do is i figure out the line from that point dx point to the ghost's point and then i basically reverse that 180 degrees and that's instead of adding this value to the dx value i subtract it off and so what happens here is when i run this with this code in this place is you can see that the blue ghost you can see the blue x and now you can watch as things are going the blue ghost basically flanks where the red ghost is i mean it's not up because everything is in flux but you notice that basically wherever the red ghost is the blue ghost is trying to get the complete opposite side of it wherever pac-man is facing up to let me just let me move this two squares ahead here let me get this to there so you can see now let's watch this here the red ghost is coming around and then the blue ghost is basically on the other edge of that and so pretty cool right my only question is when it comes to this is oh because it's two squares ahead here and then pack me yeah so it's like it's it's interesting but it's working it might not be a hundred percent perfect but i'm you know i'm trying to get perfect ai but you know at the end of the day this is uh this is looking good and it's in the behavior is what i'm looking for in this ghost is to kind of come after me but not necessarily come after me directly because it's flank and then basically come after me if it's convenient to do so but as you can see here with all these x's and everything like the target tiles for all the different objects you can see everything going crazy here you can see how everything changes the blue ghost is pretty much the only one that changes continually because it's the only one that changes based on the result of every other of the red ghost everything else is basically based on where pac-man is in relation to it and but that's it i'm gonna call it a day here on this video since uh oops i gotta make sure where are all the oh where are all my ghosts there's two where's the blue one did i lose the blue oh i don't even pay attention did i lose the blue one off the screen somewhere open up there it is i don't know what happened that was weird but anyway but i think it's a pretty good demonstration about how to get you know with you know with a little bit of ingeniousness and a little bit of hard work we have an ai that we could be happy with and we could say that you know we pretty much accomplished what pac-man did way back in the day i don't know why they get together and they can't get them apart that's the only thing that's weird to me there we go but anyway same with the blue ghost but can't get the blue ghost away from that one no i think it's this one but anywho i don't know where i'm not paying attention enough to see that just see it seems like it disappears out of nowhere when it does i'm not a hundred percent on this like i'll have to pay a little more attention but anyway if you have any questions or concerns as always sword b at cod.edu is a great way to get a hold of me comments here on youtube if you know correct me if i'm wrong i mean this is a long video this is a complex video i might have done said something incorrect or done something incorrect i'll correct it just let me know i'm not infallible good lord good oh my goodness i am not infallible i am very fallible so thanks for sticking it out with this video the next video i'm going to make is like a zelda style adventure game so i'm going to show you guys how to you know change cameras and and have weapons and different maybe if we get to it we'll have ai states for characters so they can chase you down and then they can retreat and things like that we can we can introduce more complicated ai into a battle mode with some zelda elements so anyway thanks everybody for this for sticking with this video have a great day and we'll see you next time
Info
Channel: Bradley Sward
Views: 2,632
Rating: undefined out of 5
Keywords: GameMaker, Studio, Game, Design, Development, Collisions, Timers, Alarms, Movement, Animation, GML, Igloos, Pac-Man, AI, Ms Pac-Man, Ghosts, Chase, Dots, Wakka
Id: z9oVSM40N1I
Channel Id: undefined
Length: 88min 8sec (5288 seconds)
Published: Sat Nov 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.