Before we can continue with the space invaders game we need to take a detour to learn a few new techniques. Don't forget to save the space invaders program away to disk first.
A constant is much like a variable.
It has a name (like true, TEXT_BUFFERED, VK_LEFT). It also has a value, which can be either a number or a string.
Unlike variables, the value of a constant never changes.
Basic4GL has a number of pre-defined "constants". These are constants that Basic4GL already knows about, without you having to tell it first.
Here are some pre-defined constants:
printr "m_pi = " + m_pi printr "TEXT_BUFFERED = " + TEXT_BUFFERED printr "VK_LEFT = " + VK_LEFT
Sometimes constants are used as an easy way to remember a commonly used number, such as Pi (stored in the constant m_pi).
Sometimes constants are used to associate a more meaningful
name with a number. For example VK_LEFT stores
the scan code of the left arrow key, which happens to be 37.
The computer knows that 37 means the left arrow key, but I'm less likely to remember that. But if I use the VK_LEFT constant in a program, I can easily see the program is trying to do.
We will be using the following constants to find out which keys are being pressed, so we can move the gun turret accordingly:
A function is like a command. It has a name which Basic4GL
recognises, and sometimes has one or more parameters.
The main difference is that a function evaluates to a number or a string. We call this the "return value". We can use this value anywhere we would normally use a value, e.g. in an expression, as a parameter to another command or function. We can also assign it to a variable.
Some examples of functions:
printr "Half the square root of 2 is " + sqrt (2) / 2 printr "A random number: " + rnd () dim a$, helloLength a$ = "Hello" helloLength = len (a$) print a$ + " has " + helloLength + " letters"
sqrt, rnd, and len
are all functions.
As you can see, they all evaluate to a number or string that can be used like any other value.
Basic4GL has a large number of functions that calculate a number of different things.
Right! Now we've had a very quick crash course on functions
and constants, we can introduce the ScanKeyDown
We are going to use this function to find out whether keys are being pressed or not. The function takes a single parameter which is the "scan code" of the key that we are interested in. Because I don't know all the keyboard scan key codes off the top of my head, we're going to use special predefined constants: VK_LEFT, VK_RIGHT and VK_SPACE.
Here's how it all fits together:
while true cls print ScanKeyDown (VK_SPACE) wend
(Tap the spacebar while the program is running to see it in effect.)
ScanKeyDown (VK_SPACE) evaluates to -1 when
the spacebar is being pressed, and 0 when it isn't.
(-1 and 0 are actually special "boolean" values true and false. But we will worry about boolean values later.)
Here's another example:
while true cls if ScanKeyDown (VK_UP) then locate 19, 4: print "Up" endif if ScanKeyDown (VK_DOWN) then locate 18, 19: print "Down" endif if ScanKeyDown (VK_LEFT) then locate 8, 12: print "Left" endif if ScanKeyDown (VK_RIGHT) then locate 28, 12: print "Right" endif wend
(In this one, you need to press the arrow keys while it's running to see what it does.)
This example uses the if..then..endif statement, which will be explained very soon.