| HOME | FORUM | DOWNLOADS | TUTORIAL | CODE LIBRARY | LINKS | CONTACT US |

TUTORIALS>> BASIC AI MOVEMENT

 

Contributed by CloudNine

 

I am surprised by the lack of tutorials on Artificial Intelligence (AI). It's what makes your computer game seem intelligent and therefore challenging. Otherwise, you may end up with a boring game. No fun eh! It is one of the most important areas of game development and you will have to deal with it in most games. It is also quite a skilled area as it invovles quite a lot of maths. Enough with the dawdling, let's get on with it.

Some of the codes are taken from my first public game, Pong II :) VERY SIMPLE A.I. Here's a very basic piece of code that shows some A.I. Look at the commenting in the code for more detail.

In this piece of code we have set up the screen, loaded our images and placed the images. Now comes The fun bit!

In this slice of code we told the 'bat' the limits of the screen and did the collision Then we declared a random number to use in the main A.I. code. After that we used that number to decide whether the bat would hit the ball or whether it would make a 'error' and miss the ball. After that there was the normal DrawImage code.
Let's focus more on the A.I. code.

We declared a random number 1,3, so, when we come to use the number in our code, it will 'fail' once out of every three times it has to respond.

Both paragraphs are basicly the same. When the machine decides to use to the random number, the bat will miss the ball, as closer reading will show.
That code did two things (in the form of A.I):
Moved a computer controlled bat
Made it miss using MilliSecs
That was quite simple and shouldn't have racked your brains. Let's move on to some more complicated A.I

MORE SIMPLE A.I.

We will now look at moving a sprite up,down,left and right.
We'll use some code donated by Grim_1_2_3. Sorry about the poor graphics, but it's the theory that counts ;) Here we go:

The "SeedRnd" command takes a number from the number of milliseconds and uses it as a random number (for later in the code). Millisecs is often a useful command in A.I related functions.

This part of the code loads the images and the variables

This code prints onto the screen to tell you which buttons you have to press to move the player and the direction in which the player is moving

Masks and draws the graphics.

This code calls for different functions and checks if the 'guy' and the maze have collided.

These functions check for the guy's movement. It is quite lengthy, and could be shortened

And this checks how the guy responds. It gives different directions which will help with the deciding of the dragon's movement.

This function is used to check the dragons movement by seeing which way the guy was moving using the runkeys() and checkkeys() function.
That piece of code taught you to move sprites when other sprites moved (i.e the guy and the dragon), using a variable so that the dragon could check which way the guy was moving, which made it simpler.

WE HAVE LEARNT....

To move computer controlled sprites
To move the computer with random numbers
To move sprites if other sprites moved

SOME A.I STUFF

Check out www.aihorizon.com (suggested by Krylar), and "Simple A.I" by Specis

CONCLUSION

Thanks to the people who have lent me their code, including Grim_1_2_3 (The actual code can be found in the BlitzCoder Code database under A.I). I've had a good time writing this article and hope to contribute more. The many A.I questions in the Blitz Basic and BlitzCoder boards have also helped.
I might do a tutorial on a different part of A.I, but for now, it's back to coding.

Cya soon! :D
-CloudNine

 
 
 

| HOME | FORUM | DOWNLOADS | TUTORIAL | CODE LIBRARY | LINKS | CONTACT US |