TFT 2.8 and snake game, problem erase end pixel

Hi !

I'm trying to use a tft screen to implement a snake game, but I've a problem to erase path behind the snake. I don't know why it doesn't work. Can you help me to understand my mistake ? My code :



void moveSnakePos(Adafruit_ILI9341 tft)
{
    pollSnakePos();

  // display snake
  for (int i = 0; i < snakeSize; i++)
  {
    tft.fillRect(snakePosX[i], snakePosY[i], 3, 3, WHITE);
  }

  // remove end pixel
  for (int i = snakeSize; i > 0; i--)
  {
    snakePosX[i] = snakePosX[i - 1];
    snakePosY[i] = snakePosY[i - 1];
    tft.fillRect(snakePosX[i] , snakePosY[i] , 3, 3, PURPLE);
    }
    snakePosX[0] = snakeX;
    snakePosY[0] = snakeY;
  }

  void pollSnakePos()
  {
    if (tempKey == UP) {
      snakeY -= 1;
    }
    if (tempKey == DOWN) {
      snakeY += 1;
    }
    if (tempKey == LEFT) {
      snakeX -= 1;
    }
    if (tempKey == RIGHT) {
      snakeX += 1;
    }
  }

  void playSnake(Adafruit_ILI9341 tft)
  {
    moveSnakePos(tft);
  }

Actually the display of the snake (in WHITE) and the erase of the path (Purple) are the same. So there is no snake display. I don't understand.

Thanks a lot guys.

I don't know who can help you after you have published only part of the program. Also, I don't see you checking for limits on the display.

You are not iterating over the same range. 0..snakeSize-1 is not the same as snakeSize..1

Your erase loop is off by 1

for (int i = snakeSize-1; i >= 0; i--)

ok. But I don't understand why. With your fix it doesn't work too.

Just in case :

// ***************
// SNAKE
// ****************
// array to make body of snake :
byte snakePosX[30];
byte snakePosY[30];
// snake head position :
int snakeX = 30;
int snakeY = 30;
// snake size count limited to the size of the array
int snakeSize = 20;

Because I don't understand what's wrong, even if you try to explain me ahah.
Actually the snake blinks a lot ahah.

Please post your entire sketch. snippets are rarely helpful.

Are you trying to erase the entire snake? It seems like your code draws the entire snake, then erases the entire snake... I thought the logic of the snake was to just erase the tail (last index) whenever the head moved to a new position.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.