To see how a game program can produce a display, you need to look inside one of the C# programs that XNA built. You used XNA Game Studio to create a game program. Now you are going to look at this program and discover how it works.
The file that contains
the game behavior is called Game1.cs. The name Game1 was generated
automatically when the project was created; the .cs part is the file extension
for C# programs. If you want to look inside this file, start XNA Game
Studio and open the file from Solution Explorer. You can find Solution
Explorer, shown in Figure 1,
in the top right corner of the XNA Game Studio screen. If you
double-click the name of the file that you want to work with, the file
opens in the editing window.
If you look at the
content of Game1.cs, which drew that impressive blue screen, you can see
how the program works. The program code that XNA Game Studio created
when you made an empty game contains the following method:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
base.Draw(gameTime);
}
A method is a named part of a program. In this case, the method has the name Draw (you can ignore the protected override void
part for now). All you need to know at the moment is that when XNA
wants to draw the screen, it uses this method. You can change what gets
drawn by altering the content of this method. At the moment, we just get
a blue screen; if you look at the second line of the preceding code,
you can see where the blue screen comes from.
1. Statements in the Draw Method
The Draw method contains a block of
statements. C# programs are expressed as a series of statements that
are separated by a semicolon (;). Each statement
describes a single action that your program needs to do. There are a
number of different kinds of statements; you discover new ones as you
learn more about programming. The statements are organized into a single
block. A block is a
way to lump statements together. The start of a block is marked with an
open curly bracket character ({ )and the end of the block is marked
with a closing curly bracket (}). These curly kinds of brackets are
sometimes called braces.
The C# compiler, which is trying to convert the program text into
something that can actually run, notices and complains if you use the
wrong kind of bracket.
In the preceding code, there is also a comment.
Comments are ignored by the compiler; they let you put text into your
program to describe the program or to remind you to do things. In the
preceding code, the comment is a "TODO,"
which tells programmers that they need to do something. In this case, a
programmer must add drawing statements at that position in the program
file. The compiler can tell that the text is a comment because it starts
with the character sequence //. For instance, look at the following
example:
// This is a comment. It can be any text.
You can add comments anywhere in your program.
Note:
Our
Great Programmer likes comments. She says that a well-written program
is like a story in the way that the purpose of each part is described.
She says that she will be looking at our code and making sure that we
put the right kind of comments in.
From the point of view of changing the color of your screen, the statement that is most interesting is this one:
GraphicsDevice.Clear(Color.CornflowerBlue);
Clear is a method
that is part of XNA. You will see precisely how it fits into the
framework later; for now, all you need to know is that the Clear
method is given something that describes a color, and the method clears
the screen to that color. At the moment, you are sending the Clear method the color CornflowerBlue, and it is clearing the screen to be that color. If you want a different color, you just have to send a different value into Clear:
GraphicsDevice.Clear(Color.Red);
If you change the color as shown in the preceding line and run the program, you should see that the screen is now set to red.