diff --git a/include/Game.h b/include/Game.h index cb5f34f..970a786 100644 --- a/include/Game.h +++ b/include/Game.h @@ -36,7 +36,7 @@ struct Player sf::Vector2f pos{}; sf::Vector2f previousPos{}; - float score{}; + int score{}; int lives{3}; bool left{false}; bool right{false}; @@ -75,12 +75,16 @@ private: void soundSystem(); + void scoreSystem(); + private: sf::Clock clock; sf::RenderWindow window; sf::Font font; sf::Text lives; + sf::Text score; + sf::Text currentMult; sf::SoundBuffer collideSoundBuffer; sf::SoundBuffer brickBreakSoundBuffer; sf::SoundBuffer failSoundBuffer; diff --git a/src/Game.cpp b/src/Game.cpp index 8c3c180..5a9fbef 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -29,6 +29,8 @@ Game::Game() : window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"}) , font("assets/fonts/ChakraPetch-Regular.ttf") , lives(font) + , score(font) + , currentMult(font) , collideSoundBuffer("assets/sounds/hit2.wav") , brickBreakSoundBuffer("assets/sounds/hit1.wav") , failSoundBuffer("assets/sounds/fail.wav") @@ -42,6 +44,9 @@ Game::Game() if (!parseConfigFile()) return; lives.setPosition({10, static_cast(window.getSize().y - 40)}); + score.setPosition({10, static_cast(window.getSize().y - 80)}); + currentMult.setPosition({10, static_cast(window.getSize().y - 120)}); + window.setFramerateLimit(framerate); collideSound.setVolume(50); @@ -441,6 +446,7 @@ void Game::checkEndGame() if (player.lives <= 0) { player.lives = 3; + player.score = 0; resetGame(); ballsToAdd.emplace_back(sf::Vector2f{player.pos.x, player.pos.y - 50}); } @@ -521,7 +527,13 @@ void Game::render() window.clear(); lives.setString("Lives: " + std::to_string(player.lives)); + score.setString("Score: " + std::to_string(player.score)); + currentMult.setString("Mult: x" + std::to_string(balls.size())); + window.draw(lives); + window.draw(score); + window.draw(currentMult); + tempRect.setOrigin(brickHalfSize); tempRect.setSize(brickSize); for (auto& brick : bricks) @@ -579,6 +591,34 @@ void Game::soundSystem() } } + for (auto& brick : specialBricks) + { + if (!brick.alive) + { + brickBreakSound.play(); + } + } + +} + +void Game::scoreSystem() +{ + const int mult = balls.size(); + for (auto& brick : bricks) + { + if (!brick.alive) + { + player.score += 100 * mult; + } + } + + for (auto& brick : specialBricks) + { + if (!brick.alive) + { + player.score += 100 * mult; + } + } } void Game::resetGame() @@ -608,6 +648,8 @@ void Game::run() collision(); soundSystem(); + + scoreSystem(); } checkEndGame(); @@ -638,6 +680,8 @@ void Game::runNoImgui() collision(); soundSystem(); + + scoreSystem(); } checkEndGame();