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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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!");
		}
  }	
}
Once I saw your first post, I started making snake! lol.

I solved this by not erasing the head of the snake, but the last part of the body.

To do this, I strored each body part( x and y values ) in to arrays. and then use the gotoXY() to go to the last in the array to erase it.

I use arrays to store each body part, because you'll need this later to check if the head is going to hit it, for the snake to die.

I'm just about done now, I'll be posting it on my site very soon, so you can download it and have a look at it.


EDIT:
Whoops, for some reason I thought that was about the snake! lol. I'll leave that up there anyway.

For the food, make a do/while loop, while it's not on the snakes head.

1
2
3
4
5
6
do
{
	ranX = randomRange( G::WIDTH_LOW + 1, G::WIDTH_HIGH - 1 );
	ranY = randomRange( G::HEIGHT_LOW + 1, G::HEIGHT_HIGH - 1 );

}while( ( ranX == Snake.getXPos() ) && ( ranY == Snake.getYPos() ) );


randomRange() :
1
2
3
4
5
6
7
8
9
int gameLoop::randomRange( int _Min, int _Max )
{
	//add 1 to the max, so it's included in the result.
	_Max += 1;

	int x = _Min + ( rand() % ( _Max - _Min ) );

	return x;
}


This is area of the screen it will appear:
1
2
	ranX = randomRange( 2, 78 );
	ranY = randomRange( 2, 23 );


While it's not on the snakes head. (Haven't done the body for some reason... lol )
Last edited on
Here's my finished( sort of finished, lol ) game:

http://sites.google.com/site/davevisone/home/cplusplus-programming-download

All the code is in the .zip, and there's another .zip within it, containing the game.
By the way... Line 32 is wrong.

if (snakeX[0] == food_x && snakeY[0] == food_y || snakeX[0] == food_y || snakeY[0] == food_x) // Collision detector with the food.

If the snake head is at, lets say: X.2 and Y.3
And the food is at: X.3 and Y.2

1
2
snake: x2 y3
food:  x3 y2


When you 'swap' them opposite, the head will collide with the food!
1
2
snakeX[0] == food_y || snakeY[0] == food_x
   2            2         3          3


But they are not actually in the same place.
Topic archived. No new replies allowed.