You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.6 KiB

  1. #include "Controller.h"
  2. #include <assert.h>
  3. #include <string>
  4. using namespace std;
  5. Controller::Controller()
  6. : snack{SNACK} ,inpt{0}, currentScore{0}
  7. {
  8. snake = new Snake();
  9. generateSnack(snack);
  10. }
  11. Controller::Controller(Snake *_snake)
  12. : snake{_snake}, snack{SNACK}, inpt{0}, currentScore{0}
  13. {
  14. assert(this->snake != NULL);
  15. }
  16. unsigned int Controller::getCurrScore(void){
  17. return this->currentScore;
  18. }
  19. void Controller::resetScore(void){
  20. this->currentScore = 0;
  21. }
  22. Controller::~Controller(){delete snake;}
  23. static void printScore(unsigned int _score){
  24. string str = "Score: " + to_string(_score);
  25. printMsg(-1, -1, (char*)str.c_str()); //-1, -1 because otherwise it'll be printed inside the game box
  26. }
  27. int Controller::act(void){
  28. if (snake->hasBitSnack(this->snack.getY(), this->snack.getX()) == true)
  29. {
  30. this->currentScore += 10;
  31. advanceDifficulty();
  32. generateSnack(this->snack);
  33. this->snake->incSize();
  34. printScore(currentScore);
  35. }
  36. switch (this->inpt)
  37. {
  38. case UP:
  39. snake->moveUp();
  40. break;
  41. case DOWN:
  42. snake->moveDown();
  43. break;
  44. case LEFT:
  45. snake->moveLeft();
  46. break;
  47. case RIGHT:
  48. snake->moveRight();
  49. break;
  50. default:
  51. snake->move();
  52. break;
  53. }
  54. refreshScreen();
  55. if(snake->isBitten())
  56. return DEFEAT;
  57. if (snake->hasCrashedWall())
  58. return DEFEAT;
  59. return 0;
  60. }
  61. graphics_input Controller::readInput(void){
  62. this->inpt = readInpt();
  63. refreshScreen();
  64. return inpt;
  65. }
  66. bool Controller::wantsToQuit(void){
  67. return this->inpt == EXIT_GAME;
  68. }