Recent

Author Topic: Snake Game: Collision with apple (object)  (Read 3849 times)

Danzino.Akz

  • Newbie
  • Posts: 2
Snake Game: Collision with apple (object)
« on: February 14, 2019, 04:06:36 pm »
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 :D

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

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Snake Game: Collision with apple (object)
« Reply #1 on: February 14, 2019, 04:19:09 pm »
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.html#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

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Snake Game: Collision with apple (object)
« Reply #2 on: February 19, 2019, 11:02:43 am »
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.  :)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Snake Game: Collision with apple (object)
« Reply #3 on: February 19, 2019, 12:43:15 pm »
PointInRect comes to mind....
Specialize a type, not a var.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Snake Game: Collision with apple (object)
« Reply #4 on: February 19, 2019, 06:58:11 pm »
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?  :D  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.
« Last Edit: February 19, 2019, 07:04:30 pm by User137 »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Snake Game: Collision with apple (object)
« Reply #5 on: February 19, 2019, 09:18:26 pm »
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][+][-]
  1.     type
  2.       TItem = (Empty, Snake, Fruit);

Code: Pascal  [Select][+][-]
  1.     var
  2.       GameWorld: array[1..WorldWidth, 1..WorldHeight] of TItem;

Code: Pascal  [Select][+][-]
  1. function TForm1.GetItem(X, Y: Integer): TItem;
  2. begin
  3.   Result := Empty;
  4.   if (X < 1) or (X > WorldWidth) then Exit;
  5.   if (Y < 1) or (Y > WorldHeight) then Exit;
  6.   Result := GameWorld[X, Y];
  7. end;

Again, I recommend OP to read the link I suggested.
« Last Edit: February 19, 2019, 09:22:35 pm by Handoko »

Danzino.Akz

  • Newbie
  • Posts: 2
Re: Snake Game: Collision with apple (object)
« Reply #6 on: February 23, 2019, 09:15:48 pm »
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.html#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.

 

TinyPortal © 2005-2018