bool Core::posFood(const Food &food) { if ((food.getPosX() == _pos[0].getPosX()) && (food.getPosY() == _pos[0].getPosY())) setFood(true); else setFood(false); return (true); }
void Core::displayBegin(const IGui *gui, const Food &food) const { int i; i = 0; if (gui) { while (i <= SIZE_SNAKE) { gui->displayBody(_pos[i].getPosX(), _pos[i].getPosY()); i++; } gui->displayFood(food.getPosX(), food.getPosY()); } }
void Core::loopBegin(const IGui *gui, Food food, t_key key) { int score; score = 0; if (gui) { displayBegin(gui, food); while ((key != ESCAPE_KEY) && (collisionWithWall(_pos[0].getPosX(), _pos[0].getPosY())) && (collisionWithSnake(_pos[0].getPosX(), _pos[0].getPosY())) && (posFood(food))) { if (getFood()) { food.putFood(_pos, _width, _height); gui->displayFood(food.getPosX(), food.getPosY()); score += 100; } if (!getFood()) { gui->displayAfterFood(_pos[_pos.size() - 1].getPosX(), _pos[_pos.size() - 1].getPosY()); _pos.pop_back(); } key = gui->manageKey(); if (key == ESCAPE_KEY) std::cout << "You're a poor loser, you leave the game !" << std::endl; if (key == SPEEDP_KEY || key == SPEEDL_KEY) setSpeed(key); else setDirectionSnake(key); moveDirection(); gui->displayBody(_pos[0].getPosX(), _pos[0].getPosY()); gui->displayScore(score); if (_speed <= 0) _speed += -(_speed); usleep(_speed); } } }