Chapter 2: Introduction to Computer Programming
Introduction to Computer Programming This chapter is an
introductory course in computer programming, with no prior programming
experience required. The first thing aspiring programmers often discover is that
programming looks really complicated. The truth is, computer programs are made
up of hundreds of different commands arranged in a specific order to accomplish
a task. The program doesn't always have to be a game. In the context of
DarkBASIC, most of the programs you will write using this book will be
game-related. However, there is a lot of information to cover before you'll be
ready to tackle your first complete game! This chapter explains not only how to
get started writing source code in DarkBASIC, but how programs work.
Short Course on
ProgrammingWhat's the best way to learn to write computer programs? You
have probably wondered in amazement while playing the latest game, "How in the
world did they
do that?" I hope that by the time you've
finished reading this book, you'll have a solid grasp on the answer! What I can
tell you at this point is that it's a gradual process. First you learn the
basics, and then you practice while learning more techniques. With some
diligence you will learn how the pros work their magic. Eventually, you will
gain the experience you need to become an expert programmer, but you must invest
a lot of time in studying and practicing your programming skills, learning new
languages and tricks along the way. As with most things in life, it just boils
down to a lot of work—not necessarily hard work, but consistent effort. For that
reason, it helps if you really enjoy programming in general—you must
love to play and write games to get really good at it. If you
are not fascinated by something, how will you muster the will to keep at it?
What Is a
Computer Program?First of all, you might
be asking this simple question: What exactly is a computer program? I have
already hinted at the answer, but in a nutshell, a computer program is something
that accepts input (such as an entry from the keyboard), performs some
computations (also called processing), and then sends the result to the output
(usually the screen)
There are many different ways to write a computer program, and I'm
not just talking about the programming language. You can use any language to
write a computer program, although some languages are more suited for certain
types of problems than others. When computer programs start to get really big,
they tend to become unmanageable unless the programmer is well organized. For
this reason, computer scientists came up with a fancy term called
methodology. This word refers to the steps you can take to
describe how a program is written, based on the way a programming language
works. For instance, suppose you are writing a massivelymultiplayer game that
will be able to handle thousands of players online at the same time, interacting
in a huge game world. The methodology for this game would describe how the
computers are connected online in order for the game to work. One methodology
might attempt to break up the game world into many smaller worlds linked
together, while another methodology might describe a single huge game world.
Object-Oriented ProgramsYou might have heard of object-oriented programming (OOP)
because it is used to write large and complex programs. Object-oriented programs
are easy to handle when they get large because they are made up of many smaller
related programs.
Therefore, any time you
need to solve a particular kind of problem in your program, you just plug in one
of the smaller programs. As you might imagine, OOP excels at handling very large
programs involving millions of lines of code, but it's more difficult to learn
than other types of languages.
Structured ProgramsStructured programming languages are easier to learn than
OOP languages, which is one of the reasons why this book uses DarkBASIC, a
powerful structured programming language. Structured programs are also called
procedural programs because they are made up of multiple
procedures, each of which is capable of solving a small problem. If this sounds
similar to object-oriented programs, it's because OOP evolved from structured
programming.
Structured programs tend to run faster than OOP programs. Although
they are more difficult to manage when they get large, structured programs
generally are easier to learn and use. Unlike OOP programs, structured programs
don't require that you design an object before you start writing the actual
program.
shows an illustration of structured programming.
Figure 2.2: A visual
representation of a structured program
DarkBASIC Is a Structured LanguageDarkBASIC programs are
completely self-contained executables that don't require any special run-time
library because the library is built into the compiled program. (If you don't
understand what I'm talking about, don't worry. I'll go over this information
again in the next few chapters.)
shows what the internal structure of a
DarkBASIC program might look like in theory.
DarkBASIC allows you to compile your program to a standard Windows
executable, which means that it runs like any other game you've played on your
Windows PC, complete with support for DirectX. In fact, the greatest feature of
DarkBASIC is that it lets you write DirectX games without even knowing any of
the DirectX function calls. It's completely built in!
Figure 2.4: DarkBASIC
programs are closely tied to Windows and DirectX.
Depending on your experience level, this might make sense or
it might be something that you will pick up in time. A compiler is a program
(like DarkBASIC) that converts your source code into an executable file. Like so
many subjects involved in writing computer programs, this is one that I must
defer until later in order to keep the subject matter more understandable and
less rife with theory.
Definition of a Computer ProgramIn a more technical
sense, a computer program is a series of instructions that tells the computer
what to do, usually in the context of solving a problem. A Web browser or word
processing program is just a computer program made up of instructions—just like
a fast-action first-person shooter game such as
Doom III or
Unreal Tournament 2003. It's all about the
instructions—or, as we say in gaming circles, the code.
What is code? You might have heard the word used before, but
without a clear frame of reference it can be somewhat confusing.
Code refers to the source code instructions that make up a
program. The instructions themselves are each designed to perform simple
operations such as math, logic, or memory movement. These very low-level
instructions tell the computer what to do. Of course, it's very difficult for
humans to read and write these instructions in the computer's native format,
which is referred to as
machine language. This format is
completely binary— that is, it consists of streams of 0s and 1s. These values
are referred to as
bits. A single bit can hold one piece
of digital information.
Video Game BitsYou might have heard the
word
bit used in the context of video games. Console
makers, such as Sega, Nintendo, Sony, and Microsoft, love to talk about video
games in terms of the bit strength of their consoles. For example, the Dreamcast
was marketed as the very first 128-bit home video game machine. Where did the
term "128-bit" come from? Generally, the number of bits that a video game system
can handle is related to the computational power of the GPU (
Graphics Processing Unit), which is different from the CPU
(
Central Processing Unit). This is really an interesting
subject. shows you the specifications of the major video game machines on the
market today.
These figures seem to be much lower than those found on even a
budget PC. Why do you suppose that a wide variety of console games seem to look
and play so much better than PC games? Part of the reason is that console games
are written very specifically for the hardware of the video game machine, while
PC games have to support a wide variety of hardware configurations. Usually a
high-end PC will totally blow away a console
machine, but consoles generally attract a younger audience due to the lower
price.
Since I'm on the subject of video games, what does it take to
write a game? DarkBASIC excels at helping you write games, thanks to all of the
wonderful builtin commands that were tailored just for this purpose. I'll cover
this again in later chapters, but a game is basically a program that runs in a
loop, which means that it keeps doing something over and over again until you
tell it to end. It's this looping feature that keeps the game running smoothly.
On the contrary, a turn-based game doesn't use a game loop like this unless the
game uses animation that needs to be updated on a regular basis. When it comes
to larger games that include animated graphics, artificial intelligence, sound
effects, music, multiplayer support, and user input, the game loop can be quite
complicated indeed! On top of it all, the game needs to run as fast as
possible—even with 3D graphics in most cases! Check out
for an illustration of the
game loop.
Solving
ProblemsSince computer programs
are supposed to be written to solve a problem, what kinds of problems might need
to be solved? The possibilities are innumerable. For example, there are
computers and programs on airline jets that help the pilots take off, navigate,
and land. Computer programs operate streetlights. There are even programs
running in modern cars that help to achieve better gas mileage. We are
surrounded by problems, in fact! The process for solving a problem with a
computer is similar to the processes humans use to solve real-life problems.
Every decision you make involves one or more pieces of input that you must weigh
in order to make a decision. The result of your decision is the output of your
"program."
shows a simple illustration of a decision the way a computer sees it.
You see, computers treat every decision as a single entity. Even when there are
many details that must be considered before making a decision, the computer must
think about every decision separately. Humans, on the other hand, have the
ability to quickly draw conclusions, sometimes without looking at every factor
involved.
Figure 2.7: Computer
problemsolving involves making decisions using simple logic tests.
Decisions, DecisionsSuppose you are
purchasing a new video game console at the store. You want to buy a console with
a large selection of games that appeal to you, but you also want it to last a
long time before it becomes obsolete. Therefore, the game selection alone
probably isn't good a single source of input for your decision. You intuitively
weigh factors such as price, manufacturer, online multiplayer capabilities, and
previous purchase decisions that went well or poorly before choosing a
console.
Computer programs work the same way. A program is more limited
than your mind, though, because programs have only a limited intelligence for
adapting to new situations, while you are capable of adapting to any unforeseen
condition. Computers excel at doing things precisely and quickly, but humans
have intuition—we are able to see patterns and shapes that aren't apparent at
the lowest detail, which is the narrow level that computers "see." Therefore,
computers don't get the big picture like humans do.
To help with intuition, our minds like to categorize things
as closely as possible, so we are able to recognize patterns quickly. As an
infant, one of the first patterns that your mind memorizes is the face of your
mother or father (usually both). Some of your mind's first problems involve
seeing other faces and trying to decide whether they are Mom or Dad. This is a
pattern-recognition problem that required a great deal of training and
reinforcement—looking at familiar faces over and over again caused those
patterns to be remembered and used for comparison. So, when you think about it,
our minds are already programmed when we are born; they just need input. That is
very similar to how a computer program works. First you write the program,
putting as much or as little problem-solving ability into it as you choose, and
then you send it out into the world. Of course, another thing humans have over
computers is the built-in ability to learn.