xvi Introduction
1.1 Draw a triangle onscreen. e triangle will be our spaceship.
1.2 Position the triangle at the bottom of the screen.
1.3 Position the triangle slightly to the right of where it was before.
1.4 Animate the triangle so that it moves from position left to right.
1.5 Animate the triangle from left to right only when the right-arrow key is pressed.
1.6 Animate the triangle right to left when the left-arrow key is pressed.
Of course, this is only a small fraction of all of the steps we need for a full Space Invaders game, but
it demonstrates a vital way of thinking. e benefi ts of this approach are not simply that it makes
programming easier (which it does), but that it also makes “ debugging ” easier.
Debugging
1
refers to the process of fi nding defects in a computer program and fi xing them so that the
program behaves properly. You have probably heard about bugs in, say, the Windows operating system:
miniscule, arcane errors deep in the code. For us, a bug is a much simpler concept: a mistake. Each time
you try to program something, it is very likely that something will not work as you expected, if at all. So
if you start out trying to program everything all at once, it will be very hard to fi nd these bugs. e one-
step-at-a-time methodology, however, allows you to tackle these mistakes one at a time, squishing the
bugs.
In addition, incremental development lends itself really well to object-oriented programming , a core
principle of this book. Objects, which will be introduced in Lesson 3, Chapter 8, will help us to develop
projects in modular pieces as well as provide an excellent means for organizing (and sharing) code.
Reusability will also be key. For example, if you have programmed a spaceship for Space Invaders and
want to start working on asteroids, you can grab the parts you need (i.e., the moving spaceship code), and
develop the new pieces around them.
Algorithms
When all is said and done, computer programming is all about writing algorithms . An algorithm is
a sequential list of instructions that solves a particular problem. And the philosophy of incremental
development (which is essentially an algorithm for you, the human being, to follow) is designed to make
it easier for you to write an algorithm that implements your idea.
As an exercise, before you get to Chapter 1, try writing an algorithm for something you do on a daily
basis, such as brushing your teeth. Make sure the instructions seem comically simple (as in “ Move the
toothbrush one centimeter to the left ” ).
Imagine that you had to provide instructions on how to accomplish this task to someone entirely
unfamiliar with toothbrushes, toothpaste, and teeth. at is how it is to write a program. A computer is
nothing more than a machine that is brilliant at following precise instructions, but knows nothing about
the world at large. And this is where we begin our journey, our story, our new life as a programmer. We
begin with learning how to talk to our friend, the computer.
1
e term “ debugging ” comes from the apocryphal story of a moth getting stuck in the relay circuits of one of computer scientist
Grace Murray Hopper’s computers.