diff --git a/components/Snake.cpp b/components/Snake.cpp new file mode 100644 index 0000000..f2482fe --- /dev/null +++ b/components/Snake.cpp @@ -0,0 +1,78 @@ +#include "Snake.h" + +#include + +Snake::Snake(uint32_t headY, uint32_t headX) + :direction_{LEFT} +{ + snake_.push_back(Point{headY, headX, '>'}); + + for (uint32_t i = 1; i <= SNAKE_DEFAULT_SIZE; i++) { + snake_.push_back(Point{headY, headX + i, SNAKE_BODY_CHAR}); + } +} + +void Snake::updateHead() { + switch (direction_) { + case UP: + snake_.front().moveUp(); + break; + case DOWN: + snake_.front().moveDown(); + break; + case LEFT: + snake_.front().moveLeft(); + break; + case RIGHT: + snake_.front().moveRight(); + break; + default: + std::cerr << "[x] This direction does not exist" << std::endl; + // OOPS! + } +} + +void Snake::printSnake() const { + // TODO: for each point in snake_ call point.print() + + Graphics::get().refreshScreen(); +} + +void Snake::move() { + auto head = snake_.begin(); + auto second = std::next(snake_.begin()); + + // update the previous to head node + // by copying from head and setting + // the image to be body instead of head + *second = *head; + second->setImg(SNAKE_BODY_CHAR); + + updateHead(); + + printSnake(); +} + +void Snake::moveUp(){ + snake_.front().setImg('v'); + direction_ = UP; + move(); +} + +void Snake::moveDown(){ + snake_.front().setImg('^'); + direction_ = DOWN; + move(); +} + +void Snake::moveLeft(){ + snake_.front().setImg('>'); + direction_ = LEFT; + move(); +} + +void Snake::moveRight(){ + snake_.front().setImg('<'); + direction_ = RIGHT; + move(); +} diff --git a/components/Snake.h b/components/Snake.h new file mode 100644 index 0000000..5aa646c --- /dev/null +++ b/components/Snake.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +#include "Point.h" +#include "Snack.h" + +static constexpr int SNAKE_BODY_CHAR = 'o'; +static constexpr uint32_t SNAKE_DEFAULT_SIZE = 4; + +class Snake{ +private: + std::vector snake_; + int direction_; + + void updateHead(); + void printSnake() const; + +public: + Snake(uint32_t headY = LINES/2, uint32_t headX = COLS/2); + + void moveUp(); + void moveDown(); + void moveLeft(); + void moveRight(); + void move(); +}; diff --git a/main.cpp b/main.cpp index bde253d..9358868 100755 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "components/Point.h" #include "components/Snack.h" +#include "components/Snake.h" #include "input-output/Graphics.h" void mainL01() { @@ -35,7 +36,7 @@ void mainL02() { } void mainL03() { - Graphics::get().init("Learners Helper 02"); + Graphics::get().init("Learners Helper 03"); Point p(10,10); @@ -65,7 +66,26 @@ void mainL03() { std::cout << "Helper QUIT" << std::endl; } +void mainL04() { + Graphics::get().init("Learners Helper 04"); + + Snake snake; + + for (uint32_t i = 0; i < 100; i++) { + snake.moveDown(); + Graphics::get().refreshScreen(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + snake.moveRight(); + Graphics::get().refreshScreen(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + + Graphics::get().finalize(); + + std::cout << "Helper QUIT" << std::endl; +} + int main() { - mainL03(); + mainL04(); return 0; } \ No newline at end of file