components used: TDXDraw TDXImageList TDXSprite TDXTimer TDXInput

delphix does NOT work with borland kylix

Part 1Part 2 (coming soon) download source code

In the previous tutorial we took our first steps in making good use of the DXDraw component and its various cousins. In this tutorial we'll take this a step further by talking a bit about moving our sprites around using the keyboard or joystick/joy pad and introduce the concept of scrolling backgrounds with DelphiX. It's been long awaited but here it is :-)

So, the first thing we need to do is actually add our background. Previously we set this up in our DXTimer component, redrawing it before each animation. This limits us however and to be able to effectively scroll our background we need to set up a special class of sprite called TBackgroundSprite, much as we did with our pacman and ghost sprites. So, we're going to call our background sprite type, TScrollingBackground. The following code therefore needs to be placed just after we've set up our pacman and ghost sprites near the beginning of our code.

TScrollBackground = class(TBackgroundSprite)
public
procedure
DoMove(MoveCount: Integer); override;
end;

Now that we've done that we need to actually give our TScrollBackground it's own variable with the following line in our var section of the code just after the Ghost : TGhost; line.

Background: TScrollBackground;

To finish setting up our background we need to add the following lines at the beginning of our FormCreate procedure before the existing lines.

Background := TScrollBackground.Create(DXSpriteEngine1.Engine);
with Background do
begin

 SetMapSize(640, 480);
 Image := DXImageList1.Items.Find('NewBackground');
 Y := 10;
 Z := -13;
end;

This code assumes that you've set up the DXImageList component with a background called NewBackground and that it is of size 640x480.

So far so good. We now no longer need to have the line DXImageList1.Items[1].Draw(DXDraw1.Surface,0,0,0); in our OnTimer procedure any more as this is now handled automatically by TBackgroundSprite. So, once you've deleted this line, compiling the code will give you a program much the same as before, but with a background of image 'NewBackground'. We of course want to be able to scroll our background so let's deal with this now. We need to alter our DXTimer.OnTimer code to take into consideration this change. Before we do that though let's bring in the DXInput component.

In the last tutorial we had our sprites controlled purely by our program, but we obviously would want to have at least one sprite controlled by the user in our game to make it, well a game. This is where the previously introduced component DXInput comes in useful, and only a few changes are needed to our previous code in order to get this working.

Once you've added this DXInput component to your form you need to change the DXTimer.OnTimer code to contain the following.

DXInput1.Update;

if isLeft in DXInput1.States then begin
 Pacman.AnimStart := 2;
 Ghost.AnimStart := 2;
 Background.X := Background.X + 1;
end;

if isRight in DXInput1.States then begin
 Pacman.AnimStart := 0;
 Ghost.AnimStart := 0;
 Background.X := Background.X - 1;
end;

if isUp in DXInput1.States then
 Pacman.Y := Pacman.Y -1;

if isDown in DXInput1.States then
 Pacman.Y := Pacman.Y +1;

DXSpriteEngine1.Move(2);
DXSpriteEngine1.Draw;
DXDraw1.Flip;

Compared to the code we had before in our OnTimer code, the last three lines still exist. All the code we now see before this, deals with controlling our sprite's movement. With our program we want our sprite to appear as though it is moving by scrolling the background instead of moving the sprite every time the user presses the left or right arrow. To do this we just need to increase or decrease our background's X coordinate depending on whether we want to go left or right. You can see we've done that above with the line Background.X := Background.X + 1; and in the similar code a few lines down from that. When it comes to the user pressing the up or down arrow we've just set our pacman sprite to move up and down, but you could always place some code here to get our sprite to jump. We could have also placed the code for scrolling our background in a TScrollBackground.DoMove procedure as we did with the sprites.

Before you run the program now, all you need to do is get rid of the lines PixelCheck := True; and Pacman.X := Pacman.X + 1; in our TPacman.DoMove procedure so that the code is like this:

procedure TPacman.DoMove(MoveCount: Integer);
begin
 inherited;
 if
PacmanDead = False then begin
   Collision;
 end else
end;

This stops our sprite running off. Obviously with our program the way it is just now, no collision will ever occur but keep it here for just now so that when you need it, it's there.

That's it. With some fairly simple alterations to our code, we've opened ourselves up to many new possibilities. As ever, if you have any problems or suggestions then please do e-mail me at ben@delphigamedev.com or leave a message on the forums. Until the next tutorial, enjoy :-)

Part 1Part 2 (coming soon) 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 ]