Programming Directsound
Total Page:16
File Type:pdf, Size:1020Kb
Microsoft Visual Basic® Game Programming for Teens Jonathan S. Harbour © 2005 by Thomson Course Technology PTR. All rights reserved. No SVP, Thomson Course part of this book may be reproduced or transmitted in any form or by Technology PTR: any means, electronic or mechanical, including photocopying, record- Andy Shafran ing, or by any information storage or retrieval system without written permission from Thomson Course Technology PTR, except for the Publisher: inclusion of brief quotations in a review. Stacy L. Hiquet The Premier Press and Thomson Course Technology PTR logo and Senior Marketing Manager: related trade dress are trademarks of Thomson Course Technology PTR Sarah O’Donnell and may not be used without written permission. Marketing Manager: Microsoft, Visual Basic, and DirectX are registered trademarks of Heather Hurley Microsoft Corporation in the United States and/or other countries. Manager of Editorial Services: All other trademarks are the property of their respective owners. Heather Talbot Important: Thomson Course Technology PTR cannot provide software Acquisitions Editor: support. Please contact the appropriate software manufacturer’s techni- Mitzi Koontz cal support line or Web site for assistance. Senior Editor: Thomson Course Technology PTR and the author have attempted Mark Garvey throughout this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the Associate Marketing Manager: manufacturer. Kristin Eisenzopf Information contained in this book has been obtained by Thomson Marketing Coordinator: Course Technology PTR from sources believed to be reliable. However, Jordan Casey because of the possibility of human or mechanical error by our sources, Thomson Course Technology PTR, or others, the Publisher does not Project Editor: guarantee the accuracy, adequacy, or completeness of any information Scott Harris/Argosy Publishing and is not responsible for any errors or omissions or the results obtained Technical Reviewer: from use of such information. Readers should be particularly aware of Maneesh Sethi the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press. Teen Reviewer: Andrew Nguyen Educational facilities, companies, and organizations interested in multi- ple copies or licensing of this book should contact the publisher for PTR Editorial Services Coordinator: quantity discount information. Training manuals, CD-ROMs, and por- Elizabeth Furbish tions of this book are also available individually or can be tailored for specific needs. Copy Editor: Tonya Cupp ISBN: 1-59200-587-X Interior Layout Tech: Library of Congress Catalog Card Number: 2004114414 Kate Binder Printed in the United States of America Cover Designer: 04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1 Mike Tanamachi CD-ROM Producer: Brandon Penticuff Indexer: Maureen Shepherd Thomson Course PTR, a division of Course Technology Proofreader: 25 Thomson Place Jan Cocker Boston, MA 02210 http://www.courseptr.com For my Grandmother, Myrt Cremeen Acknowledgments his book would not have been possible were it not for the hard-working editors, artists, proofreaders, marketers, indexers, layout specialists, and managers at Pre- Tmier Press (Course PTR). Thank you to everyone at Premier Press and Argosy Publishing for doing such great work: Mitzi Koontz, Scott Harris, Jenny Davidson, Bran- don Penticuff, Emi Smith, Stacy Hiquet, Andy Shafran, Sarah O’Donnell, Heather Hurley, Mark Garvey, Heather Talbot, Kristin Eisenzopf, Jordan Casey, Elizabeth Furbish, Mike Tanamachi, Tonya Cupp, Kate Binder, Maureen Shepherd, and Jan Crocker. Thank you to Maneesh Sethi for his technical review of the manuscript. I believe you will find this a solid book due to all of their efforts. Thanks to my wonderful wife, Jennifer, and our rambunctious little ones, Jeremiah and Kayleigh, and our third one on the way. You guys make every aspect of our lives together a joy, and it is a privilege to share mine with you. I am extremely grateful to Reiner Prokein for the use of his extraordinary artwork at Reiner’s Tilesets (http://www.reinerstileset.de). I can honestly say I would not have even attempted to create the RPG in this book without Reiner’s incredible talent at hand. Art- work is everything! Thank you for providing your high-quality tiles and sprites to the world. I also owe my thanks and appreciation to Robin B. for his excellent level editor, Mappy (http://www.tilemap.co.uk). In addition, thank you to Daniel Sczepansky at Cosmigo for Pro Motion (http://www.cosmigo.com), the powerful sprite animation software featured in this book. iv About the Author JONATHAN S. HARBOUR has been an avid gamer and programmer for 17 years, having started with early systems like the Commodore PET, Apple II, and Tandy 1000. He holds a bachelor of science degree in computer information systems and enjoys writing code in several languages, including C, C++, and VB. He has experience with several platforms, including Windows, Linux, Pocket PC, and Game Boy Advance. Jonathan has written nine books on the subjects of game programming, application development, console pro- gramming, cross-platform programming, and console modding. He maintains a Web site dedicated to game programming at http://www.jharbour.com. v Contents at a Glance Introduction . .xvii Part I: Introduction 1 Chapter 1 Getting Started with Visual Basic . 3 Chapter 2 Your First DirectX Program . 23 Chapter 3 Designing the Game . 39 Part II: Creating an Interactive Game World 63 Chapter 4 Core Technique: Drawing Bitmaps . 65 Chapter 5 Level Editing with Mappy . 79 Chapter 6 The Basics of Tile-Based Scrolling . 99 Chapter 7 Scrolling the Game World . 113 vi Contents at a Glance vii Chapter 8 Advanced Scrolling Techniques . 133 Part III: The Player’s Character (PC) 157 Chapter 9 Core Technique: Drawing Sprites . 159 Chapter 10 Core Technique: Animating Sprites . 183 Chapter 11 Core Technique: User Input . 201 Chapter 12 Walking Around in the Game World . 231 Chapter 13 Core Technique: Drawing Text . 255 Chapter 14 Core Technique: Collision Detection . 261 Part IV: Non-Player Characters (NPCs) 269 Chapter 15 Creating the Character Classes . 271 Chapter 16 Adding NPCs to the Game World . 279 Chapter 17 Talking with NPCs . 293 Chapter 18 Engaging in Combat with NPCs . 303 Part V: Finishing Touches 327 Chapter 19 Adding Scenery to the Game World . 329 Chapter 20 Core Technique: Sound Effects . 341 viii Contents at a Glance Chapter 21 Core Technique: Background Music . 349 Appendix Using the CD-ROM . 357 Index . 359 Contents Introduction . xvii Visual Basic and DirectX . xvii Pacing and Experience . xvii Prerequisites . xviii Which Version of Visual Basic? . xviii Which Version of DirectX? . xviii Contacting the Author . xviii Conventions Used in This Book . xix Part I: Introduction 1 Chapter 1 Getting Started with Visual Basic . 3 Game Programming . 3 Get Your Feet Wet First, Ask Questions Later . 4 Let Your Creativity Fly . 5 Creativity, Talent, and Hard Work . 5 The Sky Is the Limit . 6 Learn the Trade . 7 Getting Started with DirectX . 8 Adding a Reference to the DirectX Type Library . 10 Testing the DirectX Reference . 10 Ready to Write Some Code? . 14 ix x Contents Overview of the Game Project . 14 Building a Role-Playing Game . 14 The Story . 15 Good Guys Versus Bad Guys . 16 Adventure Game or Dungeon Hack? . 16 Describing the Player’s Character . 18 Summary . 21 Chapter 2 Your First DirectX Program. 23 Initializing DirectX . 23 Creating a New Project . 24 The DirectX and Direct3D Objects . 24 Creating the Primary Device . 26 The InitDirectX Program . 28 Loading and Displaying a Bitmap File . 30 The New LoadBitmap Project . 30 Variables . 31 Program Startup . 33 Initializing Direct3D . 34 Loading a Bitmap File . 35 Drawing the Bitmap . 37 Level Up . 38 Chapter 3 Designing the Game. 39 The Quest-Based Storyline . 39 Designing the RPG World . 40 Map of the World . 41 Regions . 44 The Player’s Character (PC) . 49 Character Attributes . ..