From a0780c7a8c603243cb473155c20364b15d54d135 Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Thu, 24 Jun 2021 10:44:13 +0200 Subject: [PATCH] Display the number of remaining lives --- lib/Canvas.cpp | 14 ++++++++++++++ lib/Canvas.hpp | 1 + 2 files changed, 15 insertions(+) diff --git a/lib/Canvas.cpp b/lib/Canvas.cpp index b58bdd4..525705a 100644 --- a/lib/Canvas.cpp +++ b/lib/Canvas.cpp @@ -25,6 +25,7 @@ void Canvas::update(const PacMan & pacMan, const Pellets & pellets, const SuperP renderSuperPellets(superPellets); renderPacMan(pacMan); renderScore(score.points); + renderLives(score.lives); render(); } @@ -85,6 +86,19 @@ void Canvas::renderScore(int score) { window.draw(text); } +void Canvas::renderLives(int lives) { + constexpr PositionInt liveSprite{3, 0}; + const int x = LEFT_MARGIN + MAZE_WIDTH + LEFT_MARGIN; + const int y = maze_texture.getSize().y; + + Sprite pacmanSprite = getSprite(liveSprite); + for(int i = 0; i < lives - 1; i++) { + PositionInt pos{x + i * pacmanSprite.getTextureRect().width, y}; + pacmanSprite.setPosition(pos.x, pos.y); + window.draw(pacmanSprite); + } +} + Rect Canvas::windowDimensions() { return { 0, 0, LEFT_MARGIN + MAZE_WIDTH + SCORE_WIDTH, TOP_MARGIN + MAZE_HEIGHT + BOTTOM_MARGIN }; } diff --git a/lib/Canvas.hpp b/lib/Canvas.hpp index 4268789..002c09f 100644 --- a/lib/Canvas.hpp +++ b/lib/Canvas.hpp @@ -33,6 +33,7 @@ private: void renderSprite(Sprite sprite, Position pos); void renderScore(int score); + void renderLives(int lives); static Rect windowDimensions(); static sf::Texture loadTexture(std::string_view path);