components used: TImage TButton TLabel

this tutorial will work with borland kylix

In this tutorial we will cover a couple of tweaks which can be made to our previous tutorials to add more functionality; changing the image to look the way it is moving and introducing a running score. In tutorial four we'll cover adding more players as this is a fairly complex issue. But anyway, firstly the changing of the image during the game.

Changing our moving image during the game
To do this is fairly simple as for example, if we wanted to make the image change to look left when the left button was clicked and right when the right button was clicked etc, we would just insert one line in each of our button click procedures. Before you insert the line though you'll have to draw four images, each looking the four different directions and insert them as different TImages. Once you've done that and named them suitably, you need to set each of their image Visible properties to false. It doesn't matter where you place these images on your form as they will not be seen but instead be 'pasted' into our main image that we will move around. In the code below we will assume you named each image ImgCharacterLeft, ImgCharacterRight, ImgCharacterBack and ImgCharacterFront. The back image we can use, when the image is moving up to show the character from behind.

In the LeftBtnClick procedure the first line after begin should now be:

ImgCharacter.Picture := ImgCharacterLeft.Picture;

In the RightBtnClick procedure the first line after begin should now be:

ImgCharacter.Picture := ImgCharacterRight.Picture;

In the UpBtnClick procedure the first line after begin should now be:

ImgCharacter.Picture := ImgCharacterBack.Picture;

In the DownBtnClick procedure the first line after begin should now be:

ImgCharacter.Picture := ImgCharacterFront.Picture;

That's all there is to it, you can change the initial image that appears when the program first runs by simply changing the default image in our main TImage, ImgCharacter.

Introducing a running score
To insert a score depends greatly on what we wanting to be scoring obviously but you can apply more or less the same tactic as below whatever you're scoring. Basically what you need to do is create a variable in which the score can be added up in and a label component in which the score can be displayed. We first have to declare the variable by adding the following line after the var line at the top of our program listing:

Score: integer;

You can add this after the Form1: TForm1; line all ready there, below the var line. Now that that's done you need to create a FormCreate procedure simply by double-clicking in any blank area of the form and add the following line after begin:

Score := 0;

This basically just makes sure the score is reset to zero each time the program is started and should stop any errors from occurring. Now we're ready to actually start using our scoring variable. What you need to do is insert the following line when ever you want the score to be added on to by one:

Score := Score+1;

If you want more than one to be added each time you'd simply change the number in that line. It's as easy as that. To actually display the score we would create a label (calling it ScoreText or something) and place it on the form where we wish the score to appear. You then would insert a line like the following after the line above:

ScoreText.Caption:='Your score so far is: '+inttostr(Score);

As long as the AutoSize property is set to true, the label will automatically change its size (funnily enough), to show an increasing score.

I hope this made sense to you and again if you have any queries about this tutorial don't hesitate to e-mail me or leave a message on the message board. In the next tutorial, as mentioned earlier, we'll cover adding more players. It is quite difficult to keep track of everything so it rightly deserves its own tutorial. In the meantime have fun adding these tweaks.

Subscribe to the feed NEWS Feed
archived news | mailing list

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