Game Development Reference
In-Depth Information
Texture2D colorRed, colorGreen, colorBlue;
Texture2D currentColor;
Vector2 position;
Color color;
Also, both the paint can and the ball have a Center property:
public Vector2 Center
{
get { return new Vector2(currentColor.Width, currentColor.Height) / 2; }
}
Again, the code is exactly the same in the different classes, and we copy it every
time we make a new kind of game object. And the same goes for the Position or
the Color property. In general, it is better to avoid copying around a lot of code.
Why is that? Because if at some point you realize there is a mistake in that part
of the code, you have to correct it everywhere you copied it to. In a small game
like Painter , this is not a big issue. But when you develop a commercial game
with hundreds of different game object classes, this becomes a serious mainte-
nance problem. In order to solve this problem, we need to think about how the
different kinds of game objects are similar, and if we can group these similarities
together, just like we grouped the member variables together in the previous chap-
ters.
Conceptually speaking, it is easy to say what is similar between balls, paint cans
and cannons: they are all game objects . Basically they can all be drawn at a certain
position, they all have a velocity (even the cannon, but its velocity is zero), and they
all have a color that is either red, green or blue. Furthermore, most of them handle
input of some kind and are updated.
10.3 Inheritance
With object-oriented programming, it is possible to group these similarities together
in a generic class, and then define other classes that are a special version of this
generic class. In object-oriented jargon, this is called inheritance and it is a very
powerful language feature. We have already used this feature before, but we did
not know yet that it was called inheritance. Our Painter game class is defined as
follows:
class Painter : Game
{
...
}
When you look at the class header, you see the name of the class ( Painter ), but also
the class name Game . We did not really explain what this meant before, except that
Search WWH ::




Custom Search