Game Programming for Teens, Second Edition Maneesh Sethi ©2005 by Thomson Course Technology PTR.All rights reserved.No part Publisher and General Manager, of this book may be reproduced or transmitted in any form or by any Thomson Course Technology PTR: means,electronic or mechanical,including photocopying,recording,or Stacy L.Hiquet by any information storage or retrieval system without written permis- Associate Director ofMarketing: sion from Thomson Course Technology PTR,except for the inclusion of briefquotations in a review. Sarah O’Donnell The Premier Press and Thomson Course Technology PTR logo and Manager ofEditorial Services: related trade dress are trademarks of Thomson Course Technology and Heather Talbot may not be used without written permission. Marketing Manager: BlitzPlus, Blitz3D, BlitzBasic2D, and BlitzMax are trademarks of Blitz Heather Hurley Research,Ltd.Paint Shop Pro is a registered trademark ofCorel Corpora- Senior Acquisitions Editor: tion. MilkShape 3D is a registered trademark of ChumbaLum sOft. Emi Smith CoolEdit Pro is a registered trademark ofSyntrillium Software Corporation. Senior Editor: All other trademarks are the property oftheir respective owners. Mark Garvey Important:Thomson Course Technology PTR cannot provide software support.Please contact the appropriate software manufacturer’s techni- Marketing Coordinator: cal support line or Web site for assistance. Jordan Casey Thomson Course Technology PTR and the author have attempted through- Project Editor: out this book to distinguish proprietary trademarks from descriptive terms Jenny Davidson by following the capitalization style used by the manufacturer. Technical Reviewer: Information contained in this book has been obtained by Thomson Jonathan Harbour Course Technology PTR from sources believed to be reliable.However, Thomson Course Technology PTR because ofthe possibility ofhuman or mechanical error by our sources, Editorial Services Coordinator: Thomson Course Technology PTR, or others, the Publisher does not Elizabeth Furbish guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained Copyeditor: from use of such information.Readers should be particularly aware of Kezia Endsley the fact that the Internet is an ever-changing entity.Some facts may have changed since this book went to press. Interior Layout Tech: Jill Flores Educational facilities,companies,and organizations interested in multi- ple copies or licensing of this book should contact the publisher for Cover Designer: quantity discount information.Training manuals,CD-ROMs,and por- Mike Tanamachi tions of this book are also available individually or can be tailored for specific needs. CD-ROM Producer: Brandon Penticuff ISBN:1-59200-834-8 Library ofCongress Catalog Card Number:2005923913 Indexer: Printed in Canada Sharon Shock 05 06 07 08 09 WC 10 9 8 7 6 5 4 3 2 1 Proofreader: Sara Gullion Thomson Course Technology PTR, a division ofThomson Course Technology 25 Thomson Place Boston,MA 02210 http://www.courseptr.com For my parents, Neelam and Prabhjot Acknowledgments J eez,there are so many people to thank.This feels like I’m accepting an Oscar™:I’m afraid the band will start playing and usher me offthe stage.Anyway,first ofall thank you André LaMothe for giving me a chance and taking me out to lunch.I will get you back for that one day.Thank you everyone at Premier Press:my production editor Jenny Davidson,my copy editor,Kezia Endsley,my acquisitions editor,Emi Smith,and my CD editor,Brandon Penticuff. Thanks go out to Adam Hepworth,who spent a lot ofhis “busy”day helping read my text and correcting a lot oferrors.To all ofmy brothers and sisters,Rachi,who offered her help even though she was across an ocean, Nagina, whose love and support (and numerous phone calls) helped carry me through,and Ramit,who gave me a person to look up to and who helped me through the thick and thin (as well as giving me the chance to use “abeyance”in a sentence).Thanks for being here and giving me support when I needed it. To my mom and dad:I couldn’t have done this without you.I love you so much. Finally,thanks to Edgar L.Ibarra (Feo) for his artwork and Thomas Stenbäck for his work on the CD’s music.Also,to Ari Feldman,for allowing me to use his sprite library on the CD. Also, for contributing a demo program to the book, thanks to Jason Brasier and Edgar Ibarra for Rockfalland Marcus “Eikon”Smith for Galaxiga. And to everyone whose name I forgot,you know who you are.Thanks! IV About the Author MANEESH SETHI is a high school student in California who will be enrolled at Stanford University in 2006.Maneesh has worked with Web design and development since he was in fifth grade and was the founder and head designer of Standard Design, a Web site design company.Maneesh has taught game programming on TechTV’s Call for Helpand at game programming conferences such as the XGDX. He is the author of Game Programming for Teens (First Edition)andWeb Design for Teens,both published by Course PTR,and How to Succeed as a Lazy Student. Besides game programming, Maneesh enjoys playing games (of course), sports such as tennis and basketball, and of course, sleep. Learn more about Maneesh, as well as his award-winning T-shirts and iPod sock case at www.maneeshsethi.com. V Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi Part I: The Basics of BASIC . . . . . . . . . . . . . . . . . . .1 Chapter 1 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 A Brief History of BASIC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Installing BlitzPlus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Windows and Panels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Toolbars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 The First Game: KONG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Compiling the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18 Chapter 2 Getting to Know BASIC . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Hello, World! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23 Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 Truth and Falsehood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 If...Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 VI Contents VII If...Then…Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .30 Select…Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33 The NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 The Goto Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 A Text-Based Guessing Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 Chapter 3 Loops, Functions, Arrays, and Types . . . . . . . . . . . . . . . . . .41 Understanding Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 For…Next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 While…Wend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Repeat…Until . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Understanding Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51 Scope Considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 When to Use Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Understanding Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 Multi-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64 Using Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .67 Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75 For…Each…Next . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Putting It All Together: Textanoid! . . . . . . . . . . . . . . . . . . . . . . . . . .79 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .93 Chapter 4 The Style Factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 Developing Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95 White Space and Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Pre-Program Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Main Program Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 Function Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 Function and Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Naming Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103 VIII Contents Part II: Getting Graphical . . . . . . . . . . . . . . . . . .105 Chapter 5 Beginning Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 Creating the Graphics Window . . . . . . . . . . . . . . . . . . . . . . . . . . . .107 Width and Height . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .108 Color Depth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109 [Mode] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 LoadImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 DrawImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .116 CreateImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118 MaskImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 RGB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Cls and ClsColor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .131 Chapter 6 Page Flipping and Pixel Plotting . . . . . . . . . . . . . . . . . . . .133 Page Flipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .133 Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136 SetBuffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 CreateImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .143 Locking and Unlocking Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . .156 Lock/Unlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156 ReadPixel()/ReadPixelFast() . . . . . . . . . . . . . . . . . . . . . . . . . . . .157 WritePixel/WritePixelFast . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161 Using Buffers: A Paint Program . . . . . . . . . . . . . . . . . . . . . . . . . . . .166 Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 Contents IX Chapter 7 Basic Image Programming . . . . . . . . . . . . . . . . . . . . . . . . .181 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 Translating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181 Scaling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186 Rotation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209 Parallaxing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217 TileBlock and TileImage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .225 Chapter 8 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Using Bitmaps in Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227 Making Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234 Displaying Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 Chapter 9 Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249 Basic Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249 Bounding Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 Distance between Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .253 Radii . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .255 Bounding Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .260 Pixel-Imperfect Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .268 Pixel-Perfect Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .274 Part III: Completing the Puzzle . . . . . . . . . . . . . .275 Chapter 10 Handling Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277 Handling the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .277 KeyDown() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .278 KeyHit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285 Mapping the Mouse to the Screen . . . . . . . . . . . . . . . . . . . . . . . . .294 Displaying the Mouse Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . .295 What Was That? Handling Mouse Key Presses . . . . . . . . . . . . .298 The Middle Mouse Wheel . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306 Handling Joystick Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312