Chapter 1. Alien Attack

AUTHOR: Brian Peek

DIFFICULTY: Medium

TIME REQUIRED: 5 hours

COST: Windows—free; Xbox 360—cost of hardware, plus $99/year for XNA Creators Club membership (note that there are ways to get this for free, such as DreamSpark for student developers); Zune—cost of Zune device

SOFTWARE: Visual C# 2008 Express or any full edition of Visual Studio 2008 and XNA Game Studio 3.0

HARDWARE: Xbox 360 version requires an Xbox 360 with a hard drive; Zune version requires a Zune device

DOWNLOAD: http://www.c4fbook.com/AlienAttack

With the release of XNA Game Studio, Microsoft has brought homebrew game development to a new level. XNA provides an easy-to-use framework for developing games, and allows those games to be distributed and run not only on a PC, but also on the Xbox 360 and Zune. Additionally, because of a common framework and common development language, XNA provides for an almost perfect"write once and run everywhere" scenario. The code used for the Windows version will, in almost every case, compile and run on the Xbox 360 and Zune with few to no changes.

Overview

In this chapter, we walk through creating a very simple two-dimensional game using the XNA Framework that will run on a PC, Xbox 360, and Zune. We will discuss the concepts of 2-D graphics, sound, user input, and simple artificial intelligence (AI) while writing a clone of the arcade classic Space Invaders called Alien Attack, as shown in Figure 1-1.

Alien Attack

Figure 1-1. Alien Attack

As you build this game, keep in mind that it will be playable on all three supported platforms—Windows, Xbox 360, and Zune—using the same code base, as long as you write the code appropriately using compiler directives. Compiler directives can be used to block out portions of code depending on which version is being compiled. By default, Windows projects will define a symbol named WINDOWS, Xbox 360 projects will define a symbol named XBOX, and Zune projects will define a symbol named ZUNE. By using the #if compiler directives, we can make certain portions of code available to specific projects.

Get Coding4Fun now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.