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

touchingplayer

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:

whenclickedforeverifkeyuparrowpressedthenchangeybyPickanumberthatbestsuitstheplayerformovement.endifkeydownarrowpressedthenchangeybyendifkeyrightarrowpressedthenchangexbyendifkeyleftarrowpressedthenchangexbyend

Coding The AI

The coding for the AI is simple:

whenclickedforeverpointtowardsplayermovestepsPickaspeedthatsuitstheproject.settouchingplayertotouchingplayer?Thiswillautomaticallysetthevariabletotrueiftouchingtheplayerandfalseifitisnottouchingtheplayer.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:

playerhealth

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

whenclickedsetplayerhealthtoPickhowmuchhealththeplayerhas.forevercreatecloneofprojectilewaitpickrandomtosecondsPicktheamountoftimeinbetweeneachtimetheAIshootstheprojectileattheplayer.end

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

whenclickedhideforevergotoAIend
whenIstartasacloneshowpointindirectiondirectionofAIrepeatuntiltouchingplayerortouchingedgemovestepsenddeletethisclone

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.

whenclickedforeveriftouchingprojectilethenchangeplayerhealthbyrepeatThiswilllettheplayerknowthatthey'vebeenhitbytheAI'sprojectile.setbrightnesseffectto20waitsecondssetbrightnesseffectto0waitsecondsendendend

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

whenclickedforeverifplayerhealth<1thenstopall

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:

distancetomouse-pointer

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:

inradius

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:

whenclickedforeverifinradius=1thenpointtowardsplayermove3stepselsepointindirection90waituntilinradius=1endend

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:

whenclickedforevergotoAIiftouchingplayer?thensetinradiusto1elsesetinradiusto0endend