Making of: Snake eating fruit

Hi everyone, I’m about to finish my Snake mini-game, everything works properly and I cane even say I added some cool features that make the game much better than the classic version!
Anyways, I tried to make the Snake object using a dynamic array (or vector) of objects from a class called, as I said, Snake… but I have a function that makes the snake itself change its dimension, adding one object at the end of the snake, and grow up as I eat a fruit, that doesn’t work as it’s supposed to. The problem is, I can’t calculate the coords of the new piece of the snake, because it appears as a “separate entity” and it doesn’t stick to the rest of the snake, so when it grows up it’s easy for that new snake piece to collide with the rest of the snake and die. Can you help me?

PS I’m gonna paste here only the function to make it grow, the conditions are OK and they’re in another piece of the code… thank you very much!

(I’m gonna add an image to explain what I’m talking about as a “separate entity”)

Here’s the code:

void redAppleRule() {

  //x and y are the fixed coords, pX and pY are the temp ones
  nPieces.add(new Snake());
  nPieces.get(nPieces.size()-1).pX=nPieces.get(nPieces.size()-2).pX;
  nPieces.get(nPieces.size()-1).pY=nPieces.get(nPieces.size()-2).pY;
}

Bit difficult to guess where the error is without the rest of the code. If the code is big post it on open processing.

1 Like