How to Make Game AI
Artificial Intelligence is the foundation of many games on Scratch. This article shows how to make an AI.
Coding the Program
First, two Sprites, the Player and the AI, need to be created.
Variables Required
This Variable will be used to let the game know that the AI is touching the player.
Coding The Player
Шаблон:Main The movement for the player is simple:
Coding The AI
The coding for the AI is simple:
Creating an AI With Projectiles
Creating an AI with projectiles will be a bit of a challenge to any Scratch game. Another sprite needs to be created. This will be the projectile sprite.
One more variable also needs to be created:
Go to the AI sprite, add this code to it:
Next, go to the projectile sprite. The code for this is fairly simple.
The final bit of code that is going to be added will be to the player. This script will give the player its health system.
To end the game when the player has no health, just add this code to the game:
Secondary Method
Here is a second AI method that will detect when the player is within range of the AI. This does not use the following block:
Sprites/Variables needed
Like the method above, three sprites are needed. However, one of the three is going to be different: Player, AI, and a sprite that is a circle. After making the player sprite, give it the same movement code displayed up above in the first method. Only one new variable is needed:
Coding The AI
This time, the AI will need some new code to function like it needs to for this method. The code for the AI is still simple and it is as shown:
The Radius
The circle sprite that was made earlier is the "visibility radius" for the AI. To make this function, the following code is needed: