We are nearly there. This short chapter will demonstrate how we can easily manipulate files stored on the hard drive using the C++ standard library, and we will also add sound effects. Of course, we know how to add sound effects, but we will discuss exactly where in the code the calls to play
will go. We will also tie up a few loose-ends to make the game complete.
In this chapter we will learn the following topics:
File I/O, or input/output, is a fairly technical subject. Fortunately for us, as it is such a common requirement in programming, there is a library that handles all the complexity for us. As with concatenating strings for our HUD, it is the Standard Library that provides the necessary functionality through fstream
.
First, we include fstream
in the same way we included sstream
:
#include "stdafx.h"
#include <sstream>
#include <fstream>
#include <SFML/Graphics.hpp>
#include "ZombieArena.h"
#include "Player.h"
#include "TextureHolder.h"
#include "Bullet.h"
#include "Pickup.h"
using namespace sf;
Now, add a new folder in the ZombieArena/ZombieArena
folder called gamedata
. Next, right-click in this folder and create a new file called scores.txt
. It is in this file that we will save the player's high-score. You could open the file and add a score to it. If you do, make sure it is quite a low score so we can easily test whether beating that score results in the new score being added. Be sure to close the file once you are done with it or the game will not be able to access it.
In the next code, we create an ifstream
object called InputFile
and send the folder and file we just created as a parameter to its constructor.
The if(InputFile.is_open())
code checks that the file exists and is ready to read from. We then put the contents of the file into hiScore
and close the file. Add the highlighted code:
// Score
Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(55);
scoreText.setFillColor(Color::White);
scoreText.setPosition(20, 0);
// Load the high-score from a text file
std::ifstream inputFile("gamedata/scores.txt");
if (inputFile.is_open())
{
inputFile >> hiScore;
inputFile.close();
}
// Hi Score
Text hiScoreText;
hiScoreText.setFont(font);
hiScoreText.setCharacterSize(55);
hiScoreText.setFillColor(Color::White);
hiScoreText.setPosition(1400, 0);
std::stringstream s;
s << "Hi Score:" << hiScore;
hiScoreText.setString(s.str());
Now we handle saving a potentially new high-score. Within the block that handles the player's health being less than or equal to zero, we create an ofstream
object called outputFile
, write the value of hiScore
to the text file, and then close the file:
// Have any zombies touched the player
for (int i = 0; i < numZombies; i++)
{
if (player.getPosition().intersects
(zombies[i].getPosition()) && zombies[i].isAlive())
{
if (player.hit(gameTimeTotal))
{
// More here later
}
if (player.getHealth() <= 0)
{
state = State::GAME_OVER;
std::ofstream outputFile("gamedata/scores.txt");
outputFile << hiScore;
outputFile.close();
}
}
}// End player touched
You can play the game and your high-score will be saved. Quit the game and notice that your high-score is still there if you play it again.
Let's make some noise.
18.217.7.174