From fedbc153aeb535b0307cf40f0a95417f8602233e Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Thu, 24 Jun 2021 10:32:54 +0200 Subject: [PATCH] Implement score --- lib/Canvas.cpp | 4 ++-- lib/Canvas.hpp | 3 ++- lib/Game.cpp | 27 +++++++++++++++++++++++---- lib/Game.hpp | 2 ++ lib/Score.hpp | 7 +++++++ 5 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 lib/Score.hpp diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index c3bf46e..b58bdd4 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -17,14 +17,14 @@ Canvas::Canvas() 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(); renderMaze(); renderPellets(pellets); renderSuperPellets(superPellets); renderPacMan(pacMan); - renderScore(0); + renderScore(score.points); render(); } diff --git a/lib/Canvas.hpp b/lib/Canvas.hpp index 2748ac1..4268789 100644 --- a/lib/Canvas.hpp +++ b/lib/Canvas.hpp @@ -1,6 +1,7 @@ #pragma once #include "Position.hpp" +#include "Score.hpp" #include class PacMan; @@ -10,7 +11,7 @@ class SuperPellets; class Canvas { public: 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 pollEvent(); private: diff --git a/lib/Game.cpp b/lib/Game.cpp index cdaf5e3..f3fb914 100644 --- a/lib/Game.cpp +++ b/lib/Game.cpp @@ -2,10 +2,21 @@ #include +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() : pacMan(board), pellets(board), - superPellets(board) {} + superPellets(board) +{ + score.lives = DEFAULT_LIVES; +} auto Game::now() { return std::chrono::system_clock::now(); @@ -35,14 +46,22 @@ void Game::run() { accumulator -= delta_time; t += delta_time; } - canvas.update(pacMan, pellets, superPellets); + canvas.update(pacMan, pellets, superPellets, score); } } void Game::eatPellets() { const auto pos = pacMan.positionInGrid(); - pellets.eatPelletAtPosition(pos); - superPellets.eatPelletAtPosition(pos); + if(pellets.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) { diff --git a/lib/Game.hpp b/lib/Game.hpp index f4e606d..abac7a7 100644 --- a/lib/Game.hpp +++ b/lib/Game.hpp @@ -1,5 +1,6 @@ #pragma once +#include "Score.hpp" #include "Board.hpp" #include "Canvas.hpp" #include "PacMan.hpp" @@ -20,6 +21,7 @@ private: PacMan pacMan; Pellets pellets; SuperPellets superPellets; + Score score; void eatPellets(); diff --git a/lib/Score.hpp b/lib/Score.hpp new file mode 100644 index 0000000..b713cd7 --- /dev/null +++ b/lib/Score.hpp @@ -0,0 +1,7 @@ +#pragma once + +struct Score { + int lives = 0; + int points = 0; + int eatenPellets = 0; +};