# Thread: Dynamic in-game object creation and manipulation

1. Does the order really matter for bullets and possibly glowing particles and the like? I find it redundant to, say if index 10 is removed from list of 100 bullets you would move all remaining 90 or so of them 1 backwards... It is simpler to move the last index in place of the removed one. 1 operation vs 90 operations. In order for this to work, you need to use a "downto" in the for loop, going from the last item to 0.

Code:
```for b:=bullets-1 downto 0 do
with bullet[b] do begin
Move;
dec(AliveTime);
if AliveTime < 0 then begin
dec(bullets);
//bullet[b].Free; // Free it here if you must. Not needed if using records
bullet[b]:=bullet[bullets]; // Replace current bullet with last one.
end;
end;```
New bullets will then always be added to the end.

2. Hello User137!

My main problem the appearance of enemy's bullets, which happening at wrong place. Its may has to do with moving and deleting of the bullets?
I do it as follows:

Code:
```if numofbullets>0 then
begin
i:=0;
while i<numofbullets-1 do
begin
if bulletarray[i]<>nil then
begin
if (bulletarray[i].xplace>ClientWidth) or (bulletarray[i].xplace<1) or (bulletarray[i].yplace>ClientHeight) or (bulletarray[i].yplace<1) or (collwithplayer(bulletarray[i].xplace,bulletarray[i].yplace)=1) then
begin
freeandnil(bulletarray[i]);
if i=numofbullets-1 then
dec(numofbullets)
else
begin
for j:=i+1 to numofbullets-1 do
bulletarray[j-1]:=bulletarray[j];
dec(numofbullets);
dec(i);
end;
end else
begin
bulletarray[i].yplace:=bulletarray[i].yplace+4
MainWindow.Canvas.Draw(bulletarray[i].xplace,bulletarray[i].yplace,bulletarray[i].itsimage);
end;
end;
inc(i);
end;
end;```

Maybe it is wrong?

3. You don't have a semicolon at the end of
Code:
`bulletarray[i].yplace:=bulletarray[i].yplace+4`
And that's all i can see is wrong with the code. If you fire just 1 bullet, it starts from wrong place? Then it sounds like the mistake is the code that fires it.

4. The player's bullets starts from right place, but the enemy's bullets sometimes comes from wrong places, as you can see in my program which can be download from 20th post.

5. It's over 800 lines of code and not in english so it's difficult to read.

Here is something odd:
Code:
```function TFoablak.pontirany(x1,y1,x2,y2: integer): word;
var szam: integer;
begin
szam:=round(arctan2(y2-y1,x2-x1));
if szam<0 then
szam:=round(szam+2*pi);
pontirany:=round(360-(szam*180)/pi);
end;```
You should set szam: single; instead of integer, and not round it directly from arctan2(). There is too much precision lost for angle (if you have radians as integer there are only about 6 different angles in the whole 360 range.).

Also
Code:
```if numofbullets>0 then  // < This line is not needed.
begin
i:=0;
while i<numofbullets-1 do```
Lets see what happens if numofbullets = 0:
Code:
```i:=0;
while 0<-1 do```
0 is not < -1 so it never enters the while.

6. "It's over 800 lines of code and not in english so it's difficult to read."

I have written the program in Hungarian. (Maybe it's not an easy language, and you not saw it yet in full dress: with diacritics )

The function "TFoablak.pontirany" is help to direct only the movement of the boss enemy and its bullets, which initial direction are towards the player.
The bullets of the other enemies are only goes downwards (line 533: [I]if foellvan=0 then elllovedektomb[i].yhely:=elllovedektomb.yhely+4... ).
And in line 419 happening the creating of bullets of these enemies in the coordinates of actual enemy (xhely: xplace, yhely: yplace):
[I]elllovedektomb[elllovedekdb]:=TJatekEleme.Letrehoz(ellensegtomb[i].xhely,ellensegtomb.yhely,elllovkepe);
But sometimes this coordinate totally different, and I don't know, why...

7. I speak Hungarian too , but even I find it difficult to read code that is not in english.

Your english is pretty good, it would help everyone here if you would develop your code in english.

8. Originally Posted by robert83
I speak Hungarian too , but even I find it difficult to read code that is not in english.
Your english is pretty good, it would help everyone here if you would develop your code in english.
Maybe I will so, but it is better for me writing in Hungarian so far, because I can separate my variables and other things better.

9. Originally Posted by Tomi
Maybe I will so, but it is better for me writing in Hungarian so far, because I can separate my variables and other things better.
I agree with Robert's suggestion.
Having your code written in English does have a great benefit that you can just simply copy and paste part of your code when you are seeking help from others.

I myself am not native English speaking person and used to write my code in Slovenian (my native language) but eventually I figured out that it is better if I write my code in English instead. Why?
When I have all of my code written in English it allows my brains to kind of switch into "English mode" where even most of my thought process is done in English. I know it sounds a bit weird.
But when I have parts of the code written in Slovenian my brain stays in "Slovenian mode" where I'm forced of constantly having to translate English parts of the code or documentation into Slovenian which I think it slows down the thought process a bit.
And sometime it might even happen that my brain would en up being in "English mode" and I would end up constantly translating my Slovenian code into English. And yes I know that this seems even weirder.

10. No, not so weird, SilverWarior, after all you are right: if I would like help from an English-speaking programmer community, the best is when I write my codes totally in English in order to better understand.
Therefore today I have made a small program, which is focusing the "bullet appearing" problem:
Code:
```unit Unit1;

{\$mode objfpc}{\$H+}

interface

uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls;

type

{ TForm1 }

TForm1 = class(TForm)
Timer1: TTimer;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;

type TGameObject=class(TObject)
private
public
xplace,yplace,celx,cely,movedir: integer;
itsimage: TBitmap;
canshoot: boolean;
shoottimer: byte;
constructor create(xplacehere,yplacehere: longint; imagehere: Tbitmap);
end;

var
Form1: TForm1;
backgroundimg,bulletimg,spaceshipimg: TBitMap;
spaceship: TGameObject;
bullet: array [0..100] of TGameObject;
numofbullets: integer;

implementation

{\$R *.lfm}

constructor TGameObject.create(xplacehere,yplacehere: integer; imagehere: Tbitmap);
begin
xplace:=xplacehere;
yplace:=yplacehere;
itsimage:=imagehere;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var initialmovedir: shortint;
begin
backgroundimg := TBitmap.Create;
bulletimg:=TBitMap.Create;
bulletimg.transparent:=true;
spaceshipimg:=TBitMap.Create;
spaceshipimg.transparent:=true;
randomize;
if round(random(2))=1 then initialmovedir:=-2 else initialmovedir:=2;
spaceship:=TGameObject.create(Form1.ClientWidth div 2,spaceshipimg.height,spaceshipimg);
spaceship.movedir:=initialmovedir;
spaceship.shoottimer:=100;
spaceship.canshoot:=false;
numofbullets:=0;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
backgroundimg.free;
bulletimg.free;
spaceshipimg.free;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var i,j: integer;
begin
Form1.Canvas.StretchDraw(Rect(0, 0, ClientWidth, ClientHeight), backgroundimg);
if spaceship.shoottimer>0 then dec(spaceship.shoottimer) else spaceship.canshoot:=true;
if (spaceship.xplace+spaceship.movedir>=ClientWidth-spaceshipimg.width) or (spaceship.xplace+spaceship.movedir<=1) then
begin
if spaceship.yplace+spaceshipimg.height<ClientHeight then
spaceship.yplace:=spaceship.yplace+spaceshipimg.height
else
spaceship.yplace:=spaceshipimg.height;
spaceship.movedir:=-spaceship.movedir;
end
else
begin
spaceship.xplace:=spaceship.xplace+spaceship.movedir;
end;
Form1.Canvas.Draw(spaceship.xplace,spaceship.yplace,spaceshipimg);
if (round(random(50))=1) and (spaceship.canshoot=true) then
begin
bullet[numofbullets]:=TGameObject.create(spaceship.xplace,spaceship.yplace,bulletimg); {!!!WRONG!!!}
inc(numofbullets);
spaceship.canshoot:=false;
spaceship.shoottimer:=100;
end;

if numofbullets>0 then
begin
i:=0;
while i<numofbullets-1 do
begin
if bullet[i]<>nil then
begin
if bullet[i].yplace>ClientHeight then
begin
freeandnil(bullet[i]);
if i=numofbullets-1 then
dec(numofbullets)
else
begin
for j:=i+1 to numofbullets-1 do
bullet[j-1]:=bullet[j];
dec(numofbullets);
dec(i);
end;
end
else
begin
bullet[i].yplace:=bullet[i].yplace+4;
Form1.Canvas.Draw(bullet[i].xplace,bullet[i].yplace,bulletimg);
end;
end;
inc(i);
end;
end;
end;

end.```
Maybe somebody can help me...

Page 3 of 4 First 1234 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•