Game Development Reference
In-Depth Information
Try to avoid options as much as possible. For example, should a player
really set the difficulty? Why not adapt the difficulty automatically by moni-
toring the player's progress? And do you need a level selection screen? Why
not simply remember where the player was the last time and immediately
continue there? Keep your interface as simple as possible!
20.5 Reading and Storing Game Settings
Having the slider control the volume of the background music was not very compli-
cated. The MediaPlayer.Volume property is static, so we do not need an actual instance
to access it. But now suppose that we want the on/off button to control whether the
player can press a button to view a hint or not. Where should we store this option in-
formation? Normally, we would probably add a boolean member variable to a class
somewhere in our game that indicates if hints are shown or not. But where should
we add this variable? And how can we be sure that we can access it everywhere?
Another example would be a control for modifying sound effect volume. Sound ef-
fects will be used throughout the game in many different classes, so we should be
able to access the desired volume from almost anywhere. Clearly, what we need is a
generic way for dealing with such kinds of settings in a game, together with a design
that allows us to access this information everywhere in the code.
In order to allow for a variety of settings to be stored and retrieved, let us define
aclass GameSettingsManager , which contains a number of static member variables
and methods. For simplicity, let us assume that each setting consists of a key (the
identifier of the setting) and a value. For both the keys and the values, we will use
the string type. And the logical collection structure for storing such key-value pairs
is a dictionary. We add a member variable to the class to store this dictionary in:
protected Dictionary< string , string > stringSettings;
In the constructor of the GameSettingsManager class, we initialize the dictionary:
public GameSettingsManager()
{
stringSettings = new Dictionary< string , string >();
}
Just like we did in the Jewel Jam game for the random number generator and the
variable containing the screen dimensions, we will add a static member variable
pointing to the game settings manager, together with a static property for accessing
it:
Search WWH ::




Custom Search