PDA

View Full Version : Shooting Bullets



Cer3brus
14-04-2009, 10:39 AM
I've been trying to make a remake of the gamemaker "scrolling shooter" tutorial(http://www.yoyogames.com/make/tutorials).
Everything's been going pretty well except when my plane has to shoot. You see, there's a
'create instance of bullet at point [X, Y]' in gamemaker but in delphi(particularly DelphiX) I have no idea where to start. Do I use an array of some type? I'll post up the source soon to give a better idea.

NecroDOME
14-04-2009, 11:00 AM
Yes, you can have some array or list that holds the projectiles.



procedure Update projecttiles();
begin
for each projectile do
begin
Projectile[ index ].Move;
Projectile[ index ].CheckHitWithEnemy;
end;
end;

Cer3brus
14-04-2009, 11:21 AM
Thanks!!
I'll try it out

Cer3brus
17-04-2009, 04:57 PM
I've found an easier way without the need for another procedure or an array, create one on the spot!
Without further ado:

If (GetKeyState(32) and 128 = 128) And (Shot_Counter < 0)
Then
Begin
Shot_Counter := 10;
with THBullet.Create(Engine.Engine) Do // <--This Here's The Important line :)
Begin
Image := Sprites.Items.Find('Bullet');
X := Hero.X + (Hero.Width DIV 2);
Y := Hero.Y;
Z := 13;
Width := Image.Width;
Height := Image.Height;
End;//With
End;//If
Of course all the movement is handled in the DoMove Procedure.

Wizard
17-04-2009, 05:54 PM
Image := Sprites.Items.Find('Bullet');

Instead of using 'find', rather use an index i.e.

Image := FormGame.DXImageList.Items[20];

It's faster :yes:

yassersoft
29-01-2010, 02:53 PM
Hey. Why not better:

Proyectiles:array[0..10] of TBitmap;

//if you nedd more so use a function to Increase the array


if theKey=VK_SPACE then
inc(i);
Proyectiles[i].Create;
Proyectiles[i].LoadFromFile('missile.bmp');
initx:=Hero.X;
for j:=initx to ClientWidth do
begin
inc(j,10);
Hero.x:=j;
end;


That is what I would do it.. i suppose it´s right

NecroDOME
29-01-2010, 03:09 PM
an other approach.

TPojectile = record
x, y : single;
Image : TBitmap;
SpeedX, SpeedY : single;
end;

...or...

TPojectile = class
Image: TBitmap;
SpeedX, SpeedY : single;
end;

...or...

TPojectile = class(TBitmap)
SpeedX, SpeedY : single;
end;

...and...

I'm not really a fan of using fixed-arrays, but if you are sure you maximum have 10 projectiles (or a fixed maximum amount), that you can indeed use a fixed array. Here is dynamic a more dynamic approach with a TList object. (Would be nicer if pascal has generics, than you could declare it like TList<TProjectile>.
Projectiles : TList;

procedure InitGame()
begin
Projectiles := TList.Create;
end;

procedure Shoot()
begin
Projectiles.Add(Projectile.Create(x, y, speed, etc...));
end;

procedure UpdateProjectiles()
var i : integer;
p : TProjectile;
begin
for i := 0 to Projectiles.Count-1 do
begin
p := TProjectile(Projectiles.Items[i]);
p.x := p.x + p.speedx;
p.y := p.y + p.speedy;
end;
end;

procedure OnProjectilehitSomething(ProjectileInstance : TProjectile)
begin
SpawnMagicalEffectThingy(ProjectileInstance .x, ProjectileInstance .y);
Projectiles.Remove(ProjectileInstance);
end;

Andreaz
29-01-2010, 05:41 PM
I'm not really a fan of using fixed-arrays, but if you are sure you maximum have 10 projectiles (or a fixed maximum amount), that you can indeed use a fixed array. Here is dynamic a more dynamic approach with a TList object. (Would be nicer if pascal has generics, than you could declare it like TList<TProjectile>.


Actually, since Delphi 2009 pascal has generics, I love em, TList<TProjectile>, TDictionary<TProjectile>, TQueue<TProjectile> etc!

NecroDOME
29-01-2010, 05:53 PM
Yes I know, but I didn't get that thing working... yet...