Adding Some Game Logic

Good times. Now that you have a class that will create a moving enemy, you need to add some game logic. As with nearly any game, there will be an element of randomness in this game, so you'll need to add a random number generator. Remember that you always want to have only one random number generator that you use throughout your entire game. If you have multiple random number generator variables, there's a possibility that some of the variables might end up with the same random seeds, in which case, the number sequences will be the same (and will no longer be random). Add a Random object to your Game1 class and a public auto-property for that object:

public Random rnd { get; protected set; }

Then, initialize the rnd variable in the constructor of the Game1 class:

rnd = new Random(  );

While you're working in the Game1 class, go ahead and change the background color of the game to black to make it look more like we're in outer space (yay—outer space!!!). Remember that to change the background color you change the parameter sent to the Clear method in Game1's Draw method:

GraphicsDevice.Clear(Color.Black);

Also, you'll probably want to make your game play in full-screen mode and in a decent resolution. Let's go with 1,280 × 1,024 (you'll want to make sure that your resolution is a standard resolution supported by most PC monitors, and the 1,280 × 1,024 resolution definitely passes that test). To change the screen size and specify full-screen mode, add the following ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.