Game Development Reference
In-Depth Information
8.3 The PaintCan Class
8.3.1 A Class with Multiple Instances
In this section, we will add paint cans to our game. We will base this section around
the Painter6 example, which adds a PaintCan class. Both the Cannon class and the Ball
class were associated with a single instance in the previous version of the Painter
game. The game only needs one ball, and one cannon. In the case of the paint cans,
we are going to need three instances. We will reuse these instances and reset them
once they fall out of the bottom of the screen, so we will not need more than three.
This also shows a big difference between a class and an object (instance). A class
is a blueprint for an object. This means that one class can have multiple instances.
In the PaintCan class, we define what a paint can is and what its behavior is. Then,
we can create multiple instances of this class. Inside the GameWorld class, we store
these instances in three different member variables:
PaintCan can1, can2, can3;
Another difference between the PaintCan class and the Ball and Cannon classes is that
each paint can has a target color . For example, the leftmost can has a red target
color. The goal of the game is to make each paint can falling down have its target
color before it falls out of the screen. Therefore, we need two member variables
of type Color : one for storing the target color, and one for storing the current color.
Furthermore, we are going to let the paint cans fall down with a certain velocity.
How we are going to set this velocity is something that we will deal with later. In
order to calculate that velocity, we want to know what the minimal velocity is that
a paint can should have, so that it does not fall down too slowly. For this, we add a
member variable minVelocity that contains this value. In total, the PaintCan class will
have the following member variables:
Texture2D colorRed, colorGreen, colorBlue;
Texture2D currentColor;
Vector2 position, velocity;
Color color, targetcolor;
float minVelocity;
In the constructor of the PaintCan class, we assign values to these member vari-
ables. The constructor takes three parameters: the content manager (for loading the
sprites), the desired position, and the target color of the paint can. Just like in the
Ball and Cannon classes, we use the content manager to load the three sprites in the
PaintCan constructor. Then, we assign values to the other member variables. Initially,
we set the y position of the paint can such that it is drawn just outside of the top
of the screen, so that later on in the game, we can see it fall down. The complete
Search WWH ::




Custom Search