Game Development Reference
In-Depth Information
It is your job as a programmer to turn the design created by a game designer into a working
implementation. If you were using the OOP paradigm, you would take this design and break it down
into distinct objects in the program. We do this by looking for the nouns that exist in the design,
as they are ideal candidates to be turned into classes. Classes form a blueprint from which you can
create the individual objects in your program. The following nouns are present in the design and
could be considered suitable for being turned into classes:
Game
User
Player
Name
Game loop
At this point you need to consider which of these really are suitable candidates for being turned
into classes. Game seems like an obvious choice for a class, as it can be the object that represents
the entire game. User might not be a good candidate for a class in our program. The user is the
person interacting with our program and does not need to be represented in the program itself;
however, a Player class would be useful to represent the current state of the players in our game.
Name appears to be data related to the player. You've seen so far that we can represent strings
in the game using the C++-supplied string class, which means we don't have to create our own
class for the player name. The game loop from Listing 7-7 was created using a namespace and a
set of functions. In this chapter we look at how we can represent this using a class rather than a
namespace to group the functionality.
This section has given you a brief introduction to the OOP design process. The rest of this chapter
looks at how we can build programs out of classes in C++.
Encapsulation
One of the main questions that comes up with programmers who are new to OOP is “Why do we
even use classes?” There are many answers to this question, making it a complicated topic to
explain. We cover more of the advanced features provided by classes in C++ as we work through
this chapter. The first major benefit for the new programmer is the concept of encapsulation. Classes
in C++ allow us to group together data in the form of variables and functions, known as methods,
that operate on this data. The classes allow us to mark some of these variables and methods as
being hidden from the rest of the program.
This concept of data hiding is known as encapsulation. Encapsulation is a desirable feature of OOP
because it allows us to operate loosely on the basis of contracts in our programs. The classes we
write will have a public interface that other classes can expect to behave in a certain manner. This
interface will remain consistent to allow us to create easily maintainable programs; however, the
private data and methods can be changed as often as we need them to. This is another concept
that can be difficult to understand, so we will work through a class listing to see how this works in
practice. Listing 8-1 shows the code for our first class, Player .
 
Search WWH ::




Custom Search