XNA 101 .Net

Learn how to program in C# while writing games in XNA Game Studio 3.0!
Our upcoming Silverlight book for beginners (includes a great chapter on game development in Silverlight!) Hello! Silverlight 2 with Dave Campbell, available online now!



Pages

Recent posts

Navigation

Archive

Blogroll

    Tampa Divorce Lawyer

    North of Tampa in Lutz, Florida. A Tampa Divorce Lawyer focusing on family, divorce, and real estate law.

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    Lesson 10: Sprite Color

    Source code for this lesson completed: http://www.bluerosegames.com/xna101/lessons/XnaLesson10.zip

    So far, when drawing the sprites we've been using Color.White as the color to draw with. Color.White will preserve the original color of the sprite, but we can get some interesting effects by using other colors.

    In order to allow each BouncingBall sprite to have its own drawing color, let's add a DrawingColor Property to the BouncingBall class and an associated private field _drawingColor to store the color. The code will look something like this:

    private Color _drawingColor; 
     
    public Color DrawingColor
    {
       get { return _drawingColor; }
       set { _drawingColor = value; }
    } 
     

    In the BouncingBall constructor, add the following line to initialize the value in _drawingColor:

    _drawingColor = Color.White;

    So if we don't set the drawing color to something else, it will default to white.

    Now change the BouncingBall.Draw() method to use this color instead of Color.White. The method will now look like this:

    public void Draw(SpriteBatch spriteBatch)
    {
       spriteBatch.Draw(Texture, _position, _drawingColor);
    } 

    So again the Game1 class is simplified and more logic is put in the BouncingBall class. Now imagine that we have another sprite class that does something completely different than the BouncingBall class. We could still have the same Draw() and Update() methods, and call them exactly the same way from the Game1 class, and do two totally different things.

    So now if you run the program again, it does the same thing as always. Now for something completely different. Add the following image to your project by saving it to your computer and then right click on the Content folder in the solution and select "Add Existing Item" from the menu. It should be called white_ball.png.

    In the Game1.LoadContent() method,  change red_ball to white_ball. Then after the ball2.Velocity = new Vector2(10f, 5f);
    statement, add the following:

    ball2.DrawingColor = Color.Purple;

    Now if you run the program again, you should have a white ball and a purple ball.

    purple

    Colors can also be specified as a combination of Red, Green and Blue. Pretty much any color can be represented by the correct combination of red, green, and blue. So you can change the above line to:

    ball2.DrawingColor = new Color(255, 0, 255);

    Where the first argument is the amount of red the color will have, with 255 being the maximum and 0 being the minimum. The second argument is the amount of green for the color, and the last is the amount of blue. So this color will be full red, no green, and full blue. When referring to colors in computer programming, especially in web design, we often refer to the RGB value of a color (short for Red, Green Blue). Run the program again. The ball should now be a bright purple.

    Posted: Oct 18 2008, 12:45 by Bill Reiss | Comments (1) RSS comment feed |
    • Currently 5/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Filed under:

    Comments

    Add comment


     

      Country flag

    biuquote
    • Comment
    • Preview
    Loading