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.
// ***************
// 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.