added scoring system

This commit is contained in:
Joseph Aquino 2025-09-24 18:57:12 -04:00
parent 73b4fa5920
commit e50b5d6940
2 changed files with 49 additions and 1 deletions

View File

@ -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;

View File

@ -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<float>(window.getSize().y - 40)});
score.setPosition({10, static_cast<float>(window.getSize().y - 80)});
currentMult.setPosition({10, static_cast<float>(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();