Game Programming
for Teens,
Second Edition
Maneesh Sethi
Publisher and General Manager,
Thomson Course Technology PTR:
Stacy L. Hiquet
Associate Director of Marketing:
Sarah O’Donnell
Manager of Editorial Services:
Heather Talbot
Marketing Manager:
Heather Hurley
Senior Acquisitions Editor:
Emi Smith
Senior Editor:
Mark Garvey
Marketing Coordinator:
Jordan Casey
Project Editor:
Jenny Davidson
Technical Reviewer:
Jonathan Harbour
Thomson Course Technology PTR
Editorial Services Coordinator:
Elizabeth Furbish
Copyeditor:
Kezia Endsley
Interior Layout Tech:
Jill Flores
because of the possibility of human or mechanical error by our sources,
Thomson Course Technology PTR, or others, the Publisher does not
guarantee the accuracy, adequacy, or completeness of any information
and is not responsible for any errors or omissions or the results obtained
from use of such information. Readers should be particularly aware of
the fact that the Internet is an ever-changing entity. Some facts may have
changed since this book went to press.
Educational facilities, companies, and organizations interested in multi-
ple copies or licensing of this book should contact the publisher for
quantity discount information. Training manuals, CD-ROMs, and por-
tions of this book are also available individually or can be tailored for
specific needs.
ISBN: 1-59200-834-8
Library of Congress Catalog Card Number: 2005923913
Printed in Canada
05 06 07 08 09 WC 10 9 8 7 6 5 4 3 2 1
Thomson Course Technology PTR,
a division of Thomson Course Technology
25 Thomson Place
Boston, MA 02210
For my parents, Neelam and Prabhjot
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 off the stage. Anyway, first of all 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 of his “busy” day helping read my text
VI
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
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
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
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .312
Chapter 11 Sounds and Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313
Sound . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .313
Loading Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .314
Listen Closely—Playing Sounds . . . . . . . . . . . . . . . . . . . . . . . . .316
Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330
Channels and PlayMusic() . . . . . . . . . . . . . . . . . . . . . . . . . . . . .330
Messing With ’Da Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . .331
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .335
Chapter 12 Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .337
Creating a MilliSecs() Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . .342
Chasing and Evading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
Chasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349
Evading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .354
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .356
Chapter 13 The Final Frontier: Invaderz!!! . . . . . . . . . . . . . . . . . . . . . .359
Let’s Bust It: Planning the Game . . . . . . . . . . . . . . . . . . . . . . . . . . .359
Constants, Functions, and Types in Invaderz!!! . . . . . . . . . . . . . . . .362
Playing Invaderz!!! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .366
Epilogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .368
Part IV: Appendixes . . . . . . . . . . . . . . . . . . . . . . .371
Appendix A Scan Code Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373
Appendix B Useful Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379
Blitz Basic Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .379
General Game Programming Links . . . . . . . . . . . . . . . . . . . . . . . . .380
Appendix C What’s on the CD. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382
Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382
Sounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .382