Browse Source

implement lesson08

lesson08
Constantin Fürst 6 months ago
parent
commit
cccf3d5389
  1. 24
      LESSONS.md
  2. 50
      game/Controller.cpp
  3. 30
      game/Controller.h
  4. 18
      main.cpp

24
LESSONS.md

@ -158,3 +158,27 @@ The snake just learned to grow by itself! How?
Once you're ready, you can _git merge lesson08_.
---
## 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!

50
game/Controller.cpp

@ -0,0 +1,50 @@
#include "Controller.h"
#include <string>
#include <iostream>
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_;
}

30
game/Controller.h

@ -0,0 +1,30 @@
#pragma once
#include <memory>
#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;
};

18
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;
}
Loading…
Cancel
Save