intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Learning XNA 3.0 - Aaron Reed

Chia sẻ: Nguyễn Thị Ngọc Lựu | Ngày: | Loại File: PDF | Số trang:508

80
lượt xem
12
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

This book is meant to be a solid introduction to game development for somebody with basic knowledge of the NET Framework and C# (or similar technologies). No previous NA or other game or graphics development experience or knowledge is required.

Chủ đề:
Lưu

Nội dung Text: Learning XNA 3.0 - Aaron Reed

  1. Learning XNA 3.0
  2. Other resources from O’Reilly Related titles C# 3.0 in a Nutshell Learning ASP.NET 2.0 with Coding4Fun AJAX Essential Silverlight 2 Learning C# 3.0 Up-to-Date Physics for Game Developers oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’ll also find links to news, events, articles, weblogs, sample chapters, and code examples. oreillynet.com is the essential portal for developers interested in open and emerging technologies, including new platforms, pro- gramming languages, and operating systems. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free.
  3. Learning XNA 3.0 Aaron Reed Beijing • Cambridge • Farnham • Köln • Sebastopol • Taipei • Tokyo
  4. Learning XNA 3.0 by Aaron Reed Copyright © 2009 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 (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com. Editors: John Osborn and Laurel Ruma Indexer: John Bickelhaupt Production Editor: Sumita Mukherji Cover Designer: Karen Montgomery Copyeditor: Rachel Head Interior Designer: David Futato Proofreader: Sumita Mukherji Illustrator: Robert Romano Printing History: November 2008: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc. Learning XNA 3.0, the image of a sea robin fish, and related trade dress are trademarks of O’Reilly Media, Inc. .NET is a registered trademark of Microsoft Corporation. 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 contained herein. This book uses RepKover™ a durable and flexible lay-flat binding. , ISBN: 978-0-596-52195-0 [M]
  5. This book is dedicated to the most beautiful woman on earth (yeah, sorry guys...that would be my wife, Stacy) and our children (Hayden, Bryson, Rylan, and Kaylee).
  6. Table of Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 System Requirements 1 Additional Resources 2 Installation 2 Creating Your First XNA Application 4 What You Just Did 5 Summary 6 Test Your Knowledge: Quiz 7 2. Fun with Sprites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 A Look Behind the Scenes 8 Game Development Versus Polling 11 Modifying Your Game 14 Adding a Sprite to Your Project 15 Loading and Drawing Your Sprite 18 Transparency and Other Options 21 Layer Depth 25 Let’s Move 28 Animation 30 Adjusting the Framerate 35 Adjusting the Animation Speed 36 What You Just Did 38 Summary 38 vii
  7. Test Your Knowledge: Quiz 39 Test Your Knowledge: Exercise 39 3. User Input and Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 More Sprites 40 Keyboard Input 43 Mouse Input 45 Gamepad Input 47 Keeping the Sprite in the Game Window 50 Collision Detection 50 What You Just Did 55 Summary 56 Test Your Knowledge: Quiz 56 Test Your Knowledge: Exercise 57 4. Applying Some Object-Oriented Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Designing Your Classes 58 Creating a Sprite Class 59 Creating a User-Controlled Sprite Class 63 Creating an Automated Sprite 65 Game Components 67 Coding the SpriteManager 69 Cleaning Up 71 Making Them Move 74 What You Just Did 74 Summary 74 Test Your Knowledge: Quiz 75 Test Your Knowledge: Exercise 75 5. Sound Effects and Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Using XACT 77 Implementing XACT Audio Files in Code 83 Using the Simplified API for Sound and Audio 85 Adding More Sound to Your Game 86 What You Just Did 91 Summary 91 Test Your Knowledge: Quiz 91 Test Your Knowledge: Exercise 91 viii | Table of Contents
  8. 6. Basic Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 The Turing Test 93 Creating Sprites at Random Intervals 93 Randomly Spawning Sprites 95 Irrelevant Objects 99 Creating a Chasing Sprite 101 Creating an Evading Sprite 107 What You Just Did 112 Summary 113 Test Your Knowledge: Quiz 113 Test Your Knowledge: Exercise 113 7. Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Drawing 2D Text 115 Randomly Generating Different Sprite Types 121 Adding Some Variety to Your Sprites 123 Adding a Background Image 128 Game Scoring 130 Game States 137 Enabling/Disabling GameComponents 141 Game-Over Logic and the Game-Over Screen 143 Fine-Tuning Gameplay 150 Creating Power-Ups 154 What You Just Did 159 Summary 159 Test Your Knowledge: Quiz 160 Test Your Knowledge: Exercise 160 8. Deploying to the Microsoft Zune . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 Setting Up Your Zune Device Connection 161 Creating a Zune Project 163 Input on the Zune 165 Audio on the Zune 167 Resolution and Gameplay Issues 169 Converting the Collision Game from Windows to Zune 170 Conditional Compilation Symbols 171 Converting the Collision Game Audio 173 Converting the Collision Game’s Player Input Code 175 Converting the Collision Game’s Screen Size 178 Table of Contents | ix
  9. Zune Performance 179 What You Just Did 180 Summary 181 Test Your Knowledge: Quiz 181 9. 3D Game Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Coordinate Systems 182 Cameras 184 Creating a 3D Camera 186 Drawing Primitives 189 Matrix Multiplication 194 Movement and Rotation 194 Backface Culling 197 More on Rotations 198 Even More Rotations 200 Primitive Types 202 Applying Textures 204 What You Just Did 209 Summary 209 Test Your Knowledge: Quiz 210 Test Your Knowledge: Exercise 211 10. 3D Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212 Using 3D Models 212 Setting Up the Project 213 Adding a Model to Your Project 216 Drawing a Model Using a BasicModel Class 217 Adding a Model Manager 221 Rotating Your Model 224 What You Just Did 226 Summary 226 Test Your Knowledge: Quiz 226 Test Your Knowledge: Exercise 227 11. Creating a First-Person Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 Components of a Moving 3D Camera 228 Moving in a First-Person Camera 232 Rotations in a First-Person Camera 235 Coding the Camera for the 3D Game 240 x | Table of Contents
  10. What You Just Did 244 Summary 244 Test Your Knowledge: Quiz 245 Test Your Knowledge: Exercise 245 12. 3D Collision Detection and Shooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Creating a Moving Enemy 246 Adding Some Game Logic 248 Firing Shots 257 3D Collision Detection and Bounding Spheres 261 Adding a Crosshair 265 Adding Sound 266 What You Just Did 273 Summary 273 Test Your Knowledge: Quiz 274 Test Your Knowledge: Exercise 274 13. HLSL Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 HLSL Syntax 277 Dissecting a Sample HLSL Effect File 278 Applying an HLSL Effect in C# 287 Applying HLSL Using Textures 295 HLSL Effects: Creating a Negative 303 HLSL Effects: Blur 304 HLSL Effects: Grayscale 304 What You Just Did 306 Summary 307 Test Your Knowledge: Quiz 307 Test Your Knowledge: Exercise 307 14. Particle Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Creating a Custom Vertex 308 Creating a Particle Engine 312 Adding a Particle Effect File 320 Adding Your Particle Engine to Your Game 322 Adding a Starfield 325 What You Just Did 330 Summary 331 Test Your Knowledge: Quiz 331 Table of Contents | xi
  11. 15. Wrapping Up Your 3D Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 Adding a Splash Screen Game Component 332 Keeping Score 342 Adding a Power-Up 347 What You Just Did 352 Test Your Knowledge: Exercise 353 16. Deploying to the Xbox 360 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 Adding an Xbox 360 Device 354 Converting a Project to Run on the Xbox 360 358 Supporting Gamepad Input 361 Deploying to the Xbox 360 367 Xbox 360 Display Settings 369 The Title Safe Region 370 What You Just Did 372 Summary 372 Test Your Knowledge: Quiz 373 17. Multiplayer Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Split-Screen Functionality 374 Network Game Development 384 Network Configurations 385 Writing an XNA Network Game 387 Modifying the Sprite Class 388 Modifying the UserControlledSprite Class 388 Coding Your Game1 Class 391 Adding Update Code 394 Adding Draw Code 410 Adding Biohazard Bombs of Insanity! 415 What You Just Did 422 Summary 423 Test Your Knowledge: Quiz 424 Appendix: Answers to Quizzes and Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477 xii | Table of Contents
  12. Preface 1 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 that is 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 Chapters 8 and 2, and so forth, while fill- ing 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. Initially, I was going to simply create some papers and essays to give to the students. I then real- ized that a large audience outside of the university would probably benefit from the material as well. I contacted O’Reilly about the book, and the rest is history. Through my experience teaching XNA game development, I have been able to deter- mine which points typically snag students and which points are typically easily grasped. I’ve also developed what I feel is a pretty straightforward way of presenting the material in a sequence that makes sense and is easy to follow. This book is designed to follow that sequence and to introduce concepts in a way that will help readers to fully understand each individual topic. xiii
  13. Who This Book Is For This book is meant to be a solid introduction to game development for somebody with basic knowledge of the .NET Framework and C# (or similar technologies). No previous XNA or other game or graphics development experience or knowledge is required. How This Book Is Organized This book introduces XNA game development concepts while walking the reader through the development of three different XNA games. Although most of the mate- rial generally applies to developing games for any of the available platforms, Chap- ters 8 and 16 focus on developing games for the Zune and the Xbox 360, respectively. The first portion of the book takes the reader through the development of a 2D game. Here are the chapters that compose this section: Chapter 1, Getting Started Walks you through a short introduction to XNA, the tools needed to develop games in XNA, and the installation of XNA Game Studio 3.0. Chapter 2, Fun with Sprites Introduces 2D sprites, transparency, sort order, movement, framerates, sprite sheets, and animation. Chapter 3, User Input and Collision Detection Covers user input from keyboards, mice, and Xbox 360 gamepads, as well as the implementation of collision detection. Chapter 4, Applying Some Object-Oriented Design Discusses and implements game components and applies an object-oriented class hierarchy to the design of your game. Chapter 5, Sound Effects and Audio Introduces the Microsoft Cross-Platform Audio Creation Tool (XACT), as well as the new simplified audio API as methods to add sound to your games on the PC, Xbox 360, and Zune. Chapter 6, Basic Artificial Intelligence Explains the nature of the science of artificial intelligence and introduces basic artificial intelligence concepts; also walks through creating customized derived classes within your class hierarchy to implement different behaviors for your sprites. Chapter 7, Putting It All Together Puts the finishing touches on the 2D game, including 2D text, scoring, adding different types of sprites, background images, game states, and power-ups. xiv | Preface
  14. Chapter 8, Deploying to the Microsoft Zune Takes the 2D game created in the previous chapters and converts it for deploy- ment to the Zune. Most code is Zune-ready, but changes to audio and user input as well as changes to handle the smaller screen size of a Zune are made; the game is then deployed to the Zune. The next section of the book walks the reader through the development of a 3D game for the PC. At the end of this section, the game is tweaked and deployed to the Xbox 360. Here are the chapters in this section: Chapter 9, 3D Game Development Discusses coordinate systems, cameras, and drawing primitive objects, as well as moving, rotating, and scaling objects in 3D space. Culling and texturing sur- faces are also discussed. Chapter 10, 3D Models Introduces 3D models and discusses drawing, rotating, and moving 3D models in 3D space. Chapter 11, Creating a First-Person Camera Walks the reader through the creation of a first-person vector-based camera in 3D; implements forward and backward movement, strafing, and rotation in yaw, pitch, and roll. Chapter 12, 3D Collision Detection and Shooting Delves into the code behind shooting a moving enemy, creating a shot object, moving it in 3D space, and handling collision detection in 3D using bounding spheres; a 3D crosshair HUD and audio effects are also added to the game. Chapter 13, HLSL Basics Introduces High Level Shader Language (HLSL) syntax and implementation as well as the code required to use HLSL effects in XNA; a number of image manip- ulation effects are implemented using HLSL. Chapter 14, Particle Systems Walks the reader through the implementation of a custom vertex and a particle used to create an explosion particle effect. Chapter 15, Wrapping Up Your 3D Game Fine-tunes the 3D game, with sections covering splash screens, game states, scor- ing, and power-ups. Chapter 16, Deploying to the Xbox 360 Walks the reader through connecting an Xbox 360 to a PC and deploying to the Xbox 360; user input and screen resolution differences between the PC and the Xbox 360 are discussed. The last section of the book walks you through creating a network game in XNA. This section assumes knowledge of all previous chapters and comprises only one Preface | xv
  15. chapter, which concentrates on networking functionality in XNA while stepping through the creation of a new XNA game: Chapter 17, Multiplayer Games Introduces multiplayer concepts through split-screen functionality as well as net- working; topics include network architectures, network states, communication via packets, and gamer services. Finally, in the appendix, you’ll find the answers to the quizzes at the end of each chapter. Support My goal in writing this book is to help the reader gain a true understanding of and passion for game development in XNA. To that end, I’ll be supporting the book through my blog, which can be found at: http://www.aaronreed.com/serenitynow/ On that website you’ll find the source code for the book, as well as other XNA- related content (and a fair bit of non-XNA-related content as well). Feel free to chime in on the forums with questions, comments, or even answers. Conventions Used in This Book The following typographic conventions are used in this book: Italic Used for emphasis, technical terms where they are defined, URLs, email addresses, filenames, file extensions, and pathnames Constant width Used for code samples, methods, functions, variables and their values, objects, and class names Constant width bold Used for emphasis in code samples This icon signifies a tip, suggestion, or general note. This icon indicates a warning or caution. xvi | Preface
  16. Using Code Examples This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from this book does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning XNA 3.0, by Aaron Reed. Copyright 2009 Aaron Reed, 978-0-596-52195-0.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. We’d Like to Hear from You We have tested and verified the information in this book to the best of our ability, but you may find that features have changed or that we may have made a mistake or two (shocking and hard to believe as that may be). Please let us know about any errors you find, as well as your suggestions for future editions, by writing to: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) We have a web page for this book, where we list examples and any plans for future editions. You can access this information at: http://www.oreilly.com/catalog/9780596521950/ You can also send messages electronically. To be put on the mailing list or request a catalog, send an email to: info@oreilly.com To comment on the book, send an email to: bookquestions@oreilly.com For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our website at: http://www.oreilly.com Preface | xvii
  17. Safari® Books Online When you see a Safari® Books Online icon on the cover of your favorite technology book, that means the book is available online through the O’Reilly Network Safari Bookshelf. Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current informa- tion. Try it for free at http://safari.oreilly.com. Acknowledgments After writing this book, I sat back and reflected on the process I’ve been through dur- ing the past six months. The first question that came to my mind was, “What on earth drove me to sit down and write something like this in the first place?” The first part of the answer to that question would be my mom, Sheree. No, my mother wasn’t asking me to write a book; instead, her influence in this area came long before that. When I was pretty young, my brother and I became hooked on the original King’s Quest game. Mom came to our rescue countless times, because even though we had twenty fingers between the two of us, we were too young to type the word “swim” before Sir Graham would drown in the waters around the Kingdom of Daventry. The King’s Quest series instilled in me a love for gaming and computers in general that has dramatically shaped the direction of my life ever since. Thanks for keeping me afloat, Mom. The other part of the answer lies with the hundreds of students who’ve taken my DirectX and XNA graphics courses at Neumont University. From Brett Beardall’s purposefully annoying, “Aaaaarrrrroooonnnn, I have a quessssttttiioooonn” voice to Ryan Abreus’ unhealthy obsession with unicorns (yeah, don’t ask), you all are the reason I have developed such enthusiasm for XNA. Thank you! Beyond simply writing the book, my next question was, “How was it possible for me, somebody whose lengthiest writing was a four-page essay on economics in col- lege, to write something this large in scale?” The answer to that question is easy. I had a lot of help. Let’s face it, this book would have been pretty boring and lame and cluttered with incorrect and poorly written code if I had done all of this myself. Luckily, I had a ton of assistance from people far smarter than myself. Laurel Ruma from O’Reilly was there the entire way, helping me with all of my ques- tions and walking me through this process. Marlowe Shaeffer was a huge boost from the beginning, getting me started on the right path. Sumita Mukherji was a patient and fantastic production editor. xviii | Preface
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2