Constantin Fürst
6 months ago
4 changed files with 122 additions and 2 deletions
-
26LESSONS.md
-
50game/Controller.cpp
-
30game/Controller.h
-
18main.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_; |
|||
} |
@ -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; |
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue