Game Development Reference
In-Depth Information
public void
Update(GameTime gameTime)
{
∗
position += velocity
(
float
)gameTime.ElapsedGameTime.TotalSeconds;
}
Finally, we can add a few convenient properties for getting and setting the color,
position or velocity. For the complete
ThreeColorGameObject
class, see the example
project
Painter9
belonging to this chapter.
10.4 Creating Special Versions of Game Objects
Now that we have created a very basic game object class, we can reuse this basic
behavior for the actual game objects in our game by
inheriting
from this class, just
like we inherited from the generic
Game
class.
10.4.1 The
Cannon
Class
Let us first have a look at the cannon game object. Since we now have the basic
ThreeColorGameObject
class, we can create the
Cannon
class, and inherit from the
ThreeColorGameObject
class:
class
Cannon : ThreeColorGameObject
{
...
}
Now we need to add a constructor method to this class. Before we start writing the
code for this constructor, let us first think about what it means when we create an
instance of
Cannon
. In the previous version of the
Painter
game, the
Cannon
instance
can be depicted as in Fig.
10.1
.
In the new version of the
Cannon
class, it is no longer a class that stands on its
own, but it inherits from the
ThreeColorGameObject
class. This means that
Cannon
is basically a special version of a
ThreeColorGameObject
instance. From the point of
view of the compiler, this means that when a
Cannon
instance is created, this instance
consists of a
ThreeColorGameObject
instance and the extensions that make it a
Cannon
object. Next to the member variables that are already in the
ThreeColorGameObject
class, a
Cannon
object also has a barrel that is drawn at a certain angle. As a result,
we need two additional member variables:
Texture2D cannonBarrel;
float
angle;