XNA Random Background Colors

Tuesday, March 27 2007 - , ,

I was spending some time on the IRC tonight, for those who do not know a lot of the XNA Community hangs out on the IRC on Efnet in the #xna channel. It would be nice to get some more people on there who are in the Australian Time zone...

But back to it, one of the people on the channel (Care|bear) was asking about generating a random color for the background in and XNA Application, he also asked about controlling a sprite on that screen but that's another post.

Here are the steps that I quickly got together for him....

Start by setting up your basic application by using the normal base windows game, or Xbox360 game, and then add the following lines of code just under the code for the generation of the content and graphics device managers.

public Color backgroundColor;

 

static Random randomize = new Random();

 

public static Color GetRandomColor()

{

byte ColorR = (byte)randomize.Next(0, 255);

byte ColorG = (byte)randomize.Next(0, 255);

byte ColorB = (byte)randomize.Next(0, 255);

return new Color(ColorR,ColorG,ColorB);

}

 

The next step is to adjust the code in the draw override where the background is cleared each frame. To do this replace the first line of code in the Draw override so that it looks like this.

graphics.GraphicsDevice.Clear(GetRandomColor());

 

Now I know that there might be a better way to do this, but to me this is the quick way. With the code you could also change it so that the change in color is based on the update time, or even by pressing a button on the controller or keyboard. But that is up to you... to me this does give a good effect, sort of reminded me of the loading screens from the good old Commodore 64 games.

Hope this Helps...

kick it on DotNetKicks.com kick it on gamedevkicks.com

Similar Posts

  1. XNA Tutorial - Fading in a Splash Screen Logo
  2. Drawing a Basic Background in XNA
  3. Phoenix, Adding to the Background

1 comment(s)

Clicky Web Analytics