PDA

View Full Version : DelphiX Question



Wizard
17-01-2007, 12:32 PM
H, I attach some of the code on a new game I'm working on. If the ball moves past a certain value of x then I want it to be killed/destroyed/made invisible... how do I do that? I've tried the following in the DoMove Procedure:

if x < 100 then
ball.dead

It works because the ball does not respond to my mouse action past that point but the image remains......how does one get rid of the image?

Thanks for your help :-)




procedure TBall.DoMove&#40;MoveCount &#58; integer&#41;;
begin
if MoveLeft then
x&#58;= x - 50;
if MoveRight then
X&#58;= X + 50;
end;

procedure TForm1.DXDraw1Initialize&#40;Sender&#58; TObject&#41;;
begin
wallpaper &#58;= TDirectDrawsurface.Create&#40;DXDraw1.ddraw&#41;;
wallpaper.LoadFromGraphic&#40;Form1.DXImagelist1.Items .Items&#91;1&#93;.picture.graphic&#41;;
Ball &#58;= tball.Create&#40;Form1.DXSpriteEngine1.Engine&#41;;
Ball.SpriteImg &#58;= TDirectDrawsurface.Create&#40;DXDraw1.ddraw&#41;;
Ball.SpriteImg.LoadFromGraphic&#40;Form1.DXImagelist1. items.items&#91;0&#93;.picture.graphic&#41;;
ball.SpriteImg.TransparentColor&#58;=clblack;
ball.x&#58;=350;
ball.y&#58;=200;
dxTimer1.Enabled &#58;= true;
end;

procedure TForm1.DXTimer1Timer&#40;Sender&#58; TObject; LagCount&#58; Integer&#41;;
begin
if not DXDraw1.CanDraw then Exit;
DXInput1.Update;
DXSpriteEngine1.Move&#40;1&#41;;
dxdraw1.Surface.Draw&#40;-10,-10, wallpaper.ClientRect, wallpaper,false&#41;;
dxdraw1.surface.draw&#40;round&#40;ball.X&#41;,round&#40;ball.Y&#41;,
ball.SpriteImg.ClientRect, ball.SpriteImg,true&#41;;
DXDraw1.Flip;
end;

jasonf
17-01-2007, 01:24 PM
why are you physically drawing the ball in your timer code? The sprite engine does this for you. Your ball keeps being drawn because you're telling it to.

you should be calling DXSpriteEngine1.Draw();

jasonf
17-01-2007, 01:27 PM
You've probably seen this before, but your code is doing a similar job to one of my tutorials.

http://www.cerebral-bicycle.co.uk/viewdoc.asp?doc=32

FNX
17-01-2007, 02:08 PM
Hi, you can draw the wallpaper with surface.draw, but the ball is a sprite
so that you may use DXSpriteEngine1.Draw(); as jasonf said. Then,
call DXSpriteEngine1.Dead; to kill every dead sprite in the list.
It does the job automatically.
Another approach, if you want to use surface.draw (and so you can avoid the use of sprites), is to keep track if your ball is dead with a boolean,
so easily if not BallisDead then surface.draw(ball...)

I hope this make sense :)

Wizard
17-01-2007, 04:17 PM
Thanks for your replies guys :-)

JasonF, I actualy saw a version of the code in Traveller's game called Baloons. The reason why I tried it is because I was struggling to get an image displayed over a bitmap.

FNX, your suggestion of using a boolean works, it now does what I want it to do :-)

My code:


procedure TBall.DoMove(MoveCount : integer);
begin
if MoveLeft then
x:= x - 50;
if (x = 100) then
BallIsDead := True;
end;

procedure TForm1.DXDraw1Initialize(Sender: TObject);
begin
wallpaper := TDirectDrawsurface.Create(DXDraw1.ddraw);
wallpaper.LoadFromGraphic(Form1.DXImagelist1.Items .Items [1].picture.graphic);
Ball := tball.Create(Form1.DXSpriteEngine1.Engine);
Ball.SpriteImg := TDirectDrawsurface.Create(DXDraw1.ddraw);
Ball.SpriteImg.LoadFromGraphic(Form1.DXImagelist1. items.items[0].picture.graphic);
ball.SpriteImg.TransparentColor:=clblack;
ball.x:=350;
ball.y:=200;
dxTimer1.Enabled := true;
end;

procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
begin
if not DXDraw1.CanDraw then Exit;
DXInput1.Update;
DXSpriteEngine1.Move(1);
dxdraw1.Surface.Draw(-10,-10, wallpaper.ClientRect, wallpaper,false);
if (BallisDead = False) then
dxdraw1.surface.draw(round(ball.X),round(ball.Y),
ball.SpriteImg.ClientRect, ball.SpriteImg,true);
DXDraw1.Flip;
end;


PS. At the moment my ball moves left and right with the OnMouseDown event. How can I get it to move diagonally ie outside the limits of always up/down or left/right?

Thanks again :-)

[/pascal]

cairnswm
18-01-2007, 05:58 AM
if (BallisDead = False) then
dxdraw1.surface.draw(round(ball.X),round(ball.Y),
ball.SpriteImg.ClientRect, ball.SpriteImg,true);



Clearly I've been doing too many QAs this last two weeks....

BallIsDead is a boolean. You dont need to test booleans = to true/false


if NOT BallIsDead then
dxdraw1.surface.draw(round(ball.X),round(ball.Y),
ball.SpriteImg.ClientRect, ball.SpriteImg,true);


Would be a better way to write this.

Wizard
18-01-2007, 06:32 AM
cairnswm wrote:



if NOT BallIsDead then
dxdraw1.surface.draw(round(ball.X),round(ball.Y),
ball.SpriteImg.ClientRect, ball.SpriteImg,true);


Does it make any difference? I've changed it anyway -- :-)

Thanks :-)

czar
18-01-2007, 06:45 AM
It looks better but I doubt it is compiled any differently