components used: TImage TButton TTimer

this tutorial will work with borland kylix

download source code

For this tutorial, we're dealing with pretty basic concepts to begin with, but nonetheless you should find it useful. I should mention that as a guideline I am using Andre LaMothe's 'Games Programming in 21 Days' to come up with things for you to try. While I don't think this book is in print at the moment, it is a useful one even though all the code is in C. In particular the chapter on Artificial Intelligence is a useful introduction. There are very different ways of doing things in Delphi than in C, so I'm not copying direct code here or anything, all I'm doing is using his ideas of basic concepts. By all means, go out and buy a copy if you can find it, or (probably) better still you could buy one of his newer equivalent books.

So without further ado, let us begin. There are a couple of concepts I want to introduce to you, and these are outlined below.

The Computer Attacker
In this part, we'll have two characters. One will be controlled by you, the other by the computer. Basically, the computer opponent's sole aim is to be on your coordinates, but you can imagine it's going for the attack. Each time you try and run away, it will run and catch up with you, and only stop when it's on you.

So, to begin with you need to create a character and the four direction buttons (or whatever you prefer), as outlined in tutorial one. When you've done that, you need to also create another TImage to represent the computer opponent. As well as that add the TTimer component from the System tab and set the time interval to about 100, you can always adjust it later. The lower the number, the faster the computer opponent will chase after you. For the purposes of this tutorial we'll assume that you called the player TImage, Player and the enemy TImage, you guessed it, Enemy.

To actually bring our opponent 'alive' we'll have to use the TTimer component. Double-click on it on your form, and you should get the relevant TTimer code coming up in the code editor. Now enter the following lines between begin and end;

if Player.Left > Enemy.Left then
 Enemy.Left := Enemy.Left + 2;

if Player.Left < Enemy.Left then
 Enemy.Left := Enemy.Left - 2;

if Player.Top > Enemy.Top then
 Enemy.Top := Enemy.Top + 2;

if Player.Top < Enemy.Top then
 Enemy.Top := Enemy.Top - 2;

What does this do? Well it checks each time interval in the TTimer component to see if the Left and Top coordinates of the player are larger or smaller than the opponent's coordinates. Remember that the Left and Top coordinates are effectively, our X and Y coordinates. So, in this program if the player's coordinates are larger than the enemy player's, it keeps adding two onto its coordinates until the enemy's coordinates are equal to the players. A simple game but you get the idea. You could for example have it so that each time the enemy reaches the player's coordinates, that an explosion appears in place of the player's image, and that a variable with the number of lives goes down by one. When the number of lives reaches zero, a Game Over screen could come up. If you want me to explain how you could do these things let me know and I will do a tutorial outlining it, but you should be able to get an idea of what to do from tutorial four, which tells you both, how to change an image during the game, and how to keep a running score. Obviously, with the running score, instead you'd want the number to be taken off rather than added on, but that's about it.

The Player Attacker
To turn the tables, and make you the attacker and the computer the target, you need only swap each - for a + and each + for a -, so that the enemy runs away instead of approaching you. However, you'll need to make sure that you've made use of tutorial two to stop players moving off the screen so that your enemy doesn't shoot off into the distance never to be seen again. Obviously, as it is, even if the enemy didn't move off the screen it'd probably just get stuck in a corner, so you may want to make some other rules of escape.

For example you could make it so that the computer opponent only runs away when you're within 50 pixels of it. So you'd add an extra four lines as shown below to also check if the two players are within the 50 pixels and if not then the enemy does not move.

if Player.Left > Enemy.Left then
 if Player.Left - Enemy.Left < 50 then
  Enemy.Left := Enemy.Left - 2;

if Player.Left < Enemy.Left then
 if Enemy.Left - Player.Left < 50 then
  Enemy.Left := Enemy.Left + 2;

if Player.Top > Enemy.Top then
 if Player.Top - Enemy.Top < 50 then
  Enemy.Top := Enemy.Top - 2;

if Player.Top < Enemy.Top then
 if Enemy.Top - Player.Top < 50 then
  Enemy.Top := Enemy.Top + 2;

That's about all there is to it. So what next? Well, in keeping with Andre LaMothe's ideas next we're going to create a program which has no human intervention but instead imitates a fly buzzing around a flower (or something else, if you're that way inclined). How are we going to do that? Well, we introduce a random number to decide whether the fly is trying to move towards the flower, away from the flower or in a random direction. It's quite a big job in itself so I'll give it a tutorial of its own. I know this tutorial was short, but the next tutorial covers more. In the meantime you can have a think for yourself how you could do this, and see if it matches up to what I show you. Chances are, you may even come up with a better way. As always, all questions and suggestions are welcome, and you can e-mail me them or leave a message on the board.

download source code

Subscribe to the feed delphigamedev.com NEWS Feed
archived news | mailing list

[ all tutorials found at delphigamedev.com are Copyright © 2008 Ben Watt ]