Hey,
I am trying to make a maths game in Free Pascal for my A level coursework. I have written code for a collision between the snake (which is just a box for now) and the apples. I have also written some code that randomizes the apples' positions after a collision. The randomisation works, however, I have realised that the collisions are very off and sometimes do not happen when the snake is touching the object. I think it might have something to do with the coordinates (.left and .width). Thanks for your help
This is my code for the collision:
begin
if (
(Image1.Left > Shape1.Left)
and (Image1.Left + Image1.Width <= Shape1.Left + Shape1.Width)
and (Image1.Top + Image1.Height >= Shape1.top)
and (DirectionTop = False)
)
or
((Image2.Left > Shape1.Left)
and (Image2.Left + Image2.Width <=Shape1.Left + Shape1.Width)
and (Image2.Top + Image2.Height >= Shape1.top)
and (DirectionTop = False))
or
((Image2.Left > Shape1.Left)
and (Image2.Left + Image2.Width <= Shape1.Left + Shape1.Width)
and (Image2.Top + Image2.Height >= Shape1.top)
and (DirectionTop = False)
)
or
((Image3.Left > Shape1.Left)
and (Image3.Left + Image3.Width <= Shape1.Left + Shape1.Width)
and (Image3.Top + Image3.Height >= Shape1.top)
and (DirectionTop = False)
)
or
((Image4.Left > Shape1.Left)
and (Image4.Left + Image4.Width <= Shape1.Left + Shape1.Width)
and (Image4.Top + Image4.Height >= Shape1.top)
and (DirectionTop = False)
)
then begin
randomize();
h1:=random(642);
w1:=random(1050);
h2:=random(642);
w2:=random(1050);
h3:=random(642);
w3:=random(1050);
h4:=random(642);
w4:=random(1050);
image1.top:=h1;
image1.Left:=w1;
image2.top:=h2;
image2.Left:=w2;
image3.top:=h3;
image3.Left:=w3;
image4.top:=h4;
image4.Left:=w4;
//include code to increase speed of snake
score:= score + 1;
Label1.caption:= IntToStr(score);
end;
end;