Game Development Reference
In-Depth Information
Chapter 10
Organizing Game Objects
10.1 Introduction
We have seen in the previous chapter how we can use classes to group variables
that belong together. In this chapter, we are going to look at the similarities be-
tween the different types of game objects, and how we can express these similarities
in C#.
10.2 Similarities Between Game Objects
If we look at the different game objects in our Painter game, we can see that they
have a lot of things in common. For example, the ball, the cannon and the paint
cans all have three sprites that represent each of the three different colors. Also,
most objects in the game have a position and a velocity. Furthermore, all game
objects need a method to draw them, some of the game objects have a method for
handling input, some of them have an Update method, and so on. Now, it is not really
a problem that these classes have similarities. The compiler will not complain about
that. However, it is a pity that we have to copy code all the time. To give an example,
both Ball and the PaintCan ) class have the following method:
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(currentColor, position, Color.White);
}
The code is exactly the same, but it is copied in both the classes. And every time
we want to add a different kind of three colored game object, we need to copy this
method again. In this case, the method is fortunately not that complicated, but in our
application we are copying around a lot of other things as well. For example, all the
game objects in the Painter game share the following member variables:
 
Search WWH ::




Custom Search