Books & ebooks about Microsoft tools, technologies, & research. Plus programming best practices. We hope you enjoy this post.
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.”
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 anaccessible 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, andeven send them to market in Xbox Live.
This book shows you how to make game programs and run them on an Xbox 360, aMicrosoft Windows PC, or a Zune device. It also gives you an insight into how software iscreated 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 bookis for you. If you have ever played a computer game and thought, “I wonder how theydo that?” or, better yet, “I want to make something like that,” then this book will get youstarted with some very silly games that you and all your friends can have a go at playing andmodifying. Along the way, you also get a decent understanding of C#, which is a massivelypopular programming language used by many thousands of software developers all overthe world. The C# skills that you pick up in this book can also be used as the basis of a careerin programming, should you find that you really enjoy writing programs. And because thedesign of the C# language is very similar to C, C++ and Java you will find that your skills canbe used with them too.
The book is structured into 16 chapters, starting with the simplest possible XNA programand 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, youcreate some very silly games, including Color Nerve, Mind Reader, Gamepad Racer, Breadand Cheese, and Button Bash. You can even download the full versions of these games fromhttp://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 bringyour ideas to life.
System Requirements
You need the following hardware and software to build and run the code samples for thisbook. Chapter 1, “Computers, C#, XNA, and You,” explains how to set up your environment.
Code Samples
All the code samples discussed in this book can be downloaded from the book’s companioncontent page at http://www.microsoft.com/learning/en/us/books/13411.aspx.
There are also code samples and games at http://www.verysillygames.com.
Here’s the Chapter 2 excerpt:
Chapter 2
Programs, Data, and Pretty Colors
In this chapter, you will
You now know how to create a Microsoft XNA program and run it. Your program only turnsthe screen blue, but you could call it a start. Next, you are going to figure out how gameprograms are constructed. Then you’ll play with colors and find out how XNA stores colorinformation and how C# stores data.
Program Project: A Mood LightYour first project is going to be a program that turns a display (the bigger thebetter) into a mood light. These are the things that they have on spaceships, where achandelier actually would not work very well. Instead, the spaceship will have a panelon the wall that can be set to glow in different colors and brightness levels or perhapseven change color over time. This is probably not a very efficient way of lighting abuilding—you are using one of the most powerful game consoles ever made to replacea lamp—but it is a fun exercise and may even lead to a game idea or two along theway. 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. Computerprograms in general read data, do something with it, and then send it out. This is truewhether the computer is working out company wages or timing the ignition spark in a carengine. Figure 2-1 shows how this works with respect to game programs. The gamepadprovides the input data to the game, and the display screen shows the output.
Later versions of games might have other inputs and outputs, too; for example, if youare playing on Xbox Live, your console is receiving information about other players inyour networked game. For now, start by considering only the output from your game. InChapter 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 usedXNA Game Studio to create a game program. Now you are going to look at this program anddiscover how it works.
The file that contains the game behavior is called Game1.cs. The name Game1 was generatedautomatically 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 SolutionExplorer. You can find Solution Explorer, shown in Figure 2-2, in the top right corner of theXNA 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 seehow the program works. The program code that XNA Game Studio created when you madean empty game contains the following method:
protected override void Draw(GameTime gameTime){GraphicsDevice.Clear(Color.CornflowerBlue);// TODO: Add your drawing code herebase.Draw(gameTime);}
A method is a named part of a program. In this case, the method has the name Draw (youcan ignore the protected override void part for now). All you need to know at themoment is that when XNA wants to draw the screen, it uses this method. You can changewhat gets drawn by altering the content of this method. At the moment, we just get a bluescreen; if you look at the second line of the preceding code, you can see where the bluescreen comes from.
Statements in the Draw Method
The Draw method contains a block of statements. C# programs are expressed as a series ofstatements that are separated by a semicolon (;). Each statement describes a single actionthat your program needs to do. There are a number of different kinds of statements; youdiscover new ones as you learn more about programming. The statements are organized intoa single block. A block is a way to lump statements together. The start of a block is markedwith an open curly bracket character ({ )and the end of the block is marked with a closingcurly 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 andcomplains if you use the wrong kind of bracket.
In the preceding code, there is also a comment. Comments are ignored by the compiler; theylet 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 todo something. In this case, a programmer must add drawing statements at that position inthe program fi le. The compiler can tell that the text is a comment because it starts with thecharacter 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 likescomments. She says that a well-written program is like a story in the way that the purpose ofeach part is described. She says that she will be looking at our code and making sure that we putthe right kind of comments in.
From the point of view of changing the color of your screen, the statement that is mostinteresting 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 frameworklater; for now, all you need to know is that the Clear method is given something thatdescribes a color, and the method clears the screen to that color. At the moment, you aresending the Clear method the color CornflowerBlue, and it is clearing the screen to be thatcolor. 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 seethat the screen is now set to red.
Sample Code: Red Screen of Anger All the sample projects can be obtained from the Webresources for this text, which can be found at http://microsoft.learning.en/us/Books/13411.aspx.The sample project in the directory “01 MoodLight Red Screen” in the resources for this chapterdraws a red screen for you. You could run this when you felt particularly angry. You can changethe color that you want to display by changing the colors used in the Draw method; there aresome 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 ofyour own, which brings us to our first project.
This is a great book for beginners; enjoy!
Why can i not access the book's site to download the source code?
i keep getting an XML error
XML Parsing Error: not well-formed
Location: http://www.microsoft.com/learning/en/us/book.aspx?ID=13411&locale=en-us
Line Number 1, Column 666:
Hi, Shaun. I just tried the page and was able to download the code. Will you try again?
Looks like the page (and others on Microsoft's site) works in IE, but not in Firefox. Annoying.
Well, I can't find this book anywhere. Amazon lists it as a pre-order item at the moment and has little or no real information about it.
This i a little bit odd really.
Paul, I'm seeing it as "In Stock" at Amazon. Take a look: http://www.amazon.com/Microsoft%C2%AE-XNA%C2%AE-Game-Studio-3-0/dp/0735626588/ref=sr_1_1?ie=UTF8&s=books&qid=1257969538&sr=1-1