New book: Microsoft XNA Game Studio 3.0: Learn Programming Now!

We’re happy to announce that Microsoft XNA Game Studio 3.0: Learn Programming Now! (Microsoft Press, 2009; ISBN: 9780735626584; 400 pages), by the witty and urbane Rob Miles, is now available. In this post we’d like to share some of the book’s content, including its Table on Contents, Introduction, and an excerpt from Chapter 2, “Programs, Data, and Pretty Colors.” As Rob says, “If you have always fancied writing software but have no idea how to start, then this book is for you.”

9780735626584f

You can grab the book’s full Table of Contents here. And here is its Introduction:

Introduction

With Microsoft XNA, Microsoft is doing something really special. They are providing an
accessible means for people to create programs for the Windows PC, Xbox 360 and Zune.
Now pretty much anyone can take their game ideas, run them on a genuine console, and
even send them to market in Xbox Live.

This book shows you how to make game programs and run them on an Xbox 360, a
Microsoft Windows PC, or a Zune device. It also gives you an insight into how software is
created and what being a programmer is really like.

Who This Book Is For

If you have always fancied writing software but have no idea how to start, then this book
is for you. If you have ever played a computer game and thought, “I wonder how they
do that?” or, better yet, “I want to make something like that,” then this book will get you
started with some very silly games that you and all your friends can have a go at playing and
modifying. Along the way, you also get a decent understanding of C#, which is a massively
popular programming language used by many thousands of software developers all over
the world. The C# skills that you pick up in this book can also be used as the basis of a career
in programming, should you find that you really enjoy writing programs. And because the
design of the C# language is very similar to C, C++ and Java you will find that your skills can
be used with them too.

The book is structured into 16 chapters, starting with the simplest possible XNA program
and moving on to show you how to use the Xbox and Zune gamepad, the keyboard, sounds,
graphics, and network in your games. In the course of learning how to use C# and XNA, you
create some very silly games, including Color Nerve, Mind Reader, Gamepad Racer, Bread
and Cheese, and Button Bash. You can even download the full versions of these games from
https://www.verysillygames.com and use them at your next party.

With this book, I show you that programming is a fun, creative activity that lets you bring
your ideas to life.

System Requirements

You need the following hardware and software to build and run the code samples for this
book. Chapter 1, “Computers, C#, XNA, and You,” explains how to set up your environment.

  • A Windows PC with 3-D graphics acceleration if you want to run your XNA games on
    your PC.
  • Microsoft Windows XP SP2 or Windows Vista.
  • Microsoft Visual Studio 2008 C# Express edition, Visual Studio 2008 Standard edition,
    Visual Studio 2008 Professional edition, or Visual Studio 2008 Team Suite.
  • XNA Game Studio 3.0.
  • To test your games on a console, you need an Xbox 360 fitted with a hard disk. Your
    Xbox 360 must be connected to Xbox Live, and you need to join the XNA Creators
    Club. You will find out how to do this in Chapter 1.
  • If you have a Zune media player, you can run XNA games on that as well. Any Zune
    device, from the original 30-gigabyte (GB) device to the latest 4 GB device, can be
    connected to your PC you can load your XNA games into it.

Code Samples

All the code samples discussed in this book can be downloaded from the book’s companion
content page at https://www.microsoft.com/learning/en/us/books/13411.aspx.

There are also code samples and games at https://www.verysillygames.com.

 

Here’s the Chapter 2 excerpt:

Chapter 2

Programs, Data, and Pretty Colors

In this chapter, you will

  • Explore how games actually work.
  • See how data is stored in a program.
  • Discover how colors are managed on computers.
  • Find out about classes and methods.
  • Write some code that controls color.
  • Write some code that makes decisions.
  • Create a funky color-changing mood light.

Introduction

You now know how to create a Microsoft XNA program and run it. Your program only turns
the screen blue, but you could call it a start. Next, you are going to figure out how game
programs are constructed. Then you’ll play with colors and find out how XNA stores color
information and how C# stores data.

Program Project: A Mood Light
Your first project is going to be a program that turns a display (the bigger the
better) into a mood light. These are the things that they have on spaceships, where a
chandelier actually would not work very well. Instead, the spaceship will have a panel
on the wall that can be set to glow in different colors and brightness levels or perhaps
even change color over time. This is probably not a very efficient way of lighting a
building—you are using one of the most powerful game consoles ever made to replace
a lamp—but it is a fun exercise and may even lead to a game idea or two along the
way. You can use the same program to convert your Zune into a multicolored flashlight.

Before going any farther, you need to consider what a game program does. Computer
programs in general read data, do something with it, and then send it out. This is true
whether the computer is working out company wages or timing the ignition spark in a car
engine. Figure 2-1 shows how this works with respect to game programs. The gamepad
provides the input data to the game, and the display screen shows the output.

image

Later versions of games might have other inputs and outputs, too; for example, if you
are playing on Xbox Live, your console is receiving information about other players in
your networked game. For now, start by considering only the output from your game. In
Chapter 3, “Getting Player Input,” you’ll take a look at where the input values come from.

Making a Game Program

To see how a game program can produce a display, you need to look inside one of the C#
programs that XNA built. At the end of Chapter 1, “Computers, C#, XNA, and You,” 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 2-2, 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);
}

image

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.

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

The Great Programmer Speaks: Comments Are Cool 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.

Sample Code: Red Screen of Anger All the sample projects can be obtained from the Web
resources for this text, which can be found at https://microsoft.learning.en/us/Books/13411.aspx.
The sample project in the directory “01 MoodLight Red Screen” in the resources for this chapter
draws a red screen for you. You could run this when you felt particularly angry. You can change
the color that you want to display by changing the colors used in the Draw method; there are
some comments in the code to help you with this.

You can set the background color to a range of preset ones, but you can also design colors of
your own, which brings us to our first project.

This is a great book for beginners; enjoy!