# Thread: how to reprint food WITHIN the borders in snake..

1. ## how to reprint food WITHIN the borders in snake..

If you visit this forum daily, you would notice that I have been posting topics about the snake game recently. I've been able to move the snake already and now, my problem is to reprint the food WITHIN the borders after it has been eaten because when it prints after it has been eaten, it sometimes prints outside the border. Need help here guys. Thank you. Here's my code btw.
Code:
```while (snake == 1)
{
delay(40000000); // Delay of the loop.
gotoxy (snakeX[0], snakeY[0]);
printf("*");
gotoxy (snakeX[nLength], snakeY[nLength]); // This erases the "trail" the snake leaves behind.
printf(" ");
for (i = nLength; i >= 1; i--) // Passing of values on the array. This means that the value of nSnakeX/Y[6] will be equivalent to the value of nSnakeX/Y[5] and so on. So nSnakeX/Y[1] would be equal to nSnake[0] and that makes the body of the snake.
{
snakeX[i] = snakeX[i - 1];
snakeY[i] = snakeY[i - 1];
}

if ( kbhit() )
{
nDir = getch();
if (nDir == 0)
nDir = getch(); // Detects arrow key input.
}

if ((nDir == UP && nDir2 != DOWN) || (nDir == DOWN && nDir2 != UP) || (nDir == LEFT && nDir2 != RIGHT) || (nDir == RIGHT && nDir2 != LEFT))
nDir2 = nDir; // This is used in order for the snake not to move in reverse. Example, if the snake is moving up, it will NOT move down because of the if statement.

switch (nDir2)
{
case UP: snakeY[0]--; break;
case DOWN: snakeY[0]++; break;
case LEFT: snakeX[0]--; break;
case RIGHT: snakeX[0]++; break;
}

if (snakeX[0] == food_x && snakeY[0] == food_y || snakeX[0] == food_y || snakeY[0] == food_x) // Collision detector with the food.
{
srand(time(NULL));
food_x = rand() % LENGTH + 2;
food_y = rand() % WIDTH + 2;
gotoxy (food_x, food_y);
printf("A");
}
if (nScore % 2 == 0 && nScore != 0) // Snake gets longer if the updated/current score is divisible by two.
nLength++;
if (snakeY[0] == 0 || snakeX[0] == 0 || snakeY[0] == LENGTH * 2 - 2 || snakeX[0] == WIDTH - 1) // Collision detector with the walls.
{
snake = 0;
gotoxy(33, 10);
printf("Game Over!");
}
}
}```

2. > srand(time(NULL));
Do this ONCE at the start of main.
Calling srand() more than once - and especially if in a loop can make things decidedly non-random.

> food_x = rand() % LENGTH + 2;
> food_y = rand() % WIDTH + 2;
How does 2..LENGTH+1 seem as a valid range to you?

Perhaps some more constants which describe the actual dimensions of the field would help you make better calculations.

Popular pages Recent additions