diff --git a/include/Controller.h b/include/Controller.h index fbc3463..b17a367 100644 --- a/include/Controller.h +++ b/include/Controller.h @@ -8,6 +8,7 @@ class Controller{ private: Snake *snake; //the figure the controler controls + Point snack; graphics_input inpt; public: Controller(Snake* _snake); diff --git a/include/Graphics.h b/include/Graphics.h index 1f36bc9..a02d416 100644 --- a/include/Graphics.h +++ b/include/Graphics.h @@ -11,9 +11,9 @@ typedef int graphics_input; //the type of the graphics input #define GAME_TOP_WALL_Y 1 -#define GAME_BOTTOM_WALL_Y LINES+1 -#define GAME_LEFT_WALL_X -1 -#define GAME_RIGHT_WALL_X COLS+1 +#define GAME_BOTTOM_WALL_Y (LINES - 4) +#define GAME_LEFT_WALL_X 1 +#define GAME_RIGHT_WALL_X (COLS - 2) void initializeGraphics(void); void endGraphics(void); diff --git a/include/Snack.h b/include/Snack.h index c97382b..09aa99e 100644 --- a/include/Snack.h +++ b/include/Snack.h @@ -4,3 +4,6 @@ #define SNACK '*' +//snack methods +void generateSnack(Point &snack); +unsigned int getSnackScore(void); diff --git a/src/Controller.cpp b/src/Controller.cpp index b66ce02..43dde6f 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -4,13 +4,15 @@ using namespace std; Controller::Controller() -:inpt{0} +: snack{SNACK} ,inpt{0} { snake = new Snake(); + generateSnack(snack); } Controller::Controller(Snake *_snake) -: snake{_snake}, inpt{0} { +: snake{_snake}, snack{SNACK}, inpt{0} +{ assert(this->snake != NULL); } @@ -18,6 +20,13 @@ Controller::Controller(Snake *_snake) Controller::~Controller(){} int Controller::act(void){ + + if (snake->hasBitSnack(this->snack.getY(), this->snack.getX()) == true) + { + generateSnack(this->snack); + this->snake->incSize(); + } + switch (this->inpt) { case UP: @@ -36,8 +45,9 @@ int Controller::act(void){ snake->move(); break; } + refreshScreen(); - + if(snake->isBitten()) return DEFEAT; if (snake->hasCrashedWall()) diff --git a/src/Graphics.cpp b/src/Graphics.cpp index c04327f..77f57e2 100644 --- a/src/Graphics.cpp +++ b/src/Graphics.cpp @@ -1,8 +1,11 @@ #include "Graphics.h" #include #include +#include +#include + ///Utillities for the graphics -unsigned int sleepTime = 50000000; +unsigned int sleepTime = 40000000; static WINDOW *_box = NULL; static void createBox(void){ @@ -17,6 +20,8 @@ static void destroyBox(void){ } //Initialization function void initializeGraphics(void){ + srand((unsigned int) time(NULL)); + initscr(); //initialize curses cbreak(); //set line buffering to false noecho(); //set input echo to false diff --git a/src/Snack.cpp b/src/Snack.cpp new file mode 100644 index 0000000..910ccd8 --- /dev/null +++ b/src/Snack.cpp @@ -0,0 +1,22 @@ +#include "Snack.h" +#include + +using namespace std; + + +const unsigned int snackScore = 10; + +void generateSnack(Point &snack){ + + int x, y; + + x = (rand() + 1) % GAME_RIGHT_WALL_X; + y = (rand() + 1) % GAME_BOTTOM_WALL_Y; + + snack.setPoint(y, x); + + printChar(y, x, snack.getImg()); +} +unsigned int getSnackScore(void){ + return snackScore; +} \ No newline at end of file diff --git a/src/SnakeImpl.cpp b/src/SnakeImpl.cpp index 5385729..d1782ac 100755 --- a/src/SnakeImpl.cpp +++ b/src/SnakeImpl.cpp @@ -6,7 +6,7 @@ Snake::Snake(int headY, int headX) :direction{LEFT} { snake.push_back(Point{headY, headX, '>'}); //add the head of the snake - for(int i=1; i<=7; i++) + for(int i=1; i<=3; i++) snake.push_back(Point{headY, headX+i, BODY}); this->printSnake(); } @@ -28,7 +28,8 @@ bool Snake::isBitten(void){ } bool Snake::hasBitSnack(int snackY, int snackX){ - return readChar(snackY, snackX) == SNACK; + return snake.begin()->getY() == snackY + && snake.begin()->getX() == snackX; } bool Snake::hasCrashedWall(void){