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 )