Tutorial: How to write games

Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>

Variables

So now we can calculate mathematical equations and print the results on the screen.
Useful... But we are still some distance away from writing a parallax side scroller! Don't worry, we will get there! But first we need to introduce a few more concepts.

We will start with variables.

A variable is somewhere that you can store some data.
In Basic4GL, this data can be one of the following:

Almost all programs use variables. Even games use them to keep track of all sorts of things. They might store how many aliens there currently are, or how many bullets the player has, or what the score is, or what the players names are on the high score chart, or how long since each one fired a bullet, or any number of things.

So let's have an example.
Clear out the old program (save it first if you want to), and type in the following.

dim a, b
a = 5
b = 3
printr "a stores " + a
printr "b stores " + b

Then run the program (with the Go button, as usual).

It should tell us that a is storing 5, and b is storing 3.

a and b are variables.
We told the computer that we wanted to create two variables with line at the top:
dim a, b
dim is the command that creates variables. After it we list the names that we want to give to those variables, with commas in between (if there are more than one). In this case, we have two variables called a and b.

Next we tell the computer to assign values to them with:

a = 5
b = 3

This tells the computer to store the value 5 in the variable named a, and the value 3 in the variable named b.
Finally we print the results to screen:

printr "a stores " + a
printr "b stores " + b

This time we're using the Basic4GL printr command. This is exactly like the print command except that it returns the cursor to the start of the next line after it has finished.

Variables can be used anywhere that you would normally put a number - in expressions or as parameters for commands. For example, consider the following program.

dim a, b, c
a = 5 
b = 3 + 4
c = a + b
printr "a = " + a
printr "b = " + b
printr "c = " + c
printr "b + c = " + (b + c)

(Type it in and run it if you want.)

This program uses an expression 3 + 4 to calculate the value to store in b:

b = 3 + 4

It uses a + b to calculate the value to store in c:

c = a + b

It uses b + c to calculate a value to use in a parameter to printr:

printr "b + c = " + (b + c)

Choosing variable names

So far we have used a, b and c as variable names, but we don't have to stick with single letters if we don't want. In our program, the variables didn't really have any purpose, but in most programs they do, so it is a good idea to try to give them names that match what it is they are for. This makes the program easier to understand when you come back to it.
If we are storing the number of lives the player has and his/her score, we could write this:

dim a, b
a = 3
b = 1020

this works fine, but if we instead wrote:

dim lives, score
lives = 3
score = 1020

it makes it a lot easier to understand what the program is trying to do.

There are a few rules you have to follow when choosing the names for your variables, otherwise the compiler won't compile and run your program:

Some examples of acceptable variable names:
cat, a1, my_shoe_size, MaximumHealth, counter

Some examples of unacceptable variable names:
cat fish, 2many, health&armour

Variable types

All the variables we have seen so far are integer variables.
We can't use them to store text strings.

dim a
a = "my house"

Would not work.

We can't use them to store real numbers with fractions.

dim a
a = 3.443

would compile and run, however it would round 3.443 to 3 before storing it in a.

So how do we tell the computer we want to store a text string instead of an integer?
To store a text string, we have tell the computer to allocate a text string variable.
We do this by appending a dollar sign ($) to the variable name when we declare it.

Here's another example to try.

dim school$, schoolMotto$

school$ = "Scott Base elementary"
schoolMotto$ = "If it moves, don't eat it"

printr "Welcome to " + school$
printr "Our motto is"
printr schoolMotto$

We've created two text string variables:

dim school$, schoolMotto$

Because they end in a dollar sign, the computer knows that we want to store text string data inside them.
We've stored some text inside them:

school$ = "Scott Base elementary"
schoolMotto$ = "If it moves, don't eat it"

And we've used them in expressions and as parameters to printr

printr "Welcome to " + school$
printr "Our motto is"
printr schoolMotto$

So that's how we store text in variables.

If we want to tell the computer to store a real number in a variable, we have to append a hash sign (#) to the variable name..

For example:

dim l#, w#, diameter#

l# = 10
w# = 14
diameter# = sqrt (l# * l# + w# * w#)

printr "A " + w# + " by " + l# + " inch screen"
printr "Is " + diameter# + " inches in diameter"
Go to page << 1 2 3 4 5 6 7 8 9 10 11 >>