Implement score

This commit is contained in:
Corentin Jabot 2021-06-24 10:32:54 +02:00 committed by Patricia Aas
parent 42ee04203f
commit fedbc153ae
5 changed files with 36 additions and 7 deletions

View file

@ -17,14 +17,14 @@ Canvas::Canvas()
game_font = loadFont("joystix.ttf"); game_font = loadFont("joystix.ttf");
} }
void Canvas::update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets) { void Canvas::update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets, const Score & score) {
clear(); clear();
renderMaze(); renderMaze();
renderPellets(pellets); renderPellets(pellets);
renderSuperPellets(superPellets); renderSuperPellets(superPellets);
renderPacMan(pacMan); renderPacMan(pacMan);
renderScore(0); renderScore(score.points);
render(); render();
} }

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "Position.hpp" #include "Position.hpp"
#include "Score.hpp"
#include <optional> #include <optional>
class PacMan; class PacMan;
@ -10,7 +11,7 @@ class SuperPellets;
class Canvas { class Canvas {
public: public:
Canvas(); Canvas();
void update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets); void update(const PacMan & pacMan, const Pellets & pellets, const SuperPellets & superPellets, const Score &);
std::optional<sf::Event> pollEvent(); std::optional<sf::Event> pollEvent();
private: private:

View file

@ -2,10 +2,21 @@
#include <chrono> #include <chrono>
constexpr int DEFAULT_LIVES = 3;
constexpr int NORMAL_PELLET_POINTS = 10;
constexpr int POWER_PELLET_POINTS = 50;
//constexpr int GHOST_POINTS[] = {200, 400, 800, 1600};
Game::Game() Game::Game()
: pacMan(board), : pacMan(board),
pellets(board), pellets(board),
superPellets(board) {} superPellets(board)
{
score.lives = DEFAULT_LIVES;
}
auto Game::now() { auto Game::now() {
return std::chrono::system_clock::now(); return std::chrono::system_clock::now();
@ -35,14 +46,22 @@ void Game::run() {
accumulator -= delta_time; accumulator -= delta_time;
t += delta_time; t += delta_time;
} }
canvas.update(pacMan, pellets, superPellets); canvas.update(pacMan, pellets, superPellets, score);
} }
} }
void Game::eatPellets() { void Game::eatPellets() {
const auto pos = pacMan.positionInGrid(); const auto pos = pacMan.positionInGrid();
pellets.eatPelletAtPosition(pos); if(pellets.eatPelletAtPosition(pos)) {
superPellets.eatPelletAtPosition(pos); score.eatenPellets++;
score.points += NORMAL_PELLET_POINTS;
}
if(superPellets.eatPelletAtPosition(pos)) {
score.eatenPellets++;
score.points += POWER_PELLET_POINTS;
}
} }
void Game::processEvents(InputState & inputState) { void Game::processEvents(InputState & inputState) {

View file

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "Score.hpp"
#include "Board.hpp" #include "Board.hpp"
#include "Canvas.hpp" #include "Canvas.hpp"
#include "PacMan.hpp" #include "PacMan.hpp"
@ -20,6 +21,7 @@ private:
PacMan pacMan; PacMan pacMan;
Pellets pellets; Pellets pellets;
SuperPellets superPellets; SuperPellets superPellets;
Score score;
void eatPellets(); void eatPellets();

7
lib/Score.hpp Normal file
View file

@ -0,0 +1,7 @@
#pragma once
struct Score {
int lives = 0;
int points = 0;
int eatenPellets = 0;
};