Game Development Reference
In-Depth Information
the Painter class was a special version of the Game class. This is in fact an example
of inheritance. The Game class contains a lot of methods and properties (such as
Content ), and because we inherit from this class, we can use them in the Painter class
as well, since Painter is basically the same as Game , except that it is a special ver-
sion of it. You could also say that Painter is a subclass of Game , or that Game is the
superclass ,orthe base class of Painter . The inheritance relationship between classes
is widely used and in a good class design, it can be interpreted as 'is a kind of'. For
example, Painter is a kind of Game . You can also say that Painter is a subclass ,ora
derived class of Game .
10.3.1 Game Objects and Inheritance
The 'is a kind of' relationship also holds for the game objects in the Painter game.
A ball is a kind of game object, and so are the paint cans and the cannon. We can
make this inheritance relationship explicit in our program by defining a generic class
called ThreeColorGameObject , and then have our actual game object classes inherit
from that generic class. We can then put everything that defines what a three color
game object is inside that class, and the ball, the cannon, and the paint can will be
special versions of that class.
10.3.2 The ThreeColorGameObject Class
So let us now have a look at this ThreeColorGameObject class in more detail. We are
going to put all the member variables that are used by all the different types of game
objects in our game, into this class. So, we can define a basic skeleton of our class
as follows:
class ThreeColorGameObject
{
Texture2D colorRed, colorGreen, colorBlue;
Texture2D currentColor;
Vector2 position, velocity;
Color color;
...
}
As a first step, let us define the constructor method of this class. Each game ob-
ject that inherits from this class will load different sprites for representing the three
different colors. Therefore, we add three parameters to the ThreeColorGameObject
constructor: one for each of the sprites. Inside the constructor, we then assign these
parameter values to the member variables:
Search WWH ::




Custom Search