added scoring system
This commit is contained in:
parent
73b4fa5920
commit
e50b5d6940
|
|
@ -36,7 +36,7 @@ struct Player
|
||||||
|
|
||||||
sf::Vector2f pos{};
|
sf::Vector2f pos{};
|
||||||
sf::Vector2f previousPos{};
|
sf::Vector2f previousPos{};
|
||||||
float score{};
|
int score{};
|
||||||
int lives{3};
|
int lives{3};
|
||||||
bool left{false};
|
bool left{false};
|
||||||
bool right{false};
|
bool right{false};
|
||||||
|
|
@ -75,12 +75,16 @@ private:
|
||||||
|
|
||||||
void soundSystem();
|
void soundSystem();
|
||||||
|
|
||||||
|
void scoreSystem();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Clock clock;
|
sf::Clock clock;
|
||||||
sf::RenderWindow window;
|
sf::RenderWindow window;
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
sf::Text lives;
|
sf::Text lives;
|
||||||
|
sf::Text score;
|
||||||
|
sf::Text currentMult;
|
||||||
sf::SoundBuffer collideSoundBuffer;
|
sf::SoundBuffer collideSoundBuffer;
|
||||||
sf::SoundBuffer brickBreakSoundBuffer;
|
sf::SoundBuffer brickBreakSoundBuffer;
|
||||||
sf::SoundBuffer failSoundBuffer;
|
sf::SoundBuffer failSoundBuffer;
|
||||||
|
|
|
||||||
44
src/Game.cpp
44
src/Game.cpp
|
|
@ -29,6 +29,8 @@ Game::Game()
|
||||||
: window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"})
|
: window({sf::VideoMode({ 1920u, 1080u }), "project-breakout"})
|
||||||
, font("assets/fonts/ChakraPetch-Regular.ttf")
|
, font("assets/fonts/ChakraPetch-Regular.ttf")
|
||||||
, lives(font)
|
, lives(font)
|
||||||
|
, score(font)
|
||||||
|
, currentMult(font)
|
||||||
, collideSoundBuffer("assets/sounds/hit2.wav")
|
, collideSoundBuffer("assets/sounds/hit2.wav")
|
||||||
, brickBreakSoundBuffer("assets/sounds/hit1.wav")
|
, brickBreakSoundBuffer("assets/sounds/hit1.wav")
|
||||||
, failSoundBuffer("assets/sounds/fail.wav")
|
, failSoundBuffer("assets/sounds/fail.wav")
|
||||||
|
|
@ -42,6 +44,9 @@ Game::Game()
|
||||||
if (!parseConfigFile()) return;
|
if (!parseConfigFile()) return;
|
||||||
|
|
||||||
lives.setPosition({10, static_cast<float>(window.getSize().y - 40)});
|
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);
|
window.setFramerateLimit(framerate);
|
||||||
|
|
||||||
collideSound.setVolume(50);
|
collideSound.setVolume(50);
|
||||||
|
|
@ -441,6 +446,7 @@ void Game::checkEndGame()
|
||||||
if (player.lives <= 0)
|
if (player.lives <= 0)
|
||||||
{
|
{
|
||||||
player.lives = 3;
|
player.lives = 3;
|
||||||
|
player.score = 0;
|
||||||
resetGame();
|
resetGame();
|
||||||
ballsToAdd.emplace_back(sf::Vector2f{player.pos.x, player.pos.y - 50});
|
ballsToAdd.emplace_back(sf::Vector2f{player.pos.x, player.pos.y - 50});
|
||||||
}
|
}
|
||||||
|
|
@ -521,7 +527,13 @@ void Game::render()
|
||||||
window.clear();
|
window.clear();
|
||||||
|
|
||||||
lives.setString("Lives: " + std::to_string(player.lives));
|
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(lives);
|
||||||
|
window.draw(score);
|
||||||
|
window.draw(currentMult);
|
||||||
|
|
||||||
tempRect.setOrigin(brickHalfSize);
|
tempRect.setOrigin(brickHalfSize);
|
||||||
tempRect.setSize(brickSize);
|
tempRect.setSize(brickSize);
|
||||||
for (auto& brick : bricks)
|
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()
|
void Game::resetGame()
|
||||||
|
|
@ -608,6 +648,8 @@ void Game::run()
|
||||||
collision();
|
collision();
|
||||||
|
|
||||||
soundSystem();
|
soundSystem();
|
||||||
|
|
||||||
|
scoreSystem();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEndGame();
|
checkEndGame();
|
||||||
|
|
@ -638,6 +680,8 @@ void Game::runNoImgui()
|
||||||
collision();
|
collision();
|
||||||
|
|
||||||
soundSystem();
|
soundSystem();
|
||||||
|
|
||||||
|
scoreSystem();
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEndGame();
|
checkEndGame();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue