Table Of ContentZune Game Development
Using XNA 3.0
Dan Waters
Zune Game Development Using XNA 3.0
Copyright © 2009 by Dan Waters
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-1861-6
ISBN-13 (electronic): 978-1-4302-1862-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewer: Shane DeSeranno
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell,
Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey Pepper,
Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh
Project Manager: Beth Christmas
Copy Editor: Marilyn Smith
Associate Production Director: Kari Brooks-Copony
Production Editor: Ellie Fountain
Compositor: Dina Quan
Proofreader: Liz Welch
Indexer: Carol Burbo
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail (cid:107)(cid:110)(cid:96)(cid:97)(cid:110)(cid:111)(cid:41)(cid:106)(cid:117)(cid:60)(cid:111)(cid:108)(cid:110)(cid:101)(cid:106)(cid:99)(cid:97)(cid:110)(cid:41)(cid:111)(cid:94)(cid:105)(cid:42)(cid:95)(cid:107)(cid:105), or
visit (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:111)(cid:108)(cid:110)(cid:101)(cid:106)(cid:99)(cid:97)(cid:110)(cid:107)(cid:106)(cid:104)(cid:101)(cid:106)(cid:97)(cid:42)(cid:95)(cid:107)(cid:105).
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail (cid:101)(cid:106)(cid:98)(cid:107)(cid:60)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105), or visit
(cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105).
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105)(cid:43)(cid:101)(cid:106)(cid:98)(cid:107)(cid:43)(cid:94)(cid:113)(cid:104)(cid:103)(cid:111)(cid:93)(cid:104)(cid:97)(cid:111).
The information in this book is distributed on an “as is” basis, without warranty. Although every precau-
tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability
to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indi-
rectly by the information contained in this work.
The source code for this book is available to readers at (cid:100)(cid:112)(cid:112)(cid:108)(cid:54)(cid:43)(cid:43)(cid:115)(cid:115)(cid:115)(cid:42)(cid:93)(cid:108)(cid:110)(cid:97)(cid:111)(cid:111)(cid:42)(cid:95)(cid:107)(cid:105). You will need to answer
questions pertaining to this book in order to successfully download the code.
This book is dedicated to my baby girl, Sydney. Sydney, when you are old
enough to read, you will see that even while writing about all this
technical stuff, I was still thinking of you first.
I also dedicate this book to my wife Jasmine, who has supported me through the
months of very, very late nights (and a burgeoning energy drink habit) to
make the most of this opportunity.
Finally, this book is dedicated to my grandmother Louise, who passed away in
2008. Rest in peace, Grams. We love and miss you very much.
Contents at a Glance
About the Author ..................................................................xiii
About the Technical Reviewer ...................................................... xv
Acknowledgments ............................................................... xvii
Introduction ......................................................................xix
CHAPTER 1 Getting Started ..................................................1
CHAPTER 2 Introduction to XNA Game Studio 3.0 ............................13
CHAPTER 3 Game Content ..................................................27
CHAPTER 4 Developing for the Zune ........................................57
CHAPTER 5 XNA Fundamental Game Programming Concepts ...............129
CHAPTER 6 Advanced Techniques .........................................203
CHAPTER 7 Final Exercise: Multiplayer Crazy Eights ........................251
APPENDIX A Recommended Resources .....................................375
APPENDIX B Zune Development Quick Reference Guide .....................377
APPENDIX C Check Your Knowledge Answers ...............................385
INDEX .......................................................................389
v
Contents
About the Author ..................................................................xiii
About the Technical Reviewer ...................................................... xv
Acknowledgments ............................................................... xvii
Introduction ......................................................................xix
CHAPTER 1 Getting Started ...............................................1
Downloading and Installing the Software ............................1
Installing Visual Studio 2008 ..................................2
Installing XNA Game Studio 3.0 ................................6
Your First “Game” ................................................9
Summary .......................................................12
CHAPTER 2 Introduction to XNA Game Studio 3.0 ......................13
The Project Structure of an XNA Game .............................13
Important Methods in an XNA Game ...............................15
Initialize Method ............................................17
LoadContent Method ........................................17
UnloadContent Method ......................................18
Update Method .............................................18
Draw Method ..............................................20
XNA Game Flow .................................................21
Putting It All Together ............................................22
Check Your Knowledge ...........................................26
Summary .......................................................26
CHAPTER 3 Game Content ...............................................27
What Is Content? ................................................27
Types of Content ................................................28
Images ....................................................28
Audio .....................................................35
Fonts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37
vii
viii (cid:78)CONTENTS
Effects ....................................................38
XML Data ..................................................38
The XNA Content Pipeline .........................................38
What the Content Pipeline Provides ...........................39
Content Pipeline Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39
Content Stages .............................................41
Loading Content at Runtime ..................................41
Custom Importers and Processors .................................42
The Content Type Class .....................................43
The Intermediate Content and Reader Classes ..................46
The Writer Class ............................................48
The Content Processor Class .................................49
Check Your Knowledge ...........................................55
Summary .......................................................55
CHAPTER 4 Developing for the Zune ....................................57
Deploying to the Zune ............................................57
Updating Your Zune’s Firmware ..............................58
Registering Your Zune with Visual Studio ......................59
Debugging, Running, and Rebooting ...............................66
Running with Debugging ....................................67
Running Without Debugging .................................67
Creating Windows Versions of Zune Games .........................68
Zune Specifications and Optimization ..............................71
Reviewing the Core Zune Specifications .......................71
Writing Performant Code for the Zune .........................71
Handling Input on the Zune .......................................73
Accessing the Zune Pad .....................................73
Accessing the Directional Click Buttons ........................77
Accessing Other Buttons .....................................77
Using an Input Handler Class for Zune and Windows Games. . . . . .78
Playing Music in Zune Games .....................................93
Using Media Player Component Classes .......................93
Using the Guide ............................................95
Putting It Together ..........................................96
Your First Real Zune Game: OutBreak .............................103
Designing OutBreak ........................................103
Creating OutBreak .........................................107
Check Your Knowledge ..........................................127
Summary ......................................................127
(cid:78)CONTENTS ix
CHAPTER 5 XNA Fundamental Game Programming Concepts .......129
Math in Two Dimensions ........................................130
The Cartesian Coordinate System ............................130
Points and Vectors .........................................132
Trigonometric Functions ....................................137
Using Math in Your Games .......................................138
Positioning Screen Objects and Dividing by Two ...............138
Locating Sides of Objects ...................................142
Creating Bounding Boxes ...................................143
Detecting Collisions with Bounding Boxes .....................144
Emulating Physics .........................................144
Using Other Time-Dependent Functions ......................148
Collision Detection Revisited .....................................152
Simple Collision Detection ..................................153
Per-Pixel Collision Detection ................................153
Simple Game State Management .................................159
Using Components .............................................162
Bringing It All Together with Monkey Feeder .......................169
Configuring the Solution, Projects, Folders, and Assets .........171
Creating the Components ...................................172
Putting the Components Together. . . . . . . . . . . . . . . . . . . . . . . . . . . .189
Setting Game Properties to Appear More Professional ..........197
Build, Run, Copious Celebration .............................197
Suggested Extensions ......................................199
Cleaning Up for Deployment .....................................199
Making Changes in AssemblyInfo.cs .........................199
Changing the Thumbnail ....................................200
Check Your Knowledge ..........................................200
Summary ......................................................201
CHAPTER 6 Advanced Techniques .....................................203
Device Status ..................................................203
Checking the Battery Status .................................203
Determining Battery Life ....................................205
Determining If the Zune Is Plugged In ........................205
Determining When the Power Status Changes .................206
Advanced Sprite Batch Techniques ...............................213
How Sprite Batches Work ...................................213
Blend Modes ..............................................213
Sprite Batches and Game Performance .......................217
x (cid:78)CONTENTS
Games in Landscape Mode ......................................223
Challenges with Landscape Mode ...........................223
Possible Solutions for Landscape Mode. . . . . . . . . . . . . . . . . . . . . . .224
Best Practices and Considerations for Mode Support ...........225
Using Render Targets for Landscape Mode ...................225
Componentizing Landscape Mode ...........................231
Game State Management with Screens ...........................231
Screen Management Samples ...............................231
Building a Screen Management System ......................232
Storage on the Zune ............................................234
Advanced Touch Sensitivity on the Zune ...........................239
Graphic Sound Visualizers .......................................243
Check Your Knowledge ..........................................249
Summary ......................................................250
CHAPTER 7 Final Exercise: Multiplayer Crazy Eights .................251
Wireless Networking with the Zune ...............................251
Elements of a Multiplayer Game ..................................252
How Data is Transmitted in Wireless Zune Games .............252
The Network API and Tags ..................................252
Robot Tag: A Two-Player Game ..................................253
Game Solution Structure ....................................254
Network Session Management ..............................255
The Robot Object ..........................................257
Game Screens ............................................259
Robot Tag Review .........................................268
Multiplayer Crazy Eights for the Zune .............................268
Rules of Crazy Eights .......................................268
Game Requirements .......................................269
Network State Design ......................................270
Architectural Options (High Level) ............................273
Screen Design ............................................273
Building the Card Library ...................................277
Building the Screen Manager Library .........................289
Building Crazy Eights .......................................313
Summary ......................................................373
(cid:78)CONTENTS xi
APPENDIX A Recommended Resources .................................375
Blogs .........................................................375
Microsoft Resources ............................................376
Creators Club Samples ..........................................376
APPENDIX B Zune Development Quick Reference Guide ...............377
Animation .....................................................377
Input Handling .................................................377
Gesture Support ................................................378
Forcing Screen Dimensions on Windows ..........................379
Playing Music ..................................................379
Collision Detection ..............................................379
Changing Game Properties ......................................380
Changing the Thumbnail ........................................381
Checking Battery Status .........................................381
Drawing in Landscape Mode .....................................382
Using Storage ..................................................382
Creating Visualizers .............................................383
APPENDIX C Check Your Knowledge Answers .........................385
Chapter 2 .....................................................385
Chapter 3 .....................................................386
Chapter 4 .....................................................386
Chapter 5 .....................................................387
Chapter 6 .....................................................387
INDEX .......................................................................389
About the Author
(cid:78)DAN WATERS is an Academic Developer Evangelist at Microsoft,
supporting students and educators in Florida, Georgia, Alabama,
and Mississippi who use Microsoft technology in (and outside) the
classroom. Prior to his work at Microsoft, Dan spent eight years as a
developer designing and building business applications for companies
in a wide range of vertical industries.
Dan is an avid musician and suffers from an addiction to guitars,
which can be spotted in several rooms in his home. He produces elec-
tronic music in his spare time.
Dan also enjoys traveling, gaming, bacon (occasionally), and most important, being a
proud papa.
xiii