How to make an enemy AI agent for your top-down games

Hello everyone! Today we are going to talk about Enemy AI! At the end of this tutorial, you have made an enemy AI Agent for a top down game we've just made!

But what is AI, and how will this AI work? AI stands for Artificial Intelligence, and it basically processes an input, and decides on an output. If we made some kind of Monster AI for a game about Monsters, then we'd make the AI of the monster, say for like, guard the door and monitor any changes in their behavior.

Let's say that the monster sees that a door is closed. If the player were to say, open it, then the monster runs towards that sound. If the door is opened, the monster senses that the player is present, and will start hunting the player down in rooms connected to that specific door.

So now we know how AI works and how we'd implement them into our game! But how does OUR AI work?

Our AI works by this: First, when it catches you, it places a marker. The next run, it goes TO that marker and sees if you are there. If you aren't, it searches for you and hunts you down. Simple enough? Then let's go make an AI for our game!

Start off by running Snap! by pressing the "Run Snap!" button on your screen. It should be at the top banner of the page.

Next, you are going to make a simple map for your game. I've made a maze.

Next, you will make two sprites, one called "Player" and the other one called "Enemy AI Agent"

Next, code some simple movement for the Player.

But the player can go through walls! We don't want the player to go through the walls! So make 2 variables that we will call "Last X" and "Last Y"

Next, you will add collisions so the player won't be able to go through walls and noclip

Now, after we are finished with the Player movement scripts, let's move over the the AI script!

So our AI will go to a keyspot, or the area we last got caught. So to do that, we have to make a marker so that our AI agent will know where to go the next run we get caught.

To do that, we will make a sprite. So first, make a sprite, and hide it. The AI will still know that the sprite is there even though it is not visible. Call this sprite "LastCaught"

Next, we will make the AI agent move across the whole map. Unfortunately, Snap doesn't come with a "[Glide () secs to (random position)]" block, so we have to do some math here. Luckily, you can just use my calculations in the screenshot below instead of doing your own math!

This should be able to give our Enemy AI Agent some kind of random movement! But we aren't done yet.

The AI can only move at random places. We want a learning AI, not an enemy. So let's add some code so that our AI can learn from it's past experiences with you!

First, you will add an IF statement for the player. We will add some code so that our Player knows if it is caught. If the player IS caught, then we will broadcast a "Caught" message so that the other sprites (Like the Enemy AI) would go back to it's resting spot, the "LastCaught" object will go to where the player was last caught, and the player can go back to it's resting spot too.

This makes sure that if the player touches the enemy AI agent, it's lights out.

Now, let's move on to the enemy AI, as that's what this tutorial is talking about. So first, add a script for the LastCaught game object so that the AI would go towards that LastCaught area.

This script means that once we get caught, the "LastCaught" marker will mark the spot the player was caught in the last run. The reason why we need a "[wait (0.3) secs]" block is because if we don't have that block, then the marker will mark the player spawn location, not the caught location.

Now let's make 2 messsages, one called "BeforeCurrentState" and one called "CurrentState". These will be the blocks of code that will indicate when to hunt and search for the player and when to go room to room in search for the player.

Now add these blocks of code for "BeforeCurrentState" and "CurrentState".

BeforeCurrentState is the state of which the AI will go room to room in our maze and try and find us. CurrentState is when the AI is hunting and searching to see if the player was where it last got caught.

For spawning the AI back to it's resting place, we will add these blocks of code:

Great! Now we have finished our AI! And that means that you have officially finished a Basic Enemy AI tutorial for a top-down game! Thank you for watching, and I can't wait to see what YOU make with this AI!

actually, a better way to do that would be

btw, you can right-click a script, then click on script pic to get a picture of the script (without blurryness)

Oh, thank you for the info

Did any of you guys enjoy the tutorial or made an Enemy AI with it in your top-down games? Reply to this comment if you did, and attach a link to your own creation!

I upgraded your bot a bit. I made it in such a way that it will detect what direction your going and attempt to catch you. Also, if the bot is patroling where you last died, and you stayed at the starting area, the game becomes imposible. Here is the link to my game: Snap! Build Your Own Blocks

When the AI catches me, I lose the game. My AI is supposed to learn from your previous actions and comprehend the shortest route to catch you. It's supposed to be like TinyBuild's Hello Neighbor AI

I was going to add some more smart features, which im doing right now.

I have changed it again. feel free to check it out
edit: ive made it better

if i wait on the opposite side of it and a wall for a long time, the enemy eventually just goes through the wall. that's unfair
i think it may be because the enemy sprite is doing a check to see if it should move back after moving x amount of steps, and eventually the amount of steps becomes greater than the width of the wall. so the check never happens.

i cant figure out how to not do that, so its just like that.

I can't code in collisions for the AI, otherwise the AI will stall. But, this is bare-bones after all, so feel free to code and improve the AI

he's talking about my project, but ok.

I went through the same glitch when trying to code Collisions for the AI and the AI teleports through the wall, so I just deleted Collisions altogether

I fixed collision! Yay! And I added an extra chalenge. And yes, they are possible.

Okay, it's too hard and the AI is too intense...

it is definitly possible. ill turn down the speed a bit.

ok, i turned down the intensity of the AI. It should now go a bit slower.

Here is me beating it:
(Sped up to fit file size limit; timer for time reference)

(Gif optimized to fit file size limit; transparency filtered through optimization, that means the colors are weird)

If you don't know, I made a Demo of this AI. You don't need to code it yourself!

Top-Down AI Demo: Snap! Build Your Own Blocks

I have added some mods to the AI so that it would have infinite memory and storage capabilities.

Well, there is a limit to how much a list can store, so...

Anyways, here it is. Snap! Build Your Own Blocks