I am trying to create a random place for the piece of fruit to be placed on the board and it does work but when my snake 'O' eats the 'F' piece,the 'F' does not disappear instead it stays there and worst yet sometimes without even eating it,it will randomly disappear and a new piece of fruit will appear in a new place,can anyone see why this is happening?
bool gameOver;
const int WIDTH = 20;
const int HEIGHT = 20;
int fruitX,fruitY,x,y;
edir dir;
void setUp(){
gameOver = false;
dir = STOP;
x = WIDTH/2;
y = HEIGHT/2;
srand(time(NULL));
fruitX = rand() % WIDTH-1;
fruitY = rand() % HEIGHT-1;
if(x == fruitX && y == fruitY){
fruitX = (rand() % WIDTH-1)+1;
fruitY = (rand() % HEIGHT-1)+1;
}
}
void draw(){
system("cls");
for(int i = 0; i < WIDTH+1; i++){
cout << "#";
}
cout << endl;
for(int i = 0; i < HEIGHT; i++){
for(int j = 0; j < WIDTH; j++){
if(j == 0){
cout << "#";
}
if(j == WIDTH-1){
cout << "#";
}
if(i == x && j == y){
cout << "O";
}
else if(i == fruitY && j == fruitX){
cout << "F";
}
else{
cout << " ";
}
}
cout << endl;
}
for(int i = 0; i < WIDTH+1; i++){
cout << "#";
}
}
void logic(){
switch(dir){
case UP:
x--;
break;
case DOWN:
x++;
break;
case LEFT:
y--;
break;
case RIGHT:
y++;
break;
}
if(x > WIDTH || x < 0 || y > HEIGHT-2 || y < 0) // check for in bounds
gameOver = true;
if(x == fruitX && y == fruitY){
fruitX = (rand() % WIDTH-1)+1;
fruitY = (rand() % HEIGHT-1)+1;
}
}
int main()
{
setUp();
while(!gameOver){
draw();
input();
logic();
}
if(gameOver){
cout << endl;
cout << endl;
cout << "GAME OVER!!!!!!!!!!!!" << endl;
}
}
Aucun commentaire:
Enregistrer un commentaire