components used: TImage TButton

this tutorial will work with borland kylix

Part 1Part 2

The CheckMoveNum procedure
Now we write the CheckMoveNum procedure itself. Remember, this procedure is being called every time a direction button is clicked on, and that we want to be able to check if a player has taken two turns, and if they have, the next player gets a couple of turns and so on until we're back round to player 1.

We've already set our starting image to Ply1 so we start by checking when our MoveNum variable is equal to two, and if it is then we move to Ply2 getting a turn and so on. When we finally reach the point when all four players have had a turn then we go back to Ply1 again. You can see this in the code below. Also note that everything here has to be added by you as none of it is automatically generated like in previous procedures, so I would place it as the last procedure in your program, making sure it is placed before the final line end.

procedure TForm1.CheckMoveNum;
begin
 if
MoveNum = 2 then
  ImgCharacter :=Ply2;
 if MoveNum = 2 then
  PlayerNum := 2;

 if MoveNum = 4 then
  ImgCharacter :=Ply3;
 if MoveNum = 4 then
  PlayerNum := 3;

 if MoveNum = 6 then
  ImgCharacter :=Ply4;
 if MoveNum = 6 then
  PlayerNum := 4;

 if MoveNum = 8 then
  ImgCharacter :=Ply1;
 if MoveNum = 8 then
  MoveNum := 0;
 if MoveNum = 8 then
  PlayerNum := 1;
end;

That's it, you should now be able to run the program with out any errors if all went well. For each player you should get to move in any two directions, with the second player getting to move on the third click of a direction button, the third player on the fifth click and so on. This may be the point when you reach problems, either because you've missed a line somewhere or I've omitted to mention something. If you think it's something I've missed out or you can't work out what you've done wrong, please e-mail me at the address at the end of this tutorial.

Looking left, looking right
Now it's time to get our character looking in the direction it's moving. You'll need to draw an additional three images for each player on top of the one you've already drawn for each player of it looking forward. By the time you're done you should have sixteen pictures in all. You want to add all sixteen pictures to the form naming them Ply1left, Ply2right and so on, remembering to set all their visible properties to false and the AutoSize property to true, in the Object Inspector window.

So looking at your form now you should have sixteen TImages each with a different picture, and you should have your four original images which you will be moving around. Each of these images should have been already showing the front of each player and have a visible property of True. That's eighteen images in all on the form. When you run the program just now it should run exactly the same as it did before as your new sixteen images are all set to be invisible.

Now we need to add the code to take into account which picture should be showing for each player. This will mean adding another four procedures, one for each direction button. We'll call them LeftBtnL, RightBtnL, UpBtnL and DownBtnL. You should start by defining these in the public section of your program underneath the previously defined CheckMoveNum procedure as shown below:

procedure LeftBtnL;
procedure RightBtnL;
procedure UpBtnL;
procedure DownBtnL;

Now we need to write the procedures. After our previously written CheckMoveNum procedure you should therefore type the following:

procedure TForm1.LeftBtnL;
begin
 if
ImgCharacter = Ply1 then
  Ply1.Picture := Ply1Left.Picture;
 if ImgCharacter = Ply2 then
  Ply2.Picture := Ply2Left.Picture;
 if ImgCharacter = Ply3 then
  Ply3.Picture := Ply3Left.Picture;
 if ImgCharacter = Ply4 then
  Ply4.Picture := Ply4Left.Picture;
end;

procedure TForm1.RightBtnL;
begin
 if
ImgCharacter = Ply1 then
  Ply1.Picture := Ply1Right.Picture;
 if ImgCharacter = Ply2 then
  Ply2.Picture := Ply2Right.Picture;
 if ImgCharacter = Ply3 then
  Ply3.Picture := Ply3Right.Picture;
 if ImgCharacter = Ply4 then
  Ply4.Picture := Ply4Right.Picture;
end;

procedure TForm1.UpBtnL;
begin
 if
ImgCharacter = Ply1 then
  Ply1.Picture := Ply1Back.Picture;
 if ImgCharacter = Ply2 then
  Ply2.Picture := Ply2Back.Picture;
 if ImgCharacter = Ply3 then
  Ply3.Picture := Ply3Back.Picture;
 if ImgCharacter = Ply4 then
  Ply4.Picture := Ply4Back.Picture;
end;

procedure TForm1.DownBtnL;
begin
 if
ImgCharacter = Ply1 then
  Ply1.Picture := Ply1Front.Picture;
 if ImgCharacter = Ply2 then
  Ply2.Picture := Ply2Front.Picture;
 if ImgCharacter = Ply3 then
  Ply3.Picture := Ply3Front.Picture;
 if ImgCharacter = Ply4 then
  Ply4.Picture := Ply4Front.Picture;
end;

That's all four of our new procedures written now and when you run it, your players should now be looking in the proper direction as they move around.

That's us finally finished! In these two last parts we have changed our previous code quite dramatically. In the next tutorial we'll be explaining the uses of the TTimer component. Until next time, be sure to e-mail me with any problems you might have or leave a message on the message board.

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 ]