Saving and loading the high score
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 this complexity for us. Like concatenating strings for our HUD, it is the C++ Standard Library that provides the necessary functionality through fstream
.
First, we include fstream
in the same way we included sstream
:
#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 to the 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 can easily open the file and add a score to it. If you do, make sure it is quite a low score...