Here in this lecture, we are going to implement AI shooting for the enemy ships. We want the enemy ships to keep shooting as they come down. So for that we'll be using alarms so that they keep shooting. But we also need to make it so that they only shoot when the player is in front of them, otherwise they should not shoot. So to achieve that, we have to use some ad. This is the x coordinate of the player and this is the y coordinate.
For this we only need the X and here's the enemy's x coordinate. You can see that as the player gets closer to being in front of the enemy, the difference between the enemy's x and the players x reduces This was when the enemy should be able to shoot. So to make the enemy shoot, we just have to check whether the difference between the enemies x and the players x is below a certain value. If it is then the enemy should shoot. So here's what we will do, we will get the difference between the players x and the enemies x. So let's write that as Oh player x minus x. writing this in Game Maker will get the player in stances x coordinate.
So this will get the difference but it may be more stream or negative depending on where the player is, we only want a positive value. So around a subtraction or add brackets and outside those brackets I'll type ABS doing that will make it so that only a positive value can be returned. So if the difference is negative, say minus 28, it will give us a positive value like 28. So this way we get the distance between the players x and the enemies x, then we can use that value to check if the enemy can shoot. So let's open up Game Maker. I've imported a sprite for the enemies bullet, make sure its origin is at the middle center.
Now for the enemies bullets, I'll go to a bullet right click on it and select Duplicate. It will create a new object with the same events. I'll name this one oh bullet enemy. I'll assign the sprite to it. Now I'll open is creative. And you can see that the speed here is minus 20 because the player's bullets are supposed to move up but the enemy's bullets need to go down.
So I'll just remove the minus sign from here. Now this bullet will go downwards. Here you have a collision event with the enemy versus this is the enemy's bullet, it needs to hit the player. So right click on the event, select Change event, then go on the collision and select or player. And now this is a collision event with the player. So when this bullet hits the player, the players SP will go down.
Now we'll open a player, I'll go to a square demand or create a new variable with the assigned variable action, it will be SP with a value of five. So this is a player's health. And we'll come back to the subject in the next lecture. Now we'll open Oh enemy and go to its creative and here are the set alarm countdown action altitude from zero to 60. That alarm will be used for the shooting. So I'll add the event for that alarm.
First of all, I'll add the variable action. In place of the variable I'll type this so this console distance between the x and the enemy's x Now we need to make sure that this distance is low enough so that the enemy could shoot. So I'll change the condition to less and in the value I'll enter 128. So the distance should be smaller than 128. Now let's create an instance action and attach it to this condition for the object as let's call it enemy, the position will be the enemies x and y and the layer will be bullish. So if top layer is in front of the enemy, it creates a bullet in stance.
Now whether or not this condition is true, we want the alarm to keep looping. So it's like the get random number action and drop it below the condition so that it's placed outside of it. I'll use the section to set a random value for the alarm. So I'll change the symbol to integer I'll set the minimum to 30 and the maximum to 60. Now in the target, normally, I would use a temporary variable and then use that variable in an alarm action, but you can actually start the alarm directly from here without using the alarm action. So you have to type alarm square brackets and then the alarm number inside them.
So this will get a random value between 30 and six And directly assign it to the alarm. Now run the game. You can see that the enemy shoot when the player is in front of them, otherwise they don't. Now let's move on to the next lecture we will work on the player being head