Taking off from the end of part 2, go to your dximagelist
and add another item. We're going to add a background, a black one in this
case. Create a picture (with paint or something) that's 640x480 in size
and make it black.
Head back to the imagelist and add the black image to the 2nd item (note
that in code this is item[1], since it starts at 0). Remember to make sure
that the transparent color is not the same as your background color!
If you've played around a bit with the result of part 2 you probably
noticed that under certain circumstances the image will not be removed. To
make sure that the old images are removed we'll simply overwrite them,
hence the background. This is done by adding the following line above the
item[0].draw line in the OnTimer event of the timer:
dximagelist1.Items[1].Draw(dxdraw1.Surface, 100, 100, 0);
Compile your project and move the smilie around a bit, you'll probably
won't see alot of difference if you didn't played around with the result
of part 2. If that's the case then temporarely replace the smilie with a
yellow block of 300x300 (there's alot more sizes, but it doesn't always
happen). Test it with and without the line and you'll see that without the
image won't be removed. There's still one problem though, go to the end of
the screen and you'll see that the smilie goes out of the screen. This
might be handy from time to time (ie. end of a level), but alot of games
never even use this. So let's limit where the smilie can go.
Development side-note: Develop your games using a grid with the screen
sizes, this reduces the chance of errors.
Go to the FormKeyDown event of Form1
(OI) and enter the following code above and below what's in the function:
if x_pos > 5 then
begin
if y_pos > 5 then
begin
[..existing code..]
end
else
begin
y_pos := y_pos + 1;
end
end
else
x_pos := x_pos + 1;
The first part set the boundaries to ([x,y]) [5,5]. Now you can't cross
the left or the top of the screen. The right side works the same, try to
get this working yourself. Remember, if dxdraw is not running in full
screen, the actual screen-size may differ, just test it out ;). The part that goes below the existing code makes sure that if the image is not
within those borders that it get's a + 1 on the x or y value. You have to
do this, because if you don't the image will go beyond the border (if it's
on x = 6 it will set x to 5 rendering the image beyond the defined
boundary), wich results in your image getting stuck.
Now for the flickering, flickering will occur when you're drawing
faster then Windows. This usually happens when you have animations or when
moving larger pictures under runtime. We won't have alot of problems with
this yet, but this is something that can give you alot of problems later.
It's quite simple to fix this, under
type
TForm1 = class(TForm)
add the following line (just add it above the other procedures):
procedure StopFlickering(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
This means that when Windows sends the message to repaint our GUI
together with the rest of the screen this function gets started. Now for
the function itself:
Procedure TForm1.StopFlickering(var Msg: TWMEraseBkgnd);
begin
Msg.Result := 1;
end;
We're handling the repainting ourself, but since we don't want this to
happen (we got the timer for that) the function simply says that it has
finished (msg.result := 1;). I'll show you some samples of
flickering in later
parts, but for now that's not important yet. Well that's it for this part,
check the next one for animating and sprites.
Download the source-code at http://devcenter.darkangeldev.com (normally outline version as well). |