Snake Game: Collision with apple (object) lazarus / 31-LCL ---------------------------------------------------------------------- Danzino.Akz · 2019-02-14 15:06 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; ---------------------------------------------------------------------- Handoko · 2019-02-14 15:19 Hello Danzino.Akz, Welcome to the forum. If you have problem with your source code, you should provide the whole source code and all the necessary files. So we can inspect it, tell you where you did it wrong and give you some advices. To do it: Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here. Here has a detailed tutorial for writing a computer snake game: https://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.h tml#msg258381 The thread in the link above is very long and I guarantee it is boriiiiiiiing. But if you follow the tutorial, you will learn: - Game loop - User input detection - Using array to represent the the game world - Collision detection - How to use TList (to replace array) - Using buffer for keyboard input - Using buffer to reduce flickering Have fun! ---------------------------------------------------------------------- Ñuño_Martínez · 2019-02-19 10:02 Collision is quite simple, actually.  Just take a piece of paper and a pen or pencil and draw some rectangles, some of them colliding and others that doesn't.  Then figure out what coordinates and sizes they have and you'll get it. ---------------------------------------------------------------------- Thaddy · 2019-02-19 11:43 PointInRect comes to mind.... ---------------------------------------------------------------------- User137 · 2019-02-19 17:58 2D collision in pseudo: IF |object1.X - object2.X| < (object1Radius+object2Radius) THEN    Collides in X axis But doing it like that requires that visual representation of the objects are separated from ones that are moving. You would be moving the visuals to a centered position like: image1.Left:=Snake.head.X - image1.Width div 2; Is this confusing enough?    But truthfully it is always easiest to calculate these things from objects center points. You can think of snake as snake that it is, not something that is dragged from its corner. ---------------------------------------------------------------------- Handoko · 2019-02-19 20:18 Actually, collision detection in snake game is much simpler than you all have suggested. Because each snake movement, each segment of the snake, each apple occupies only exactly 1 space, the collision detection is simply checking and comparing the target location (in the array). Checking the target location is easy if you done these: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     type    TItem = (Empty, Snake, Fruit); Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     var    GameWorld: array[1..WorldWidth, 1..WorldHeight] of TItem; Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.GetItem(X, Y: Integer): TItem;begin  Result := Empty;  if (X < 1) or (X > WorldWidth) then Exit;  if (Y < 1) or (Y > WorldHeight) then Exit;  Result := GameWorld[X, Y];end; Again, I recommend OP to read the link I suggested. ---------------------------------------------------------------------- Danzino.Akz · 2019-02-23 20:15 Quote from: Handoko on February 14, 2019, 04:19:09 pmHello Danzino.Akz, Welcome to the forum. If you have problem with your source code, you should provide the whole source code and all the necessary files. So we can inspect it, tell you where you did it wrong and give you some advices. To do it: Create a new folder, copy and paste all the necessary files except: the binary (exe file), *.bak, lib and backup folders. Compress the folder and send the zip here. Here has a detailed tutorial for writing a computer snake game: https://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.h tml#msg258381 The thread in the link above is very long and I guarantee it is boriiiiiiiing. But if you follow the tutorial, you will learn: - Game loop - User input detection - Using array to represent the the game world - Collision detection - How to use TList (to replace array) - Using buffer for keyboard input - Using buffer to reduce flickering Have fun! Thank you very much, I really do appreciate it.