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 Programming

What'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). Figure 2.1 illustrates this point.

click to expand
Figure 2.1: The basis for computer programming is input, process, and output.

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 Programs

You 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 Programs

Structured 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. Figure 2.2 shows an illustration of structured programming.

click to expand
Figure 2.2: A visual representation of a structured program

DarkBASIC Is a Structured Language

DarkBASIC 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.) Figure 2.3 shows what the internal structure of a DarkBASIC program might look like in theory.

click to expand
Figure 2.3: DarkBASIC executables include both your compiled code and the run-time library.

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 shows the relationship between DarkBASIC, Windows, and DirectX.

click to expand
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 Program

In 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. Figure 2.5 shows a game project made up of several parts.

click to expand
Figure 2.5: Computer programs are made up of separate parts, each of which accomplishes a task.

Video Game Bits

You 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. Table 2.1 shows you the specifications of the major video game machines on the market today.

Table 2.1: Video Game Console Comparison Chart





Microsoft Xbox

733 MHz

256-bit, 233 MHz

64 MB

Nintendo GameCube

485 MHz

128-bit, 162 MHz

40 MB

Sony PlayStation 2

295 MHz

128-bit, 150 MHz

40 MB

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 Figure 2.6 for an illustration of the game loop.

click to expand
Figure 2.6: The game loop keeps the game running smoothly.

Solving Problems

Since 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."

Figure 2.7 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.

click to expand
Figure 2.7: Computer problemsolving involves making decisions using simple logic tests.

Decisions, Decisions

Suppose 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.

Logic versus Intuition

Have you ever discovered something new and immediately found it difficult to compare that new thing to anything else you have seen in your life? Part of the enjoyment of discovering new things is the mystique of trying to categorize them. We leap upon solutions so quickly that it is fun to find something indescribable— at least at first. This is precisely why children have such a need to play, and why their lives are filled with such mystery, intrigue, and suspense. Children never know when something new will present itself to them. Adults are often not as fascinated by daily life, after having "seen it all." If you think about that observation for a minute and note how it applies to your life, doesn't it start to make sense? The wonder of childhood is very closely tied to our built-in ability to categorize things in the world. When a baby sees a new person's face, the baby might frown, look puzzled, or even cry, unhappy with the new situation. Often the baby is very quickly able to perform the "parent/not parent" test by comparing new faces with the most important faces in his or her mind. The infant's mind is an extraordinarily complex neural network. (A neural network is a pattern-recognition machine. Everything in the human mind is stored as a pattern, and our ability to organize and store patterns efficiently determines our intelligence.)

Do you suppose your brain really stores every letter of the alphabet and then searches that list of 26 letters from start to finish when you read the words in this book? Again, I ask that you take a moment to think about this, because it is more relevant to computer programming than you might imagine. No, you are reading these words and your mind is recognizing entire words as patterns, not just letters. Your mind will often misread words that have similar spellings (such as "grind" and "blind") because a neural network seeks out solutions as quickly as possible, not always with perfect accuracy. That is intuition in a nutshell.

One reason why programmers are so fascinated with writing code is because it puts your mind through a constant workout as you try to reprogram your mind to stop making assumptions about things. You must put a conscious halt to some patterns and try to think like the computer thinks (in the manner in which it processes data). The ability to quickly surmise the solution to a problem and write a program to solve it is what makes a good programmer, and it does take practice. As you write more and more code and solve one problem after another, you will start to get the hang of it.

Methodology of the Mind

I will elaborate on this more in the next section, but I wanted to give you more insight on writing computer programs. Over the years, I have learned that real programmers never get tired of writing code. No matter how boring the subject, a great programmer will have an inherent fascination with the "intuition bypass" that I described in the previous section. As you learn new techniques as a programmer, you will find that many solutions simply involve challenging your assumptions about what the computer will do in a given situation. As you have lived your daily life, your mind has started to develop more advanced ways to store patterns. This is what you might call the "methodology of the mind," developed with experience. Not only is the human mind able to store and organize patterns efficiently, it is also able to completely reprogram the method by which it does these things— something that computer scientists might never be able to build into a computer.

I have mentioned categories already. A category is a sort of bin in which your mind places like patterns; there can be a hierarchy of these storage bins of the mind. For instance, you might lump shells, seaweed, and sand together in a category called "the beach." Since seaweed comes from the ocean, your mind doesn't need to add water to the category—it is just an assumption that water goes along with these other beach-related items.

Now what if you have never seen a beach in your life, or even a picture of the ocean? It is hard to imagine, like trying to imagine what it would be like to have been blind from birth. Without ever having used your eyes, how would you imagine the many colors in the world? You might imagine shades of color that resemble different scents or textures or temperatures, but you'd never have any idea what different colors really look like.

As a programmer, you must learn to think outside of your assumptions to solve the most difficult problems in the best way. Not only will you become a better programmer, but you will be able to apply this skill to other areas of your life. After writing code for many years, I find that I am able to see things differently than others. Often, this is what causes non-programmers to think of programmers as a little strange. While most people will look at a sports car and admire the paint and body style, a typical programmer will be interested in such things as the engine displacement, fuel economy, and gear ratio.

Three Steps to Solving Logical Problems

There are three basic steps to solving a problem—input, calculations, and output. Consider a sample problem in which you are deciding which product to purchase at a store.


Figure 2.8 shows three inputs for a decision that must be made. This represents the actual logic of a program (which could be the human brain or a computer, as the case may be). This problem involves selecting a product. The square labeled "Decision: Select a Product" could be a program or a thought process that uses the incoming information.

click to expand
Figure 2.8: A problem that needs to be solved

Take a look at the sample problem to find the input, which is nothing more than the available data or facts. In the sample problem, three products represent the input: Product A, Product B, and Product C. Each of these inputs might be stored in the program using a variable— a piece of data that the program can use.

A variable is a location in the computer's memory that stores information that can be changed at any time.


The next step to solving a problem is to do something with the input. This step is called the calculation or processing step; it usually involves some type of algorithm designed to solve a specific problem. Figure 2.9 shows how you might select a product using specific criteria (also known as logic). This is really where all the action takes place, because the processing of the program occurs here. The program might run through many complex algorithms or just a simple equation.

click to expand
Figure 2.9: Several conditions must be met before this decision returns true.

An algorithm is a step-by-step process for solving a problem.

Figure 2.9 shows a chain of test conditions involved in processing a decision. The decision is made only if all three conditions are true. Of course, in real life there might be any number of conditions, from just one to a hundred or even more. Naturally, computers are more efficient than humans when a decision involves a very large number of conditions.


The output of a decision (or an entire program) is usually the most obvious part, where you clearly see something on the screen or printer or perhaps receive the answer to a decision. The output from one process is often also the input for another process. There are many different methods to map out a program. Thinking in the simple terms of input, process, and output will help you understand how programs work.

Video Game Basics

Well, I have covered many different aspects of computer programming without getting too technical. As you might have guessed from the title, this book is not just about writing computer programs; it is about writing games.

A computer game or video game is a program, written with a programming language such as C++ or DarkBASIC, which allows a player to interact within an imaginary world. The goal of a video game is to compete with other players in a cooperative or adversarial competition or to play solo, usually with an emphasis on setting a new high score (which other players will attempt to beat later).

Why Do We Play Video Games?

Video games are fun because they are very good at simulating another world and then drawing the player into that world. They are excellent for developing keen hand-eye coordination and problem-solving skills through the achievement of goals with specific rewards. Often the completion of the game is reward enough, but many games do not have a specific ending point. Rather, some games are suited for pitting two or more friends against each other, either at the same time or in turns.

Competition helps to develop the skills you need to work with others and strive to better yourself, and video games are great at fostering competition. In addition, they are wonderful works of art that reflect the often arcane imaginings of the game designer. As such, game designers are like painters with virtual canvasses, capable of transporting someone to another world. Once a game has achieved a minimum acceptable standard on the visual and audible levels, players expect it to entertain through a gripping story or fantastic goal.

The Game Engine

Every game, from a simplistic card game like Solitaire to a complicated 3D first-person shooter like Doom III, has what is called a game engine. The game engine is the core set of commands that are executed repeatedly in a main game loop, which involves displaying images on the screen, playing sound effects, handling user input, and so on. Tall order, huh? Well, a game engine can be broken down into a few basic parts.

A sprite is a small image that may or may not be animated, with properties that define how it moves around on the screen. The source image for a sprite usually has a background color that is defined as the transparent color and is not displayed when the sprite is drawn on the screen. Engineers at Atari originally coined the word "sprite."

Game Graphics

In the old days, all games used two-dimensional images called sprites. It is a given that most games developed today will run entirely in 3D and feature three-dimensional objects throughout the game world rather than simply sprites. Not all games display 3D objects, but most games use a 3D world. DarkBASIC makes 3D programming easy by providing all the commands you need to load and draw 3D characters and worlds, as you'll see in later chapters!

Sound Effects and Music

Sound is quite often more important than the graphics in a game. It is clear that humans interact primarily through speech and sound. It should therefore be no surprise that sound is a vital part of any new game. In fact, now that games use 3D sound, it's no longer a great thing to simply have sound and music in a game. 3D sound provides the often eerie effect of being fully immersed in an environment filled with sounds from every direction.

Getting Input

User input is always needed, no matter what type of game you are writing. After all, a game without user input is nothing more than a technology demo! DarkBASIC is capable of handling any number of input devices that you can plug into your PC, including the newer USB (Universal Serial Bus) force-feedback joysticks. DarkBASIC handles the devices through a series of simple but robust commands, as you'll discover in Chapter 12, "Programming the Keyboard, Mouse, and Joystick."

Artificial Intelligence

Computer-controlled players are necessary for most types of games, and are absolutely essential for single-player games. Some multiplayer games, such as firstperson shooters, do not have computer-controlled players and thus do not need AI (Artificial Intelligence) code. However, the vast majority of games—especially strategy games—do, so it is important to learn the tricks and techniques for simulating intelligence and challenging the player with competitive computer players.

Writing Your First DarkBASIC Program

Now that I've covered some basics of programming and an overview of video games, I'd like to give you some experience playing around with DarkBASIC. You should now have a basic grasp of what makes a computer program tick—even if you don't have a clue exactly how to put it to use yet. In other words, you have some theory but nothing useful yet! In this chapter and those that follow, you will apply the input-process-output model to game programming. I'll start with some basic concepts that will be useful in every DarkBASIC programming project, regardless of the subject.

Running DarkBASIC for the First Time

First, fire up DarkBASIC. If you haven't installed it yet, go ahead and do that now; I'll wait.

Ready to continue? Great! The first time you run DarkBASIC, you will see a screen that looks like Figure 2.10. If you followed along in the last chapter as I gave you a tour of DarkBASIC, you might have already seen this screen when DarkBASIC started. This check is only performed the first time you run DarkBASIC.

click to expand
Figure 2.10: DarkBASIC detects the capabilities of your 3D card the first time it runs.

DarkBASIC performs this check to ensure that it will run on your PC. First, your video card must support 3D at a resolution of 640480. The other three features (alpha blending, texture filtering, and fogging) are advanced features that DarkBASIC can use, but which are not required for it to run. To continue, just press any key.

The Introduction screen appears next, as shown in Figure 2.11. You saw this screen in the last chapter.

click to expand
Figure 2.11: The Introduction screen is loaded when you first run DarkBASIC, providing you with immediate help on how to get started.

The Hello World Program

A program is made up of source code, which is the instruction manual that tells the computer what to do. When programming in DarkBASIC, you will type in source code, which is any text that instructs the computer what to do. Figure 2.12 shows an example of source code in the DarkBASIC editor.

click to expand
Figure 2.12: Sample source code in the DarkBASIC environment

Entering the "Hello World" Source Code

One of the hardest parts of writing a computer game is typing in the source code. You have to keep everything in mind, from what commands you want to use to what variables you want to create. Most programming languages are case sensitive, which means that you not only have to spell everything correctly, but also verify the capitalization of each letter. Fortunately DarkBASIC is not case sensitive, so you can use whatever capitalization you prefer. If it is easier for you to type in the source code using all lowercase letters, DarkBASIC won't mind. For example, the print command will work equally well as print, PRINT, or pRiNt.

Go ahead—type in the program yourself and see how it runs. Type the following lines of source code into the DarkBASIC editor exactly as shown in Figure 2.12.

Before you do anything else, however, you need to fire up DarkBASIC (or DarkBASIC Professional, depending on which version you are using). In most cases, DarkBASIC is located in the Windows Start menu under Program Files, Dark Basic Software. If you can't find DarkBASIC in the Start menu, you might want to make sure it was installed properly. The easiest way to tell is to run the install program and reinstall DarkBASIC from the CD-ROM.

Installing DarkBASIC

DarkBASIC is easy to install, but the installer differs depending on the version. Whether you are using DarkBASIC or DarkBASIC Professional, the installation process requires very little input and only takes a minute or two to complete. The CD-ROM that comes with this book includes the demo versions of both DarkBASIC and DarkBASIC Professional. The demo version of DarkBASIC is located in a folder called DBV1 Install. Likewise, the demo version of DarkBASIC Professional is located in a folder called DBPro Install. These demo versions will allow you to try DarkBASIC and read through the book before you purchase the retail version. In either case, Dark Basic Software Ltd has provided the latest version of each compiler at the time of this writing. For more information, browse to the official DarkBASIC Web sites at and

REM ---------------------------------
REM Beginner's Guide To Game Programming With DarkBASIC
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 2 Hello World program
REM ---------------------------------

REM Display first message
PRINT "Hello, world!."

REM Pause for one second...
SLEEP 1000

REM Display second message
PRINT "Wait, what was that?"

REM Pause for 1/10 second...

REM Display third message
PRINT "What, you want more?"

REM Pause for 1/10 second...

REM Ask user to type in a number
PRINT "Type in a number..."
REM Display the message several times
FOR x = 1 to a
 PRINT "Hello, Again!"

This program includes several things that I have not introduced you to yet, but a little exposure to what is to come will just give you a sneak peek at coming chapters. You should be able to type this program into the DarkBASIC or DarkBASIC Professional code editor window and run the program by pressing F5. If you receive an error message, it is most likely due to a typing error. Simply check each line of the program and compare it with the printed program listing. Note that the comment lines at the top of the program are optional—you can leave out REM lines without hurting the program.

Quick Keyboard Shortcuts

Sometimes the mouse just isn't convenient enough. Your fingers are just finishing up a long stream of code, and the little effort it takes to move the mouse up to the Run menu is just too much. Yes, I'm speaking from experience here. DarkBASIC has that situation covered as well.

When you clicked on the Run menu, did you notice that each option has a function key listed next to it? The Compile option has F4, and the Run option has F5. This tells you that if you hit the F4 key while in DarkBASIC, the program will compile. If you hit the F5 key, the program will run.

Saving the Program

Always be sure to save a program you've typed in before running it. Programming is a scientific endeavor replete with mistakes, which will occasionally cause a compiler to crash—although DarkBASIC is particularly user friendly and has never crashed in my experience. Another reason to be sure you save your work is that you never know when the power will go out after a long programming session. I could tell you many different stories of people who did not save and lost a lot of work. (I won't mention my name in any of those stories.)

To save the program, choose File, Save, as shown in Figure 2.13. If this is the first time you have saved the project, the Save a Program dialog box will appear, allowing you to select the file to save. This dialog box also allows you to type in the name of the program, the author, and the date (see Figure 2.14).

click to expand
Figure 2.13: Saving the "Hello World" program using the File menu

click to expand
Figure 2.14: The Save a Program dialog box lets you save a new program.

Now comes an important step—naming the project. This can be easy if you follow a few guidelines. First, know what you are writing, and second, describe it in your project name. Once you have come up with a project name you like, just type it in the Filename field. DarkBASIC will take care of the rest, including creating the files and directories for you.

Compiling the Program

You will need to check your source code for errors before running it. DarkBASIC will not run the program if you have entered illegal commands. You can attempt to run it, but it will not work if there is anything wrong. There is an easy way to compile and run your program—simply press the F5 key at any time and DarkBASIC will run it (that is, if there are no errors).

DarkBASIC is a fun programming environment to use, as you have probably noticed. It just seems like everything was designed for writing games, and it's very immersive. DarkBASIC isn't overloaded with an unbelievable amount of features that completely overwhelm anyone but an expert (like Visual C++).

Now that you've saved the program, compile it to make sure it doesn't contain any typos. To compile the program, select Run, Compile (see Figure 2.15).

click to expand
Figure 2.15: You will find the Compile and Execute commands in the Run menu.

When you compile your source code, DarkBASIC turns it into something that can be run on the computer.

Small programs such as this "Hello World" program compile in less than a second on most PCs, but larger programs can take a long time to compile. Figure 2.16 shows the compilation progress bar.

click to expand
Figure 2.16: The compilation progress bar shows the progress while a program is being compiled.

Running the Program

Of course, you don't have to compile the program. You can just press F5, and DarkBASIC will automatically compile the program before running it. Figure 2.17 shows the output from the "Hello World" program.

click to expand
Figure 2.17: Output from the "Hello World" program

Program Comments

Some of the most important source code in a program consists of lines that are never executed, called comments. Adding comments to source code is helpful in two ways. First, it tells you what you were thinking when you wrote that particular section of code. Second, it helps you remove code that you do not want run but that you want to save for later. They're also useful for including blank lines in the source code to separate sections of code and make it easier to read. DarkBASIC ignores blank lines, just like comment lines.

Making comments in DarkBASIC is very easy. There are three different commands (and a character) related to comments.

  • REM
  • ‘ (single opening quote)

The single opening quote character, which looks like a reverse apostrophe, is the key normally associated with the ∼ (tilde), and is found to the immediate left of the 1 key and above Tab on most keyboards. It is the most convenient way to add comments to your source code. After a few examples of using the other comment commands in upcoming chapters, I will use the quote character more often. Just wait; you'll see!

The REM Command

The REM command tells DarkBASIC that the line is a comment. The REM command must come first in the line, followed by the comment.

REM This is a comment that doesn't do anything.
REM Comment lines are ignored by DarkBASIC.

The REM command is short for remark, and it allows you to include comments that are ignored by DarkBASIC in a program.


Often the REM command is just not enough to handle many lines in a lengthy comment. For example, you might want to add a header to your source code to give information about the program. You can include as many lines as you want in this header by defining those lines as comments. Adding REM to each line is easy to do, but there is a more elegant solution—the REMSTART and REMEND commands.

REMSTART indicates that a comment starts at that line. Anything following a REMSTART is considered a comment and is ignored by DarkBASIC until it reaches a REMEND command. Here is an example of the REMSTART and REMEND commands:

Program: Test.bas
Author: Joshua Smith
Date: April 3, 2002
Description: Demonstrates the use of comments.

Starting and Stopping the Program

Now let's talk about program flow. DarkBASIC executes the source code from the top to the bottom, executing each line of the program one at a time. That means the first command in the source code file is the first command that DarkBASIC executes. Since DarkBASIC executes code in this manner, you can use an END command to end the program from any point. Otherwise, the program will simply end after the last line of the program executes.

The END Command

The END command ends the execution of your program wherever you put it. It can be placed in multiple locations. The following program shows how to use the END command.

Program: FunWithComments.bas
Author: Joshua Smith
Date: April 3, 2002
Description: Demonstrates the use of comments.

REM End the program!

This program might not do much, but it will end (little joke there). The END command is key in controlling the flow of your program, as you will discover in later chapters.

Why do you need an END command? Why not just place the last line of source code at the end of the file? If you want to stop your program from running, you need to use the END command. Nothing after the END command will run.

The Command Line Interface

The command line interface (CLI), which is built into DarkBASIC, allows you to debug a program while you are running it. The console is like a real-time DarkBASIC file that runs every command after you type it. You can print variables or ask for input. Anything that can be typed in source code can be typed in the CLI (see Figure 2.18).

click to expand
Figure 2.18: The command line interface

To debug a program is to remove all of the bugs from it. Sometimes it takes just as long to debug a program as it does to write it!

To gain access to the command line interface window while a program is running in the DarkBASIC IDE (Integrated Development Environment), just press the Esc key. The command line interface will pop up, interrupting your program. At that point you can enter whatever commands you want. To return to your program, just hit the Esc key again. I know this was a brief introduction to the CLI; it will be covered again in more detail in a later chapter. Note that DarkBASIC Professional operates differently and does not bring up the CLI. Instead, when you hit Esc, by default the program ends and control returns to the editor. DarkBASIC Professional has integrated the CLI into the debugging features of the new editor.


This chapter covered a lot of basics in a short amount of time, including how to create a new project, how to type in source code, and how to run your first DarkBASIC program. Congratulations, that's a great start! If you feel the need, go ahead and reread the chapter to strengthen your understanding of the material and become more familiar with the DarkBASIC editor and programming environment before moving on to the next chapter. But first, give the chapter quiz a try and see how well you do.


The chapter quiz will help you to retain the information that was covered in this chapter, as well as give you an idea about how well you're doing at understanding the subjects. You can find the answers for this quiz in Appendix A, "Answers to the Chapter Quizzes."


Programming is the art of solving what?

  1. Crossword puzzles
  2. Problems
  3. Word searches
  4. Pizza toppings



Which is not part of the method for solving problems?

  1. Input
  2. Output
  3. Processing
  4. Pass on



Which command is used to make a comment?

  2. REM
  4. COMIT



What command is used to start a series of comments?




The END command will restart the program.

  1. True
  2. False



What is a good tool for debugging your program?

  1. Console
  2. REM statement
  3. END statement
  4. Mouse cursor



Why is it important to comment?

  1. It lets you know what you were thinking when you wrote the code.
  2. It's not important.
  3. To fill space.
  4. B and C



What is an algorithm?

  1. A musical term
  2. A step-by-step process used to solve a problem
  3. A graphics card
  4. A brand of cereal



What command do you use to end a program?

  1. STOP
  2. END
  3. HALT



What does REMEND do?

  1. Ends a series of comments
  2. Nothing
  3. Starts a series of comments
  4. Prints to the screen























Beginner's Guide to DarkBASIC Game Programming
Beginners Guide to DarkBASIC Game Programming (Premier Press Game Development)
ISBN: 1592000096
EAN: 2147483647
Year: 2002
Pages: 203 © 2008-2020.
If you may any questions please contact us: