caution
Because of margin constraints, some of the lines of code may have spread over two lines or more.
In a real game, all of the code must be on one line, or else it won’t run. For example, if I had writ-
ten something like the following line
ElseIf ImagesOverlap(ballimage,ball\x,ball\y,player2image,740,player2\y) ;This
tests to see if the ball has collided with player 2's image.
Typing it into the compiler with the line break would not work. It must be on the same line, even
though the margins in the book made it appear broken up.
Figures 1.7 and 1.8 show the KONG title screen and main screen, respectively.
Compiling the Code
Compiling the code is a very simple
procedure. Just open the file (demo01-
01.bb) off the CD in BlitzPlus (or type
it into the workspace), save the file
(File>Save) onto your computer, and
select Program>Run Program, as
shown in Figure 1.9.
Well, that isn’t what you would call a
full game. I did not add any special
effects or sounds because they aren’t
very important at this point. The idea
is to get a feel for what code looks like
and how it is written. You will notice
that the meanings of most of the func-
tions are easy to understand because
of the function names. This helps in
understanding the program.
Let me summarize the main parts of a
game. The game consists of:
■
The initialization section
We have certainly covered a lot of ground in this chapter! So far, we have learned about
the history of BASIC, we have installed BlitzPlus, we have learned the important features
of the program, and we have written, read, and played our first game. One important
thing: Do not be disheartened by the length or complexity of the sample code. This game
is not a tough one, and although it seems long now, it will be relatively simple to write by
the time you finish this book.
In this chapter, we went over the following concepts:
■
The history of BASIC
■
Installing the BlitzPlus program
■
Creating our first game
■
Compiling our first game
The next chapter will introduce you to the fundamentals of BASIC; it will discuss common
operators and operations. If you’ve made it this far, the next chapter should be a cinch.
Just sit back, relax, and enjoy the ride.
Chapter 1
■
Getting Started18
The Day that Maneesh Got
Embarrassed
In March of 2004, I was on a show called “Call for
Help” on TechTV. I decided to demonstrate this
game, KONG, on the show, because it was an easy
to understand and play game. Turns out I made a
bad choice. During the game, some of the random-
ization code got messed up, so the ball bounced up
and down and up and down repeatedly. My game
you are typing is blank first. Only the code should be displayed in the main window of the
BlitzPlus compiler.
If you don’t want to compile the code, you can also run this program from the CD. Figure
2.2 shows the executed Hello World program.
;demo02-01.bb - Displays text "Hello World"
Print "Hello, World!"
;Wait for five seconds
Delay 5000
Although this program may seem very simple, it is a big hurdle you have just crossed. You
just created a file, typed in the code, compiled it, and ran it as a program. Congratulations!
Let’s analyze this program a bit (although there isn’t much to analyze). First of all, the line
;demo02-01.bb - Displays text "Hello, World!"
is a comment. A comment is any text that is written after a semicolon (;). The comment
ends at the end of the line. A comment does not have to occupy its own line; it can be writ-
ten after some actual program code. For example, this line
Print "This is code" ;This is a comment.
Chapter 2
■
Getting to Know BASIC20
Figure 2.1 The Hello World program in BlitzPlus.
consists of two parts: a line of code and a comment. Comments are used to help you
understand the code; the compiler does not understand or care about information in
comments. The compiler automatically ignores any comments. Figure 2.3 demonstrates
how comments look inside a compiler.
Hello, World! 21
Figure 2.2 The executed Hello World program.
Figure 2.3 Comments in a compiler.
tip
You might be wondering, “If it is my code, why would I need a comment to understand it? I wrote
it, so I understand it!” The problem with this assumption is twofold: one, you may decide to share
End
somewhere in the source file.
I usually like to provide the function declaration for easy reference when calling functions.
A function declaration describes any parameters taken in by the function as well as the
function name. The function declaration for
Print
is:
Print [string$]
note
Notice the square brackets ([]) on the left and right of the [
string$
] variable. These brackets mean
that the variable is optional and not required. If the variable is required but omitted, you will receive
an error and not be able to compile your code.
As you can see, the function’s name is
Print
and the only parameter is [
string$
]. A string
is just a series of characters put together; you can think of a sentence as a string. The string
would be the entire sentence lined up together, including the spaces and punctuation.
Chapter 2
■
Getting to Know BASIC22
First of all,
Print
is a function. Functions (which are described in more detail later) come
in two flavors: user-defined and compiler-defined. User-defined functions are written by
the programmer (
TestKeyboard()
string$
A text string followed by a new line that will be displayed onscreen. If
string$
is omitted, only a new line will be printed.