Learning XNA 4.0
Total Page:16
File Type:pdf, Size:1020Kb
Learning XNA 4.0 Learning XNA 4.0 Aaron Reed Beijing • Cambridge • Farnham • Köln • Sebastopol • Tokyo Learning XNA 4.0 by Aaron Reed Copyright © 2011 Aaron Reed. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editor: Courtney Nash Indexer: Fred Brown Production Editor: Kristen Borg Cover Designer: Karen Montgomery Copyeditor: Genevieve d’Entremont Interior Designer: David Futato Proofreader: Kristen Borg Illustrator: Robert Romano Printing History: December 2010: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning XNA 4.0, the image of a sea robin fish, and related trade dress are trade- marks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tained herein. ISBN: 978-1-449-39462-2 [M] 1291906059 To my mother and father, who, after shelling out $2,500 in the 1980’s for a sweet Compaq 286 with a 4-color monitor, 16k internal memory, a 5 1/4" floppy and no internal hard drive, didn’t yell at me when they came home and found that I’d taken the thing apart. I’ve been hooked ever since. 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 123 Test Your Knowledge: Quiz 124 Test Your Knowledge: Exercise 124 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 169 Test Your Knowledge: Quiz 169 Test Your Knowledge: Exercise 169 9. 3D Game Development . ............................................... 171 Coordinate Systems 171 Cameras 173 Creating a 3D Camera 176 Drawing Primitives 178 Matrix Multiplication 183 Movement and Rotation 184 Backface Culling 187 More on Rotations 188 Even More Rotations 190 Primitive Types 191 Applying Textures 195 Table of Contents | 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 Rotating a 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 Up 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 of Contents | 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 Game1 Class 414 Adding Update Code 417 Updating While in the SignIn 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 | Table of Contents Preface I’ve taught DirectX and XNA game development courses at Neumont University for the past several years, and I’ve been repeatedly frustrated by my inability to find the right book for these classes. There are numerous books on the subject, but none that I felt fit the goals of my classes (introductory college-level game development courses targeted to an audience familiar with C#) or that presented the material in a style and sequence I felt was appropriate. Many books required too much previous game or graphics development knowledge. Others assumed too little knowledge of nongame-related development concepts. Still others relied too much on third-party libraries, or presented the material in a sequence that would be counterintuitive to a reader who is new to game development. I found myself jumping around from Chapter 3 in one book to Chapter 18 in another, then back to the original book for Chapter 7 and Chapter 9, and so forth, while filling in gaps with slides, code samples, and documents I created on my own. Eventually, I decided to take the content I had developed for my XNA game develop- ment course and write some material on the subject for use in the classroom.