Colors

[ LiB ]

Before I let this chapter end, I want to teach you how to work with color. Of course, color is an integral part of any program. When using page flipping (which is explained in the next chapter), color takes on an even greater importance.

You need to know some functions before you move on to the next chapter. These functions are Color , Cls , and ClsColor . You also need to understand RGB values.

RGB

When working with color, you will often encounter RGB (red, green, blue) values. These numbers give you the ability to pick any one of 16 million different colors. That's a lot, huh?

NOTE

Why 16 Million?

When you are using RGB values, you usually pick a number between 0 and 255 for each color. What does this have to do with the number of colors? Well, if you multiply 256 by itself three times (256 x 256 x 256), you get 16.7 million. This means that you have all 16.7 million values to choose from.

When used in functions, there are usually three fields for you to enter your choicesred, green, and blue. For each field, you can pick a number between 0 and 255 (256 choices total). The higher the number, the more of that color there will be. For example, if you set the red value to 255 and the green and blue values to zero (255,0,0), you will have a perfectly red color. (0,0,0) is black, and (255,255,255) is white.

Now, you may be wondering how you are supposed to find the exact values for the color you want. Well, there are two ways. You can use guess and check (by putting in guesses for the red, green, and blue fields) or you can use a program, such as Microsoft Paint.

Open Microsoft Paint by going to Start Menu->Programs->Accessories->Paint. See Figure 5.16 for a visual image of Microsoft Paint and how to open it (the background is Paint, the foreground is the start menu [your menu will probably be a little different]). Now hit Colors->Edit Colors. A window will pop up. Click where it says Define Custom Colors >>. Figure 5.17 shows you the custom colors box.

Figure 5.16. Opening Microsoft Paint.


Figure 5.17. Defining custom colors.


Now choose your color, and it should tell you the RGB value on the bottom. If it doesn't work at first, move the scrollbar on the far right first, and then proceed to pick your color.

That's pretty much all there is to RGB. Let's actually use some color in our programs now.

Color

Color is kind of a fun function. It defines what the default color of the program is. When you draw something, be it lines, shapes , or text (not images), it will be drawn with the defined color.

NOTE

The default color of any Blitz Basic program (before you call Color ) is white (RGB 255,255,255).

What can you do with Color ? If you want to make the text anything other than white, just use this. Or maybe you want to draw a green triangle. Just set the color to green, and draw it! You can change the color at any time.

Let's start off with the function declaration.

 Color red, green, blue 

See Table 5.10 for the parameters. You will most likely just put in the red, green, and blue values to get your color.

Table 5.10. Color's Parameters

Name

Description

red

The color's red value

green

The color's green value

blue

The color's blue value


Now let's write a program that uses this. This program will draw a bunch of ellipses with random sizes and colors.

 ;;;;;;;;;;;;;;;;;;;;;; ;demo05-06.bb ;By Maneesh Sethi ;Demonstrates the Color function, draws ellipses ;No Input Parameters required ;;;;;;;;;;;;;;;;;;;;;; Graphics 800,600    ;Seed random generator SeedRnd (MilliSecs())   ;Max width of ellipse Const MAXWIDTH = 200 ;Max Height of ellipse Const MAXHEIGHT = 200   ;Main Loop While Not KeyDown(1)   ;Set the color to a random value Color Rand(0,255), Rand(0,255), Rand(0,255)   ;Draw a random oval Oval Rand(0,800),Rand(0,600),Rand(0,MAXWIDTH),Rand(0,MAXHEIGHT), Rand(0,1) ;Slow down! Delay 50 Wend 

Pretty cool, huh? Figure 5.18 is a screenshot from the program. Let's look a little closer. The program first sets the graphics mode and seeds the random generator. Then it defines the maximum width and height of each ellipse. Feel free to change the values.

Figure 5.18. The demo05-06.bb program.


Next, the game enters the main loop. It first sets the color to a random value, using the line

 Color Rand(0,255), Rand(0,255), Rand(0,255) 

This allows the next line to draw an ellipse with the random color. The ellipse function (notice that it is actually called Oval I just like the word ellipse) is defined like this:

 Oval x,y,width,height[,solid] 

Take a look at Table 5.11 for each parameter.

Table 5.11. Oval's Parameters

Parameter

Description

x

The x coordinate of the ellipse.

y

The y coordinate of the ellipse.

width

The width in pixels of the ellipse.

height

The height in pixels of the ellipse.

[solid]

Default value is 0; set to 1 if you prefer the ellipse to be filled. Otherwise, the inner region will be transparent.


Well, that's pretty much it for the color function. Next upthe Cls and the ClsColor functions.

Cls and ClsColor

We are almost done with this chapter! Before I send you packing, though, I want you to have a bit of basis for the next chapter.

The function Cls 's action is pretty simple. All it does is clear the screen. The next chapter goes over it in more depth. The ClsColor function works with Cls to allow you to change the background of your program.

 ClsColor is defined like this: ClsColor red,green,blue 

See Table 5.12 for a description of each parameter.

Table 5.12. ClsColor's Parameters

Name

Description

red

The color's red value

green

The color's green value

blue

The color's blue value


ClsColor's job is to change the background color. This means that you can leave the default black behind and make the background anything you want it to be. To use this function, call ClsColor with the red, green, and blue values you want, and then call Cls to actually clear the screen with the background color.

Let's try a program. Demo05-07.bb makes a bunch of colors appear on the screen (along with some advice you should follow). Try it out!

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net