Book Image

XNA 4.0 Game Development by Example: Beginner's Guide

By : Kurt Jaegers
Book Image

XNA 4.0 Game Development by Example: Beginner's Guide

By: Kurt Jaegers

Overview of this book

XNA Game Studio enables hobbyists and independent game developers to easily create video games. It gives you the power to bring your creations to life on Windows, the Xbox 360, the Zune, and the Windows Phone platforms. But before you give life to your creativity with XNA, you need to gain a solid understanding of some game development concepts.This book covers both the concepts and the implementations necessary to get you started on bringing your own creations to life with XNA. It details the creation of four games, all in different styles, from start to finish using the Microsoft XNA Framework, including a puzzler, space shooter, multi-axis shoot-'em-up, and a jump-and-run platform game. Each game introduces new concepts and techniques to build a solid foundation for your own ideas and creativity. Beginning with the basics of drawing images to the screen, the book then incrementally introduces sprite animation, particles, sound effects, tile-based maps, and path finding. It then explores combining XNA with Windows Forms to build an interactive map editor, and builds a platform-style game using the editor-generated maps. Finally, the book covers the considerations necessary for deploying your games to the Xbox 360 platform.By the end of the book, you will have a solid foundation of game development concepts and techniques as well as working sample games to extend and innovate upon. You will have the knowledge necessary to create games that you can complete without an army of fellow game developers at your back.
Table of Contents (15 chapters)
XNA 4.0 Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
Preface
4
Asteroid Belt Assault – Lost in Space
Index

Time for action – adding variables to the class declaration area


  1. Right below the SpriteBatch spriteBatch; line, add the following:

    Random rand = new Random();
    Texture2D squareTexture;
    Rectangle currentSquare;
    int playerScore = 0;
    float timeRemaining = 0.0f;
    const float TimePerSquare = 0.75f;
    Color[] colors = new Color[3] { Color.Red, Color.Green, Color.Blue };

What just happened?

These are all the variables you will need for the SquareChase mini game. Here is a quick breakdown:

rand : This instance of the Random class is used to generate random numbers via the Next() method. You will use this to generate random coordinates for the squares that will be drawn to the screen.

squareTexture : The Texture2D class holds a two dimensional image. We will define a small texture in memory to use when drawing the square.

currentSquare : The XNA Framework defines a structure called Rectangle that can be used to represent an area of the display by storing the x and y position of the upper left corner along with a width and height. SquareChase will generate random squares and store the location in this variable.

playerScore : Players will score one point each time they successfully "catch" a square by clicking on it with their mouse. Their score accumulates in this integer variable.

timeRemaining: When a new square is generated, this float will be set to a value representing how many seconds it will remain active. When the counter reaches zero, the square will be removed and a new square generated.

TimePerSquare : This constant is used to set the length of time that a square will be displayed before it "runs away" from the player.

colors: This array of Color objects will be used when a square is drawn to cycle through the three colors in the array. The Color structure identifies a color by four components: Red, Green, Blue, and Alpha. Each of these components can be specified as a byte from 0 to 255 representing the intensity of that component in the color. The Alpha component determines the transparency of the color, with a value of 0 indicating that the color is fully transparent and 255 indicating a fully opaque color. Alternatively, each component of a color can be specified as a float between 0.0f (fully transparent) and 1.0f (fully opaque).

The Game1 class constructor

The XNA templates define an instance of the Microsoft.Xna.Framework.Game class with the default name "Game1" as the primary component of your new game. Slightly more goes on behind the scenes, as we will see when we add an XNA game to a Windows Form in Chapter 8, but for now, we can consider the Game1 constructor as the first thing that happens when our XNA game is executed. The class constructor is identified as public Game1(), and by default, it contains only two lines:

graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

For most of the games in this book, we will not need to make extensive modifications to the Game1 constructor, as its only job is to establish a link to the GraphicsDeviceManager object and set the default directory for the Content object which is used to load images, sound, and other game content.

The Initialize() method

After the constructor has finished and your XNA game begins to run, the Initialize() method is called. This method only runs once, and the default code created with a new project template simply calls the base version of the method. The Initialize() method is the ideal place to set up things like the screen resolution, toggle full screen mode, and enable the mouse in a Windows project. Other game objects that do not rely on external content such as graphics and sound resources can also be initialized here.