diff --git a/LESSONS.md b/LESSONS.md index 27f5362..4342754 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -157,4 +157,28 @@ The snake just learned to grow by itself! How? Once you're ready, you can _git merge lesson08_. ---- \ No newline at end of file +--- + +## Lesson 08 + +#### Prequisites +None + +#### Files +- _game/Controller.cpp_ +- _game/Controller.h_ +- _main.cpp_ + +#### Task + +Even though our snake and snack are ready, we a re still not at the point where we can play. This is where the game controller comes in. + +1) Inspect the new files in _game/Controller.*_ and try to figure out their purpose. +2) In the main game-function called _Controller::act()_ there are still some unimplemented sections. Fix that! +3) There is a rudimentary game loop implemented for your use in _main.cpp_ as __mainL08__ with which you can test out what you have achieved. + +--- + +## Improvements + +If youre still hungy for more learning, check out the master branch which does things a little differently. Try to understand everything and maybe fix the weird speed difference between vertical and horizontal snake travel (Graphics has the set-vertical which you could use). You could also implement a highscore system and allow the game state to be written to a file in order to keep high scores. Good luck and thanks for working through this! \ No newline at end of file diff --git a/game/Controller.cpp b/game/Controller.cpp new file mode 100644 index 0000000..73a6399 --- /dev/null +++ b/game/Controller.cpp @@ -0,0 +1,50 @@ +#include "Controller.h" + +#include +#include + +Controller::Controller() + : input_{0}, score_{0}, snack_{Point(0,0,0)} +{ + generateSnack(&snack_); +} + +void Controller::printScore(uint32_t score) const { + const std::string str = "Score: " + std::to_string(score); + // locate message at (-1,-1) because otherwise it'll be printed inside the game box + Graphics::get().printMsg(-1, -1, str); +} + +int Controller::act() { + readInput(); + + if (snake_.hasBitSnack(snack_.getY(), snack_.getX())) { + score_ += 10; + snake_.incSize(); + + generateSnack(&snack_); + Graphics::get().advanceDifficulty(); + + printScore(score_); + } + + // TODO: perform action depending on value of input_ - see "Snake::updateHead" for some guidance + + Graphics::get().refreshScreen(); + + // TODO: handle bit-itself and crashed-wall case by returning DEFEAT + + return 0; +} + +void Controller::readInput() { + input_ = Graphics::get().readInpt(); +} + +bool Controller::wantsToQuit() const { + return input_ == EXIT_GAME; +} + +uint32_t Controller::getScore() const { + return score_; +} diff --git a/game/Controller.h b/game/Controller.h new file mode 100644 index 0000000..50b9d24 --- /dev/null +++ b/game/Controller.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +#include "../input-output/Graphics.h" +#include "../components/Snake.h" +#include "../components/Point.h" + +static constexpr int DEFEAT = -1; + +class Controller { +private: + Snake snake_; + Point snack_; + + int input_; + uint32_t score_; + + void printScore(uint32_t score) const; + void readInput(); + +public: + Controller(); + + int act(); + + bool wantsToQuit() const; + + uint32_t getScore() const; +}; diff --git a/main.cpp b/main.cpp index 17abfd2..eabe040 100755 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include "components/Point.h" #include "components/Snack.h" #include "components/Snake.h" +#include "game/Controller.h" #include "input-output/Graphics.h" void mainL01() { @@ -105,7 +106,22 @@ void mainL06() { std::cout << "Helper QUIT" << std::endl; } +void mainL08() { + Graphics::get().init("Snake Game made by YOU"); + + Controller controller; + + while(true) { + controller.act(); + if (controller.wantsToQuit()) break; + } + + Graphics::get().finalize(); + + std::cout << "Final Score: " << controller.getScore() << std::endl; +} + int main() { - mainL06(); + mainL08(); return 0; } \ No newline at end of file