I'm making my own version of Space Invaders for my A Level project and I am stuck with the collision detection. I need to detect collision when the bullet hits one of the invaders and I'm really stuck.
The invaders currently are stored in a 2d array and move on a timer, the code for this is as follows:
for Row:=1 to 5 do
begin
frmGame.Canvas.FillRect(WaveArea)
for Column:=1 to 11 do
begin
frmGame.Canvas.Draw(30+Column*50+x, 180 Images[1].Picture.Graphic);
frmGame.Canvas.Draw(30+Column*50+x, 230 Images[2].Picture.Graphic);
end;
x:=x+xShift;
end;
if x>500 then
tmrMoveInvaders.Enabled:=False;
The collision code which I have written does not work but I am not sure why. It could be the way in which the images are loaded onto the form by using a 2D array but I'm not sure.
The code for the collision procedure is:
Procedure Collision(img:TImage);
Var
TargetLeft,BulletLeft:integer;
TargetRight,BulletRight:integer;
TargetTop,BulletTop:integer;
TargetBottom,BulletBottom:integer;
Hit:boolean;
begin
with frmGame do
hit:=true;
TagetLeft:=img.Left;
BulletLeft:=shpBullet.Left;
TargetRight:=img.Left+46; //left + width of the image
BulletRight:=shpBullet.Left+8;
TargetTop:=img.Top;
BulletTop:=shpBullet.Top;
TargetBottom:=img.Top+42; //top + height of image
BulletBottom:=shpBullet.Top+15;
if (TargetBottom < BulletTop) then hit:=false;
if (TargetTop > BulletBottom) then hit:=false;
if (TargetRight < BulletLeft) then hit:=false;
if (TargetLeft > BulletRight) then hit:=false;
if not img.Visible then hit:=false;
if hit=true then
img.Visible:=false;
Any help would be greatly appreciated.