Blitz2D Newbies: Basic A.I. Movement
by CloudNine

Introduction:

I am surprised by the lack of tutorials on Artifical Intelligence. 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 code is 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 (Check out the functions section as well, as there are many good A.I programs there. Don't miss the A.I sections of the board as well.

CONCLUSION

Thanks to the people who have lent me their code, including Grim_1_2_3 (The actual code can be found in the Code database under A.I). I've had a good time writing this article and hope to contribute more to Blitzcoder. 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.

  • To download the source code/demos used with this tutorial, Click Here.

Cya soon! :D

-CloudNine


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.