Skip to Main Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner content levelBeginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed
Beginning C# 7 Programming with Visual Studio 2017

Beginning C# 7 Programming with Visual Studio 2017

Benjamin Perkins, Jacob Vibe Hammer, Jon D. Reid

Publisher Resources

ISBN: 9780596154905Supplemental ContentErrata Page