Learning XNA 4.0 Aaron Reed O'REILLY8 Beijing Cambridge Farnham Koln Sebastopoi Tokyo
Table of Contents Preface xiii 1. What's New in XNA 4.0? 1 Revised Project Folder Structure 1 Develop Games for Windows Phone 7 Series 2 Graphics Profiles 2 Configurable Effects 4 Built-in State Objects 5 Scalars and Orientation 5 Revised Input API 5 Additional Audio API 6 Music and Picture Enumeration and Video Playback 6 Modified and Added Classes and Methods 6 Test Your Knowledge: Quiz 6 2. Getting Started 7 System Requirements 7 Additional Resources 8 Installation 8 Creating Your First XNA Application 10 What You Just Did 13 Summary 13 Test Your Knowledge: Quiz 14 3. Fun with Sprites 15 A Look Behind the Scenes 15 Game Development Versus Polling 18 Modifying Your Game 21 Adding a Sprite to Your Project 22 Loading and Drawing Your Sprite 25 Transparency and Other Options 28 vii
Layer Depth 33 Let's Move 36 Animation 38 Adjusting the Framerate 43 Adjusting the Animation Speed 44 What You Just Did 45 Summary 46 Test Your Knowledge: Quiz 46 Test Your Knowledge: Exercise 47 4. User Input and Collision Detection 49 More Sprites 49 Keyboard Input 52 Mouse Input 54 Gamepad Input 56 Keeping the Sprite in the Game Window 59 Collision Detection 60 What You Just Did 65 Summary 65 Test Your Knowledge: Quiz 65 Test Your Knowledge: Exercise 66 5. Applying Some Object-Oriented Design 67 Designing Your Classes 67 Creating a Sprite Class 68 Creating a User-Controlled Sprite Class 72 Creating an Automated Sprite Class 74 Game Components 77 Coding the SpriteManager 78 Cleaning Up 80 Making Them Move 83 What You Just Did 83 Summary 84 Test Your Knowledge: Quiz 84 Test Your Knowledge: Exercise 84 6. Sound Effects and Audio 85 Using XACT 86 Implementing XACT Audio Files in Code 92 Using the Simplified API for Sound and Audio 94 Adding More Sound to Your Game 96 What You Just Did 100 Summary 100 viii Table of Contents
Test Your Knowledge: Quiz 101 Test Your Knowledge: Exercise 101 7. Basic Artificial Intelligence 103 The Turing Test 104 Creating Sprites at Random Intervals 104 Randomly Spawning Sprites 106 Irrelevant Objects 110 Creating a Chasing Sprite 112 Creating an Evading Sprite 117 What You Just Did 123 Summary Test Your Knowledge: Quiz 124 Test Your Knowledge: Exercise 124 123 8. Putting It All Together 125 Drawing 2D Text 126 Randomly Generating Different Sprite Types 132 Adding Some Variety to Your Sprites 133 Adding a Background Image 139 Game Scoring 140 Game States 147 Enabling/Disabling GameComponents 150 Game-Over Logic and the Game-Over Screen 153 Fine-Tuning Gameplay 161 Creating Power-Ups 164 What You Just Did 168 Summary Test Your Knowledge: Quiz Test Your Knowledge: Exercise 169 169 169 9. 3D Game Development 171 Coordinate Systems 171 Cameras 173 Creating a 3D Camera 176 Drawing Primitives 178 Matrix Multiplication Movement and Rotation 184 Backface Culling 187 More on Rotations 188 Even More Rotations 190 Primitive Types 191 Applying Textures 195 183 Table ofcontents ix
What You Just Did 198 Summary 199 Test Your Knowledge: Quiz 200 Test Your Knowledge: Exercise 200 10. 3D Models 201 Using 3D Models 201 Setting Up the Project 202 Adding a Model to Your Project 205 Drawing a Model Using a BasicModel Class 206 Adding a Model Manager 210 Rotating Your Model 212 What You Just Did 214 Summary 215 Test Your Knowledge: Quiz 216 Test Your Knowledge: Exercise 216 11. Creating a First-Person Camera 217 Components of a Moving 3D Camera 217 Moving in a First-Person Camera 221 Rotations in a First-Person Camera 224 a Rotating Camera in a Yaw 226 Rotating a Camera in a Roll 227 Rotating a Camera in a Pitch 228 Coding the Camera for the 3D Game 229 What You Just Did 233 Summary 233 Test Your Knowledge: Quiz 234 Test Your Knowledge: Exercise 234 12. 3D Collision Detection and Shooting 235 Creating a Moving Enemy 235 Adding Some Game Logic 237 Firing Shots 246 3D Collision Detection and Bounding Spheres 251 Adding a Crosshair 253 Adding Sound 256 What You Just Did 262 Summary 262 Test Your Knowledge: Quiz 263 Test Your Knowledge: Exercise 263 x Table of Contents
13. HLSL Basics 265 HLSL Syntax 267 Dissecting a Sample HLSL Effect File 269 Applying an HLSL Effect in C# 277 Applying HLSL Using Textures 285 HLSL Effects: Creating a Negative 293 HLSL Effects: Blur 293 HLSL Effects: Grayscale 295 What You Just Did 296 Summary 296 Test Your Knowledge: Quiz 297 Test Your Knowledge: Exercise 297 14. Particle Systems 299 Creating a Particle Engine 299 Adding a Particle Effect File 310 Adding Your Particle Engine to Your Game 312 Adding a Starfield 315 What You Just Did 320 Summary 321 Test Your Knowledge: Quiz 321 15. Wrapping lip Your 3D Game 323 Adding a Splash Screen Game Component 323 Keeping Score 334 Adding a Power-Up 338 What You Just Did 343 Test Your Knowledge: Exercise 344 16. Deploying to the Xbox 360 345 Adding an Xbox 360 Device 345 Converting a Project to Run on the Xbox 360 349 Supporting Gamepad Input 351 Deploying to the Xbox 360 358 Xbox 360 Display Settings 360 The Title Safe Region 361 What You Just Did 363 Summary 363 Test Your Knowledge: Quiz 363 17. Developing for Windows Phone 7 365 Setting Up Your Windows Phone 7 Device 365 Table ofcontents xi
Registering for a Developer Account 366 Registering Your Windows Phone 7 Device 368 Unlocking Your Phone 369 Creating a Windows Phone 7 Project 370 Resources 372 Digging In 373 Screen Rotation and Resolution 378 More Model Classes 380 Adding a ModelManager 386 Finishing Details 391 What You Just Did 395 Summary 395 Test Your Knowledge: Quiz 395 18. Multiplayer Games 397 Split-Screen Functionality 397 Network Game Development 406 Network Configurations 408 Writing an XNA Network Game 410 Modifying the Sprite Class 411 Modifying the UserControlledSprite Class 411 Coding Your Game 1 Class 414 Adding Update Code 417 Updating While in the Signln Game State 418 Updating While in the FindSession Game State 419 Updating While in the CreateSession GameState 422 Updating While in the Start Game State 422 Updating While in the InGame Game State 430 Updating While in the GameOver Game State 431 Adding Draw Code 432 Adding Biohazard Bombs of Insanity! 438 What You Just Did 444 Summary 445 Test Your Knowledge: Quiz 446 Appendix: Answers to Quizzes and Exercises 447 Index 501 xii I Table of Contents