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

(touching player)

This Variable will be used to let the game know that the AI is touching the player.

Coding The Player

Main article: Sprite Controls

The movement for the player is simple:

When green flag clicked
forever
	if <key (up arrow v) pressed> then
	change y by ()//Pick a number that best suits the player for movement.
	end
	if <key (down arrow v) pressed> then
	change y by ()
	end
	if <key (right arrow v) pressed> then
	change x by ()
	end
	if <key (left arrow v) pressed> then
	change x by ()
end

Coding The AI

The coding for the AI is simple:

When green flag clicked
forever
point towards (player v)
move () steps//Pick a speed that suits the project.
set [touching player v] to < touching (player v)?>//This will automatically set the variable to true if touching the player and false if it is not touching the player.
end

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:

(player health)

Go to the AI sprite, add this code to it:

When green flag clicked
set [player health v] to ()//Pick how much health the player has.
forever
create clone of (projectile v)
wait (pick random () to ()) seconds//Pick the amount of time in between each time the AI shoots the projectile at the player.
end

Next, go to the projectile sprite. The code for this is fairly simple.

when green flag clicked
hide
forever
go to (AI v)
end
when I start as a clone
show
point in direction ([direction v] of [AI v])
repeat until <<touching (player v)> or <touching (edge v)
move () steps
end
delete this clone

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.

when flag clicked
forever
	if <touching (projectile v)> then
	change [player health v] by ()
		repeat ()//This will let the player know that they've been hit by the AI's projectile.
		set [brightness v] effect to (20)
		wait () seconds
		set [brightness v] effect to (0)
        wait () seconds
	    end
	end
end

To end the game when the player has no health, just add this code to the game:

when flag clicked
forever
if <(player health) < [1]> then
stop [all v]

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:

 (distance to (mouse-pointer v)) 

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:

(in radius)

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:

when green flag clicked
forever
	if <(in radius) = [1]> then
	point towards (player v)
	move (3) steps
	else
	point in direction (90)
	wait until <(in radius) = [1]
	end
end

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:

when green flag clicked
forever
go to (AI v)
	if <touching (player v)?> then
	set [in radius v] to (1)
	else
	set [in radius v] to (0)
	end
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.