Game Development Reference
In-Depth Information
The Color type consists of three 'sub' variables: one for each color component. 1
In the image above, we write the type above the block containing the data so that it
is clear we are dealing with a Color type. Other examples of colors are:
Color blueColor, greenColor, redColor, grayColor, whiteColor;
blueColor = new Color(0, 0, 255);
greenColor = new Color(0, 255, 0);
redColor = new Color(255, 0, 0);
grayColor = new Color(200, 200, 200);
whiteColor = new Color(255, 255, 255);
As you can see in these examples, the color intensity values range between 0 and
255, where 255 is the highest color intensity. By setting the 'red' value to 255 and
the other two values to 0, we create the color red. We get the color white by setting
all intensity values to 255. Similarly, the black color is created by setting all values
to 0. Using these RGB values, it is possible to create a wide range of colors.
In our example, we want to use the passed game time to vary the color. We
already stored the milliseconds value of the current game time in an integer variable:
int red = gameTime.TotalGameTime.Milliseconds;
Now, we use this value to create a new color:
background = new Color(red, 0, 0);
In the DiscoWorld example, we have done this work in the Update method, because
basically the background color can be regarded as a very simple game world , and up-
dating the background color, hence, the game world, needs to be done in the Update
method. However, you can see that we placed the declaration of the background vari-
able outside of the method, at the top in the class body (line 8). Why is this?
1 In fact, the Color type consists of four elements: the R, G, and B values, as well as an A (alpha)
value that stores the transparency. For now, we are only using the first three elements.
Search WWH ::




Custom Search