You
are now going to start from scratch with your first program. It won’t
actually do much—you won’t be writing your own version of Halo just
yet—but it does give you an insight into what XNA does and how you can
write your own C# bits to produce a cool mood light.
1. Creating Your First Project
A computer game is not just
a program—it is also lots of other bits and pieces that make playing
the game fun and interesting. Just about every game has graphics,
sounds, 3-D models of game objects, and all sorts of other items that
must be created along with the code. This means that when you make a
game, you have to manage all these other resources, too. The good news
is that the XNA designers have thought of this, and they provide a
comprehensive content management
solution that looks after all these resources. You can just give your
game resources to the Content Manager, and it makes sure that they are
available to the programs that you write. Later on in the book, I’ll
show you how to add some content of your own so that you can use a
picture of your mom or your dog as a game character.
The content management is part of the project
mechanism provided by XNA Game Studio. What this means right now is
that to create your first game program, you actually have to create an
XNA Game Studio project.
Note:
This menu is created
when XNA Game Studio is installed and contains a link to the version of
Visual Studio 2008 installed on your Windows PC. If you are using a
different version of Visual Studio 2008 (perhaps you installed XNA Game
Studio on top your installation of Visual Studio 2008 Professional
Edition) you will see a link to your version.
In XNA Game Studio, select New Project from the File menu, as shown in Figure 1. This automatically creates the entire project and the file into which you are going to put your code.
XNA Game Studio can make a
whole range of different projects depending on what you actually want
to build. The skeletons for each of these types of program are contained
in project templates and Starter Kits. You can download and install
other Starter Kits from the Creators Club Web site. Right now, you are
going to use a template to create an empty XNA project.
Figure 2
shows all the possible types of projects that can be created. You need
to select the project that matches the device on which you want to run
the game.
Note:
Make sure that you create a "Game" and NOT a "Game Library"; otherwise, you will not be able to make your program run.
You
should just choose the one that you want to use; the way that the
program works is identical for all. Call the project "MoodLight" because
that is what we are building first. You can use the Browse button to
select an appropriate destination for the project. You should ensure
that the Create Directory For Solution check box is selected so that all
the files for this game are held in one place. Once you have done this,
click OK to get XNA Game Studio to build the project for you.
When the project has been created, you should see a screen that looks like the one in Figure 1-5.
Yours might not look quite the same (it certainly won’t have the big
arrow pointing at the Start Debugging button), but it should look
similar. There are a lot of controls that you can play with. At the
moment quite a few are disabled and can’t be used, but it still looks
confusing the first time that you see it. The key here is not to panic.
You are going to use only a few of the buttons to start with, and I’ll
explain the other ones as you need them.
2. Running Your First Program
If you are running your program on a PC, you can just click the Start Debugging button (indicated by the arrow in Figure 3),
and the program runs. If you are sending your program to an Xbox 360,
you must make sure that the XNA Game Studio Connect application is
running on your Xbox 360 and that the Xbox has been connected to your
Windows PC. If you are sending your program to a Zune, make sure that it
is connected to your computer and that the Zune application is not
running.
When you click the button to run the program, a number of things happen in quick succession:
XNA Game Studio compiles the source code
files. The source code of the program is all the lines of C# code that
you and XNA Game Studio create that actually describe what you want the
computer to do. A compiler is a program that takes source code and
creates a set of machine instructions that can be loaded into the
computer’s processor to control what the computer does. The C# language
has a particular specification, and the compiler knows all about the
rules in the specification. The compiler rejects any program that it
thinks is not correct and tells you about the compilation errors.
You are going to have to live with the fact that you will see a lot of
these errors if you decide to become a computer programmer.
Your
project might contain a large number of different source files; each of
them must be compiled. If all the program source files compile
correctly, they are then combined with any resources (for example,
images and sounds) that are part of the project.
If you are using an external device, either Xbox 360 or Zune, the compiled files are now transferred into it.
Finally,
XNA Game Studio starts the program running. If you are using a Windows
PC, the program runs in a window on the desktop. If you are using an
external device, the program takes it over completely. At this point,
the window or target device is under the control of your program
statements.
When XNA Game Studio
produces an empty project, it actually creates a program that will
compile and run, so you can just click the Start Debugging button (if
you haven’t already) and turn the program loose.
When you run the
program, the screen turns blue. That’s it—nothing else. All that work to
turn the screen blue? You could have done that with a can of paint in
30 seconds. The "empty" project from XNA just turns the screen blue. You’re
going to make a light that can display millions of possible colors, an
ever-changing mood lamp, and finally, the world’s first-ever
color-changing game.
Note:
One slightly irritating
thing about Visual Studio is that when the program is running, the
organization of the controls in Visual Studio changes. This can confuse a
first-time user because menus, toobars, and panes suddenly don’t seem
to be where they used to be. If you carefully compare Figure 1-5 and Figure 1-6, you notice that a new toolbar has appeared that has buttons on it that you can use to pause or stop the program.