Beginning .NET Game Programming in En
Total Page:16
File Type:pdf, Size:1020Kb
Beginning .NET Game Programming in en DAVID WELLER, ALEXANDRE SANTOS LOBAo, AND ELLEN HATTON APress Media, LLC Beginning .NET Game Programming in C# Copyright @2004 by David Weller, Alexandre Santos Lobao, and Ellen Hatton Originally published by APress in 2004 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 978-1-59059-319-6 ISBN 978-1-4302-0721-4 (eBook) DOI 10.1007/978-1-4302-0721-4 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. Technical Reviewers: Andrew Jenks, Kent Sharkey, Tom Miller Editorial Board: Steve Anglin, Dan Appleman, Gary Cornell, James Cox, Tony Davis, John Franklin, Chris Mills, Steve Rycroft, Dominic Shakeshaft, Julian Skinner, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski Assistant Publisher: Grace Wong Project Manager: Sofia Marchant Copy Editor: Ami Knox Production Manager: Kari Brooks Production Editor: JanetVail Proofreader: Patrick Vincent Compositor: ContentWorks Indexer: Rebecca Plunkett Artist: Kinetic Publishing Services, LLC Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski The information in this book is distributed on an "as is" basis, without warranty. Although every precaution 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 indirectly by the information contained in this work. Para Ana: Mi esperanza, mi corazon, mi tesoro, mi amiga, mi amor. -David Weller Contents at a Glance Foreword ........................................................... .xi About the Authors ................................................ .xiii About the Technical Reviewers ................................... .xv Credits ........................................................... .xvi Acknowledgments .................................................. .xvii Preface ........................................................... .xix Introduction ...................................................... .xxi Chapter 1 • Nettrix: GDI + and Collision Detection ......... 1 Chapter 2 .Netterpillars: Artificial Intelligence and Sprites ....................................... 65 Chapter 3 Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI + ...................139 Chapter 4 Space Donuts: Sprites Revisited .............. .207 Chapter 5 Spacewar! ........................................243 Chapter 6 Spacewar3D: Meshes and Buffers and Textures, Oh My! ................................269 Chapter 7 Adding Visual Effects to Spacewar3D ..........325 Epilogue Taking Your Next Steps .........................341 Bonus Chapter Porting • Nettrix to Pocket PC .................349 Appendix A Suggested Reading ...............................369 Appendix B Motivations in Games ...........................373 Appendix C How Do I Make Games? ...........................379 Appendix 0 Guidelines for Developing Successful Games ..389 Index ..............................................................397 v Contents Foreword ........................................................... .xi About the Authors ................................................ .xiii About the Technical Reviewers ................................... .xv Credits ........................................................... .xvi Acknowledgments .................................................. .xvii Preface ........................................................... .xix Introduction ...................................................... .xxi Chapter 1 .Nettrix: GDI+ and Collision Detection .... 1 Basic GDI+ Concepts ................................................2 Performing Graphic Operations with a Graphics Object ...........4 Creating Gradients .................................................. 7 Collision Detection ................................................8 Optimizing the Number of Calculations ...........................17 Extending the Algorithms to Add a Third Dimension ............ .21 The Game Proposal ..................................................23 The Game Project ...................................................24 The Coding Phase .................................................. .30 Final Version: Coding the GameField Class and the Game Engine .................................................51 Adding the Final Touches .........................................59 Summary ............................................................. 63 Acknowledgments ....................................................64 Book Reference .....................................................64 Chapter 2 . Netterpillars: Artificial Intelligence and Sprites ...................... 65 Object-Oriented Programming ......................................66 Artificial Intelligence ........................................... 69 Sprites and Performance Boosting Tricks ........................ 76 The Game Proposal .................................................. 85 The Game Project ................................................... 86 vii Contents The Coding Phase ...................................................99 Adding the Final Touches ........................................134 Summary ............................................................138 Acknowledgments ...................................................138 Chapter 3 Managed DirectX First Steps: Direct3D Basics and DirectX vs. GDI+ .... 139 DirectX Overview ................................................. .140 3-D Coordinate Systems and Projections .........................151 Drawing Primitives and Texture ..................................158 The Application Proposal .........................................166 The Application Project ..........................................166 The Coding Phase ..................................................168 Adding the Final Touches ....................................... .202 More About DirectX and GDI+ .................................... .203 Summary ........................................................... .204 Acknowledgments .................................................. .205 Chapter 4 Space Donuts : Sprites Revisited ........... 207 Spri tes ........................................................... .208 Space Donuts ......................................................223 Summary ............................................................242 Acknowledgments ...................................................242 Chapter 5 Spacewar! ......................................... 243 About Spacewar ....................................................244 Methodology: Challenges of Working with Someone Else's Code ............................................246 Using the Application Wizard ....................................247 Direct Play .......................................................259 Summary ............................................................267 Acknowledgments ...................................................268 Chapter 6 Spacewar3D: Meshes and Buffers and Textures, Oh My! ......................... 269 DirectX Basics: The Application Wizard Revisited ............ .270 Spacewar3D .........................................................282 viii Contents The Game Proposal ................................................282 The Game Proj ect ..................................................283 Summary ............................................................323 Acknowledgments ...................................................323 Chapter 7 Adding Visual Effects to Spacewar3D ...... 325 Point Sprites .....................................................325 Step 10: Adding Thrust Effects to Spacewar3D ..................327 Step 11: Adding Explosion Effects to Spacewar3D ..............335 Step 12: Adding a Shockwave Effect to Spacewar3D .............336 Summary ............................................................338 Epilogue Taking Your Next Steps ......................... 341 Moving On ..........................................................341 Habits to Build ...................................................342 Things We Neglected to Tell you ................................346 Happy Trails ......................................................348 Bonus Chapter Porting . Nettrix to Pocket PC ........ 349 Programming for Mobile Devices ................................ ..350 Running Desktop PC Programs and Operating Systems on Mobile Devices .....................................354 The Game Proposal ................................................355 The Game Project ..................................................355 The Coding Phase ..................................................356 Adding the Final Touches ........................................366 Summary ............................................................367 Appendix A Suggested Reading ............................. 369 Game Programming ..................................................369 Math and Physics ................................................. .370 Computer Graphics ...............................................