Game Programming for Teens, Seconnd Edition P1

Jeez, 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.

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 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
and correcting a lot of errors. To all of my 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 Rockfall and 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 Help and
at game programming conferences such as the XGDX. He is the author of Game
Programming for Teens (First Edition) and Web 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

Introduction
H ey, reader. Thanks for buying my book. I am really thankful for it.
What is programming? Programming, according to Dictionary.com, is “a set of coded
instructions that enables a machine, especially a computer, to perform a desired sequence
of operations.” In other words, programming allows you to make your computer do what-
ever you want it to do. Programming is used in everything related to the computer, but
there are many distinct flavors of programming. This book teaches game programming.
Game programming is very different from the other types of programming. When you
turn on your computer, you often see static programs—programs that sit there and wait
for you to do something. Not with games. Games are running constantly, and they don’t
need the player to do anything to keep it running.
Of course, does this mean that game programming is a bit tougher than other styles of
programming? Yes and no. Although game programming can be difficult at times, this
book strives to turn the difficult into second nature. We use a language of programming
called BASIC, which, as you can probably guess, is pretty darn basic.
What’s in the Book?
This book is meant to be a guide to teach any beginner how to design and develop games.
Inside the book you will find a wealth of knowledge all written in mind to help you reach
the goal of making a game. Each chapter builds on the previous chapters and makes the
book seem like a staircase—you move up step by step. The last chapter helps use all of
your culminated knowledge in the production of a final game.
XI

XII Introduction
Part I discusses the BlitzPlus language. During this time, the book does not discuss graph-
ical programs, but instead uses text-based to get the language points across.
Part II teaches the reader all about graphics within games. There is such information as
varying colors, loading and displaying images, making scrolling backgrounds, and things
like that.
Part III examines other related parts to game programming. The reader can learn how to
use keyboard input, sounds, and music, and take into account artificial intelligence. This
part also creates the final game that the book has been leading up to.
Part IV contains all the appendixes for the book. You will find all of the scan codes (for han-
dling input), a list of Web sites to further your knowledge in the field, and what is on the CD.
Who Are You?
I suppose you can answer this question better than I, but let me tell you what I am look-
ing for in a reader. First of all, I am guessing that you are either a teenager who is inter-
ested in game programming or a parent who is interested in having your child learn game
programming. That is pretty much all that is required: interest. This book is not set up to
be difficult. It helps introduce the reader into the world of game programming.
There are literally very few requirements. All you really need is a basic knowledge of math,
like addition, subtraction, multiplication, and division. If you know those techniques, you
are pretty much set! I also suggest that the reader asks his or her parent for help whenever
necessary. I use some rudimentary algebra, but those instances are few and far between,
and should be easy to comprehend.
You don’t need to have any knowledge of other programming languages. Not that it won’t
help if you do, of course. If you do know any other languages, you can learn from this
book as well. But, Game Programming for Teens, Second Edition teaches the language of
game programming along with the ability to actually implement games.
If you are the parent of a child who wants to learn programming, this book is the way to
go. General programming is a long and boring subject, but game programming allows your
child to create things that are fun. Help your child with programming while he or she reads
this book, as well. Not only will you both learn programming, but who knows, it may
strengthen the bond between parent and child (this comes from my psychologist side).
Who Am I?
Hey everybody, I am Maneesh Sethi. I am a high school student, and the reason I am writ-
ing this book is because I believe that, because I am a teenager myself, I would be the best
one to help other teens learn about programming. I began programming in 1999 in C and

Introduction XIII
C++. Two years ago I discovered BlitzPlus and I have studied it ever since. BlitzPlus seems
to be the easiest way for any novice to begin writing games, and I want to help the reader
progress as fast as possible. The Web site for this book is located on
www.maneeshsethi.com, and you can e-mail me with any questions (before or after you
buy the book) at maneesh@maneeshsethi.com. I love to get e-mails!
Conventions Used in This Book
note
Notes provide additional information on a feature, extend an idea on how to do something, or illus-
trate a cool fact.
caution
Cautions warn you of potential problems and tell you what not to do.
tip
Tips give you some pertinent information on a better, faster, or easier way of doing something.
Let’s Get Ready to Rumble…
If you are still browsing this book in the bookstore, now would be the time to take it
home. The bookstore would probably appreciate it if you buy it first (so would I!).
The first part quickly teaches you all of the intricacies of the BlitzPlus programming
language.
And so we begin…

W elcome to the amazing world of game programming! This book will show you
the ins and outs of video games and teach you to develop your own. Game
programming is a huge topic, however, and we are going to hurry through the
boring material in order to get to the fun stuff. In other words, let's start right away!
The easiest language for learning programming (at least in my opinion) is BASIC.
BASIC stands for Beginner's All Purpose Symbolic Instruction Code, but that's not really
important. BASIC is very easy to write and understand, and it's modeled after human
language (it uses words instead of just numbers), so if you can speak English, you
shouldn’t have a hard time with BASIC.
We will be using a program called BlitzPlus in this book. BlitzPlus is built to use a
modified version of BASIC in its programming. We begin with a short history of BASIC,
just to get the ball rolling on the language.

chapter 1
Getting Started
A Brief History of BASIC
The language of BASIC was first developed in 1964 by J. Kemeny and T. Kurtz at
Dartmouth College. It was designed to be a very easy language to understand, translate, and
write. It was also meant to be the first step toward writing programs for tougher languages.
In the 1970s two people, Paul Allen and Bill Gates, decided to develop a BASIC language
for the new Altair Personal Computer. The developers of the Altair showed a lot of interest
in the BASIC language, and Gates and Allen licensed it.
Bill Gates and Paul Allen put BASIC onto other types of computers. By 1980, BASIC was
moved to Atari, Commodore, and Apple computers as well as the Altair. Bill Gates devel-
oped an operating system called DOS (Disk Operating System) with a BASIC interpreter.
This allowed any user that owned DOS to write programs in BASIC.
Microsoft, headed by Gates, realized how popular BASIC was and decided to write a com-
piler for it that did not require DOS. QuickBasic, the first standalone BASIC compiler, was
born. Soon after, Microsoft decided to focus on graphics, and developed Visual Basic,
which created graphical programs using BASIC as a core language.
BlitzPlus, the program we are using in this book, was developed by Mark Sibly, and is
geared toward the game developer. BlitzPlus is very easy to learn and understand due to
its BASIC nature, and is a good way to learn game programming without having to worry
about extra code that has almost nothing to do with the actual game itself.
3

4 Chapter 1 ■ Getting Started
Installing BlitzPlus
We need to get BlitzPlus onto our computers so that we can start writing games as soon
as possible. BlitzPlus is a compiler, so it takes your code and turns it into a program that
any computer can run. However, the demo version that is included on the CD does not
include the compiler, but only the interpreter. Unlike a compiler, an interpreter does not
create an executable file that can be run on any computer; instead, it only runs from with-
in the compiler. In other words, the programs you write will only be able to be run from
the compiler on your computer. If you want to compile the program into a standalone
executable, you can purchase the full BlitzPlus package from http://www.blitzbasic.com.
In addition, you can download new versions of BlitzPlus from http://www.maneesh-
sethi.com (this book’s Web site). The BlitzPlus installer is shown in Figure 1.1.
Okay, first things first. To install this
program, put the CD into your CD-
ROM drive, and run BlitzPlus-
Demo.exe. BlitzPlus will ask you
where you want to install the pro-
gram. Choose a directory (the default
one is a good choice), and press
install. When the installation finishes,
click OK, launch the program, and
you’re done! You now have a full
BASIC interpreter on your computer.
Figure 1.1
BlitzPlus installer.
note
The BlitzPlus demo has one annoying part—the demo runs only 30 times before it locks up and
requires you to purchase it. For this reason, I have included on the CD a demo for the old version
of BlitzPlus, BlitzBasic. You can install this by choosing BlitzBasicDemo.exe when loading the CD.
The BlitzBasic demo will run most of the BlitzPlus code, although you may need to make a few small
modifications. BlitzBasic has no time limit, however, and will allow you to continue to write code
for as long as you like.
Understanding the IDE
BlitzPlus can seem a little daunting at first. The program has a lot of menus and icons, but
you can master them with a little effort. The first thing you see when you open the pro-
gram is the documentation window, pictured in Figure 1.2. If you need to find tutorials
or sample programs, this is the place to do it. After you have read through anything that
interests you, open a new document, by selecting File>New or the New icon.

A Brief History of BASIC 5
note
The > (arrow) symbol means a selection
from a menu. In other words, File>New
instructs you to open the File menu and
select New. You can access the menus at
the top of the program, right above the
main toolbar.
What you see now, as in Figure 1.3, is
considered the IDE. IDE means
Integrated Development Environment,
and it is an area in which you can write Figure 1.2
and compile your programs in the The Documentation window.
same workspace.
Each of the windows, toolbars, and
menus are necessary for game pro-
gramming, so a good explanation of
each might be helpful.
Figure 1.3
The BlitzPlus IDE.
Windows and Panels
The main window takes up most of the program space and it is the most important part
of BlitzPlus. This window is where the actual code from the game is typed. The keywords
and important parts of your program will be highlighted when you type in this area. If
you want to see an example, type the word End, so that your screen looks like the one
pictured in Figure 1.4. You will notice that as soon as you complete the word and press
the spacebar, it becomes a different color. This highlight feature helps in reading and
understanding your program.