components used: TImage TButton

this tutorial will work with borland kylix

Part 1Part 2

In this tutorial we will be adding more players to our previous code. It won't be easy so hold on to your hats.

When adding more players, things start to get a little more complicated. For the purpose of this tutorial we'll deal with turn-based play as for the moment we are limited to only being able to use our direction buttons. So, if we were to have a four player game where say, each player got to move two times each turn we would have to do the following to allow the buttons to move different images depending on whose turn it is. Firstly, you will have to create a set of four main images (each with their visible property set to False and their AutoSize to True), which each represent different players and if you are wanting to have the images change direction you'll also have to add the four images (with their visible property set to false) for each player to show them looking in each direction. This will get pretty messy, but we'll be introducing the use of image libraries in a future tutorial that will tidy this up. For the moment, we're just introducing the concepts of having multiple players.

The adding and deleting of images
We'll have four images for each of our four players, called Ply1, Ply2, Ply3 and Ply4 with the appropriate Ply1left, Ply2right pictures etc. To actually use these images we'll need to alter some of our previous code as well as introduce some new code. You will also need to delete or rename our old TImage that we've been using up until now as it has been divided into four different players. We can still make use of all our code relating to this image though as you'll see soon.

The code itself
Before we continue we need to add two new integers to our variable list so the following lines need to be added after the lines already after our var line:

MoveNum: integer;
PlayerNum: integer;
ImgCharacter: TImage;

These variables will be used later with the ImgCharacter variable now being made use of by our existing program. We also need to add our own procedure, so the following line placed under the private line would not go amiss:

procedure CheckMoveNum;

You now need to add a link to this procedure from each click of the direction button but to do that we need to alter our BtnClick procedures quite a bit. Below is the altered code for all four BtnClick procedures. Also added is our addition to the MoveNum variable after each move. This addition only happens if our character has not 'hit' the edge of the screen.

procedure TForm1.LeftBtnClick(Sender: TObject);
begin
 if
ImgCharacter.Left > 0 then
  ImgCharacter.Left := ImgCharacter.Left-10;
 if ImgCharacter.Left > 0 then
  MoveNum := MoveNum+1;
 if ImgCharacter.Left > 0 then
  CheckMoveNum;
 if ImgCharacter.Left = 0 then
end;

Note the deletion of the line ImgCharacter.Picture := ImgCharacterLeft.Picture;. The equivalents of this line in each of our BtnClick procedures have also been deleted. We'll be coming back to this later.

procedure TForm1.UpBtnClick(Sender: TObject);
begin
 if
ImgCharacter.Top > 0 then
  ImgCharacter.Top := ImgCharacter.Top-10;
 if ImgCharacter.Top > 0 then
  MoveNum:= MoveNum+1;
 if ImgCharacter.Top > 0 then
  CheckMoveNum;
 if ImgCharacter.Top = 0 then
end;


procedure TForm1.RightBtnClick(Sender: TObject);
var
 ImgRight : integer;
begin
 ImgRight := Form1.ClientWidth - ImgCharacter.Width-4;
 if ImgCharacter.Left < ImgRight then
  ImgCharacter.Left := ImgCharacter.Left+10;
 if ImgCharacter.Left < ImgRight then
  MoveNum := MoveNum+1;
 if ImgCharacter.Left < ImgRight then
  CheckMoveNum;
 if ImgCharacter.Left > ImgRight then
end;

Here you should notice the introduction of the ImgRight variable. I used this to try and tidy up the procedure listing a little so that the variable ImgRight can be used each time instead of the long winded Form1.ClientWidth - ImgCharacter.Width - 4;. The same has been done with the DownBtnClick procedure below.

procedure TForm1.DownBtnClick(Sender: TObject);
var
 ImgDown: integer;
begin
 ImgDown := Form1.ClientHeight- ImgCharacter.Height-1;
 if ImgCharacter.Top < ImgDown then
  ImgCharacter.Top := ImgCharacter.Top+10;
 if ImgCharacter.Top < ImgDown then
  MoveNum := MoveNum+1;
 if ImgCharacter.Top < ImgDown then
  CheckMoveNum;
 if ImgCharacter.Top > ImgDown then
end;

So basically what's happening in each procedure is that if the character will not hit the screen on its next move then firstly it is moved ten pixels, secondly the move number is added to and thirdly the CheckMoveNum procedure is run. If the character will hit the screen on its next move the procedure is just ended as shown on the last two lines of each BtnClick procedure.

You should also set the starting values of these new variables, so the following lines should be added in our FormCreate procedure after the begin line. If you have not already created a FormCreate procedure when adding a scoring variable in the last tutorial, then simply double-click in an empty space on your form and the procedure will be created.

ImgCharacter := Ply1;
MoveNum := 0;
PlayerNum := 1;

The ImgCharacter variable has been basically told to start with our player 1 image (Ply1), while our MoveNum has been set to zero so that it can be added to when we use it later, and our PlayerNum has been set to 1 as this tells our program to assume we start with player 1. The MoveNum variable will be used to keep track of (surprise, surprise) the move number of a particular player because as mentioned earlier, in this example we want the player to be only able to move two moves each turn.

What we've done in this tutorial is pretty complicated so if you've got any questions about this don't hesitate to e-mail me or leave a message on the message board. In part two we write the CheckMoveNum procedure itself so that you can get this program into action again, and also we'll try changing the direction each of our characters are 'facing'.

Part 1Part 2

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

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