Programming simple enemy AI

Hello! I'm going to show you how to program basic enemy AI for battle games!
Now, your enemy has to be able to do something, but it also needs a little life, right? That's where the RND command comes in. Say, you make a GOSUB command directing to a possibility of the enemy doing two actions: @ENEMY

IF RND(50)==1 THEN GOTO @ENEMYACTION1

IF RND(50)==1 THEN GOTO @ENEMYACTION2

RETURN The smaller a number in the parenthases, the bigger chance the enemy will have of doing the action. Now, let's say you describe your player X coordinate as X, and the Y coordinate as Y. You could make the enemy's first action moving to your current X and Y coordinates. (this is for if your enemy is a sprite, learn about sprites on one of my other tutorials.) @ENEMYACTION1

SPOFS (ENEMYCONTROLNUMBER),X,Y,(SPEED)

RETURN This will set the enemy to your current X and Y coordinate. But your sprite can very easily move out of range of the enemy before it can attack you.

But what if the enemy should only do the second action when it's touching your sprite?We'll take... IF RND(50)==1 THEN GOTO @ENEMYACTION2 And change it to... IF RND(50)==1 AND SPHIT(ENEMYCONTROLNUMBER,YOURCONTROLNUMBER) THEN GOTO @ENEMYACTION2 Easy!

I hope this tutorial helped! Thanks for reading!